-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontext.go
149 lines (121 loc) · 3.54 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package nano
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// Context defines nano request - response context.
type Context struct {
Request *http.Request
Writer http.ResponseWriter
Method string
Path string
Origin string
Params map[string]string
handlers []HandlerFunc
cursor int // used for handlers stack.
}
// newContext is Context constructor.
func newContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{
Request: r,
Writer: w,
Method: r.Method,
Path: r.URL.Path,
Origin: r.Header.Get(HeaderOrigin),
cursor: -1,
}
}
// Next is functions to move cursor to the next handler stack.
func (c *Context) Next() {
// moving cursor.
c.cursor++
if c.cursor < len(c.handlers) {
c.handlers[c.cursor](c)
}
}
// Status is functions to set http status code response.
func (c *Context) Status(statusCode int) {
c.Writer.WriteHeader(statusCode)
}
// SetHeader is functions to set http response header.
func (c *Context) SetHeader(key, value string) {
c.Writer.Header().Set(key, value)
}
// GetRequestHeader returns header value by given key.
func (c *Context) GetRequestHeader(key string) string {
return c.Request.Header.Get(key)
}
// SetContentType is functions to set http content type response header.
func (c *Context) SetContentType(contentType string) {
c.SetHeader(HeaderContentType, contentType)
}
// Param functions is to get request parameter.
func (c *Context) Param(key string) string {
value, _ := c.Params[key]
return value
}
// PostForm is functions to form body field.
func (c *Context) PostForm(key string) string {
return c.Request.FormValue(key)
}
// PostFormDefault return default value when form body field is empty.
func (c *Context) PostFormDefault(key string, defaultValue string) string {
v := c.PostForm(key)
if v == "" {
return defaultValue
}
return v
}
// Query is functions to get url query.
func (c *Context) Query(key string) string {
return c.Request.URL.Query().Get(key)
}
// QueryDefault return default value when url query is empty
func (c *Context) QueryDefault(key string, defaultValue string) string {
v := c.Query(key)
if v == "" {
return defaultValue
}
return v
}
// IsJSON returns true when client send json body.
func (c *Context) IsJSON() bool {
return c.GetRequestHeader(HeaderContentType) == MimeJSON
}
// ParseJSONBody is functions to parse json request body.
func (c *Context) ParseJSONBody(body interface{}) error {
return json.NewDecoder(c.Request.Body).Decode(&body)
}
// ExpectJSON returns true when client request json response
func (c *Context) ExpectJSON() bool {
return strings.Contains(c.GetRequestHeader(HeaderAccept), MimeJSON)
}
// JSON is functions to write json response.
func (c *Context) JSON(statusCode int, object interface{}) {
c.SetContentType(MimeJSON)
c.Status(statusCode)
encoder := json.NewEncoder(c.Writer)
if err := encoder.Encode(object); err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
}
// String is functions to write plain text response.
func (c *Context) String(statusCode int, template string, value ...interface{}) {
c.SetContentType(MimePlainText)
c.Status(statusCode)
text := fmt.Sprintf(template, value...)
c.Writer.Write([]byte(text))
}
// HTML is functions to write html response.
func (c *Context) HTML(statusCode int, html string) {
c.SetContentType(MimeHTML)
c.Status(statusCode)
c.Writer.Write([]byte(html))
}
// Data is functions to write binary response.
func (c *Context) Data(statusCode int, binary []byte) {
c.Status(statusCode)
c.Writer.Write(binary)
}