-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
177 lines (161 loc) · 4 KB
/
context.go
File metadata and controls
177 lines (161 loc) · 4 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package slide
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"github.com/valyala/fasthttp"
)
// Ctx -- route level context
type Ctx struct {
RequestCtx *fasthttp.RequestCtx
Next func() error
config *Config
appMiddlewareIndex int
groupMiddlewareIndex int
routerPath string
queryPath string
}
// JSON Sending application/json response
func (ctx *Ctx) JSON(statusCode int, payload interface{}) error {
ctx.RequestCtx.Response.Header.Set(ContentType, ApplicationJSON)
ctx.RequestCtx.SetStatusCode(statusCode)
response, err := json.Marshal(payload)
if err != nil {
return err
}
ctx.RequestCtx.SetBody(response)
return nil
}
// Send Sending a text response
func (ctx *Ctx) Send(statusCode int, payload string) error {
ctx.RequestCtx.SetStatusCode(statusCode)
ctx.RequestCtx.SetBody([]byte(payload))
return nil
}
// SendStatusCode -- sending response without any body
func (ctx *Ctx) SendStatusCode(statusCode int) error {
ctx.RequestCtx.SetStatusCode(statusCode)
return nil
}
// Redirect to new url
// reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#Temporary_redirections
// status codes between 300-308
func (ctx *Ctx) Redirect(statusCode int, url string) error {
ctx.RequestCtx.Redirect(url, statusCode)
return nil
}
// SendAttachment Sending attachment
//
// here fileName is optional
func (ctx *Ctx) SendAttachment(filePath, fileName string) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
content, err := ioutil.ReadAll(f)
if err != nil {
return err
}
contentType, err := getFileContentType(filePath)
if err != nil {
panic(err)
}
ctx.RequestCtx.Response.Header.Set(ContentType, contentType)
headerValue := getAttachmentHeader(fileName)
ctx.RequestCtx.Response.Header.Set(ContentDeposition, headerValue)
ctx.RequestCtx.SetBody(content)
return nil
}
// UploadFile uploads file to given path
func (ctx *Ctx) UploadFile(filePath, fileName string) error {
form, err := ctx.RequestCtx.FormFile(fileName)
if err != nil {
return err
}
file, err := form.Open()
if err != nil {
return err
}
defer file.Close()
out, err := os.Create(filePath)
if err != nil {
return err
}
_, err = io.Copy(out, file)
if err != nil {
return err
}
defer out.Close()
return nil
}
// Bind Deserialize body to struct
func (ctx *Ctx) Bind(input interface{}) error {
body := ctx.RequestCtx.Request.Body()
if err := json.Unmarshal(body, input); err != nil {
return err
}
if ctx.config.Validator != nil {
if err := ctx.config.Validator.Struct(input); err != nil {
return err
}
}
return nil
}
// GetParam - Getting path param
//
// /name/:name
//
// /name/slide
//
// name := ctx.GetParam("name")
//
// name = slide
//
func (ctx *Ctx) GetParam(name string) string {
return extractParamFromPath(ctx.routerPath, string(ctx.RequestCtx.Path()), name)
}
// GetParams returns map of path params
//
// routerPath /auth/:name/:age
//
// requestPath /auth/madhuri/32
//
// returns { name: madhuri, age: 32 }
//
func (ctx *Ctx) GetParams() map[string]string {
return getParamsFromPath(ctx.routerPath, string(ctx.RequestCtx.Path()))
}
// GetQueryParam returns value of a single query Param
//
// route path /hello?key=test&value=bbp
//
// keyValue = GetQueryParam(key)
//
// keyValue = test
func (ctx *Ctx) GetQueryParam(name string) string {
return getQueryParam(ctx.queryPath, name)
}
// GetQueryParams returns map of query Params
//
// route path /hello?key=test&value=bbp
//
// returns {key : test, value : bbp}
func (ctx *Ctx) GetQueryParams() map[string]string {
return getAllQueryParams(ctx.queryPath)
}
// ServeFile serving file as response
func (ctx *Ctx) ServeFile(filePath string) error {
contentType, err := getFileContentType(filePath)
if err != nil {
return err
}
ctx.RequestCtx.Response.Header.Set(ContentType, contentType)
return ctx.RequestCtx.Response.SendFile(filePath)
}
func getRouterContext(r *fasthttp.RequestCtx, slide *Slide) *Ctx {
return &Ctx{
RequestCtx: r,
config: slide.config,
}
}