-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
118 lines (101 loc) · 2.55 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package wrap
import (
"github.com/torniker/wrap/request"
"github.com/torniker/wrap/response"
)
// HandlerFunc defines handler function
type HandlerFunc func(*Ctx) error
// Ctx is struct where information for each request is stored
type Ctx struct {
Prog *Prog
Request request.Request
Response response.Response
Store map[string]interface{}
User Userer
}
// Post handles checks if the request method and calls HandlerFunc
func (ctx *Ctx) Post(f HandlerFunc) {
if ctx.Request.Action() == request.POST {
ctx.call(f)
}
}
// Get handles checks if the request method and calls HandlerFunc
func (ctx *Ctx) Get(f HandlerFunc) {
if ctx.Request.Action() == request.GET {
ctx.call(f)
}
}
// Put handles checks if the request method and calls HandlerFunc
func (ctx *Ctx) Put(f HandlerFunc) {
if ctx.Request.Action() == request.PUT {
ctx.call(f)
}
}
// Delete handles checks if the request method and calls HandlerFunc
func (ctx *Ctx) Delete(f HandlerFunc) {
if ctx.Request.Action() == request.DELETE {
ctx.call(f)
}
}
func (ctx *Ctx) call(f HandlerFunc) {
if ctx.Response.Commited() {
return
}
err := f(ctx)
if err != nil {
ctx.Error(err)
}
}
// Next calls pathed function with next url segment and increases segment index by 1
func (ctx *Ctx) Next(f HandlerFunc) {
ctx.Request.Path().Increment()
ctx.call(f)
}
// ResJSON is a struct for wrapping a JSON data response
type ResJSON struct {
Data interface{} `json:"data"`
}
// JSON responses with json body
func (ctx *Ctx) JSON(body interface{}) error {
ctx.Response.SetHeader("Content-Type", "application/json")
return ctx.Response.Write(ResJSON{Data: body})
}
// NoContent responses with status 204 No Content
func (ctx *Ctx) NoContent() {
ctx.Response.SuccessWithNoContent()
}
// IsGET handles checks if the request method is GET
func (ctx *Ctx) IsGET() bool {
if ctx.Request.Action() == request.GET {
return true
}
return false
}
// IsPOST handles checks if the request method is POST
func (ctx *Ctx) IsPOST() bool {
if ctx.Request.Action() == request.POST {
return true
}
return false
}
// IsPUT handles checks if the request method is PUT
func (ctx *Ctx) IsPUT() bool {
if ctx.Request.Action() == request.PUT {
return true
}
return false
}
// IsDELETE handles checks if the request method is DELETE
func (ctx *Ctx) IsDELETE() bool {
if ctx.Request.Action() == request.DELETE {
return true
}
return false
}
// IsOPTIONS handles checks if the request method is OPTIONS
func (ctx *Ctx) IsOPTIONS() bool {
if ctx.Request.Action() == request.OPTIONS {
return true
}
return false
}