-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
ctx.go
328 lines (276 loc) · 11.1 KB
/
ctx.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package fuego
import (
"context"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-fuego/fuego/internal"
)
const (
maxBodySize = 1048576
)
type ContextNoBody = ContextWithBody[any]
// ContextWithBody is the context of the request.
// It contains the request body, the path parameters, the query parameters, and the HTTP request.
// Please do not use a pointer type as parameter.
type ContextWithBody[B any] interface {
context.Context
ValidableCtx
// Body returns the body of the request.
// If (*B) implements [InTransformer], it will be transformed after deserialization.
// It caches the result, so it can be called multiple times.
Body() (B, error)
// MustBody works like Body, but panics if there is an error.
MustBody() B
// PathParam returns the path parameter with the given name.
// If it does not exist, it returns an empty string.
// Example:
// fuego.Get(s, "/recipes/{recipe_id}", func(c fuego.ContextNoBody) (any, error) {
// id := c.PathParam("recipe_id")
// ...
// })
PathParam(name string) string
QueryParam(name string) string
QueryParamArr(name string) []string
QueryParamInt(name string) int // If the query parameter is not provided or is not an int, it returns the default given value. Use [Ctx.QueryParamIntErr] if you want to know if the query parameter is erroneous.
QueryParamIntErr(name string) (int, error)
QueryParamBool(name string) bool // If the query parameter is not provided or is not a bool, it returns the default given value. Use [Ctx.QueryParamBoolErr] if you want to know if the query parameter is erroneous.
QueryParamBoolErr(name string) (bool, error)
QueryParams() url.Values
MainLang() string // ex: fr. MainLang returns the main language of the request. It is the first language of the Accept-Language header. To get the main locale (ex: fr-CA), use [Ctx.MainLocale].
MainLocale() string // ex: en-US. MainLocale returns the main locale of the request. It is the first locale of the Accept-Language header. To get the main language (ex: en), use [Ctx.MainLang].
// Render renders the given templates with the given data.
// Example:
// fuego.Get(s, "/recipes", func(c fuego.ContextNoBody) (any, error) {
// recipes, _ := rs.Queries.GetRecipes(c.Context())
// ...
// return c.Render("pages/recipes.page.html", recipes)
// })
// For the Go templates reference, see https://pkg.go.dev/html/template
//
// [templateGlobsToOverride] is a list of templates to override.
// For example, if you have 2 conflicting templates
// - with the same name "partials/aaa/nav.partial.html" and "partials/bbb/nav.partial.html"
// - or two templates with different names, but that define the same block "page" for example,
// and you want to override one above the other, you can do:
// c.Render("admin.page.html", recipes, "partials/aaa/nav.partial.html")
// By default, [templateToExecute] is added to the list of templates to override.
Render(templateToExecute string, data any, templateGlobsToOverride ...string) (CtxRenderer, error)
Cookie(name string) (*http.Cookie, error) // Get request cookie
SetCookie(cookie http.Cookie) // Sets response cookie
Header(key string) string // Get request header
SetHeader(key, value string) // Sets response header
// Returns the underlying net/http, gin or echo context.
//
// Usage:
// ctx := c.Context() // net/http: the [context.Context] of the *http.Request
// ctx := c.Context().(*gin.Context) // gin: Safe because the underlying context is always a [gin.Context]
// ctx := c.Context().(echo.Context) // echo: Safe because the underlying context is always a [echo.Context]
Context() context.Context
Request() *http.Request // Request returns the underlying HTTP request.
Response() http.ResponseWriter // Response returns the underlying HTTP response writer.
// SetStatus sets the status code of the response.
// Alias to http.ResponseWriter.WriteHeader.
SetStatus(code int)
// Redirect redirects to the given url with the given status code.
// Example:
// fuego.Get(s, "/recipes", func(c fuego.ContextNoBody) (any, error) {
// ...
// return c.Redirect(301, "/recipes-list")
// })
Redirect(code int, url string) (any, error)
}
// NewNetHTTPContext returns a new context. It is used internally by Fuego. You probably want to use Ctx[B] instead.
func NewNetHTTPContext[B any](route BaseRoute, w http.ResponseWriter, r *http.Request, options readOptions) *netHttpContext[B] {
c := &netHttpContext[B]{
CommonContext: internal.CommonContext[B]{
CommonCtx: r.Context(),
UrlValues: r.URL.Query(),
OpenAPIParams: route.Params,
DefaultStatusCode: route.DefaultStatusCode,
},
Req: r,
Res: w,
readOptions: options,
}
return c
}
// ContextWithBody is the same as fuego.ContextNoBody, but
// has a Body. The Body type parameter represents the expected data type
// from http.Request.Body. Please do not use a pointer as a type parameter.
type netHttpContext[Body any] struct {
internal.CommonContext[Body]
body *Body // Cache the body in request context, because it is not possible to read an HTTP request body multiple times.
Req *http.Request
Res http.ResponseWriter
fs fs.FS
templates *template.Template
readOptions readOptions
serializer Sender
errorSerializer ErrorSender
}
var (
_ ContextWithBody[any] = &netHttpContext[any]{} // Check that ContextWithBody implements Ctx.
_ ContextWithBody[string] = &netHttpContext[string]{} // Check that ContextWithBody implements Ctx.
_ ValidableCtx = &netHttpContext[any]{} // Check that ContextWithBody implements ValidableCtx.
)
// SetStatus sets the status code of the response.
// Alias to http.ResponseWriter.WriteHeader.
func (c netHttpContext[B]) SetStatus(code int) {
c.Res.WriteHeader(code)
}
// readOptions are options for reading the request body.
type readOptions struct {
DisallowUnknownFields bool
MaxBodySize int64
LogBody bool
}
func (c netHttpContext[B]) Redirect(code int, url string) (any, error) {
http.Redirect(c.Res, c.Req, url, code)
return nil, nil
}
// Get request header
func (c netHttpContext[B]) Header(key string) string {
return c.Request().Header.Get(key)
}
// Has request header
func (c netHttpContext[B]) HasHeader(key string) bool {
return c.Header(key) != ""
}
// Sets response header
func (c netHttpContext[B]) SetHeader(key, value string) {
c.Response().Header().Set(key, value)
}
// Get request cookie
func (c netHttpContext[B]) Cookie(name string) (*http.Cookie, error) {
return c.Request().Cookie(name)
}
// Has request cookie
func (c netHttpContext[B]) HasCookie(name string) bool {
_, err := c.Cookie(name)
return err == nil
}
// Sets response cookie
func (c netHttpContext[B]) SetCookie(cookie http.Cookie) {
http.SetCookie(c.Response(), &cookie)
}
// Render renders the given templates with the given data.
// It returns just an empty string, because the response is written directly to the http.ResponseWriter.
//
// Init templates if not already done.
// This has the side effect of making the Render method static, meaning
// that the templates will be parsed only once, removing
// the need to parse the templates on each request but also preventing
// to dynamically use new templates.
func (c netHttpContext[B]) Render(templateToExecute string, data any, layoutsGlobs ...string) (CtxRenderer, error) {
return &StdRenderer{
templateToExecute: templateToExecute,
templates: c.templates,
layoutsGlobs: layoutsGlobs,
fs: c.fs,
data: data,
}, nil
}
// PathParams returns the path parameters of the request.
func (c netHttpContext[B]) PathParam(name string) string {
return c.Req.PathValue(name)
}
func (c netHttpContext[B]) MainLang() string {
return strings.Split(c.MainLocale(), "-")[0]
}
func (c netHttpContext[B]) MainLocale() string {
return strings.Split(c.Req.Header.Get("Accept-Language"), ",")[0]
}
// Request returns the HTTP request.
func (c netHttpContext[B]) Request() *http.Request {
return c.Req
}
// Response returns the HTTP response writer.
func (c netHttpContext[B]) Response() http.ResponseWriter {
return c.Res
}
// MustBody works like Body, but panics if there is an error.
func (c *netHttpContext[B]) MustBody() B {
b, err := c.Body()
if err != nil {
panic(err)
}
return b
}
// Body returns the body of the request.
// If (*B) implements [InTransformer], it will be transformed after deserialization.
// It caches the result, so it can be called multiple times.
// The reason the body is cached is that it is impossible to read an HTTP request body multiple times, not because of performance.
// For decoding, it uses the Content-Type header. If it is not set, defaults to application/json.
func (c *netHttpContext[B]) Body() (B, error) {
if c.body != nil {
return *c.body, nil
}
body, err := body[B](*c)
c.body = &body
return body, err
}
// Serialize serializes the given data to the response. It uses the Content-Type header to determine the serialization format.
func (c netHttpContext[B]) Serialize(data any) error {
if c.serializer == nil {
return Send(c.Res, c.Req, data)
}
return c.serializer(c.Res, c.Req, data)
}
// SerializeError serializes the given error to the response. It uses the Content-Type header to determine the serialization format.
func (c netHttpContext[B]) SerializeError(err error) {
if c.errorSerializer == nil {
SendError(c.Res, c.Req, err)
return
}
c.errorSerializer(c.Res, c.Req, err)
}
// setDefaultStatusCode sets the default status code of the response.
func (c netHttpContext[B]) SetDefaultStatusCode() {
if c.DefaultStatusCode != 0 {
c.SetStatus(c.DefaultStatusCode)
}
}
func body[B any](c netHttpContext[B]) (B, error) {
// Limit the size of the request body.
if c.readOptions.MaxBodySize != 0 {
c.Req.Body = http.MaxBytesReader(nil, c.Req.Body, c.readOptions.MaxBodySize)
}
timeDeserialize := time.Now()
var body B
var err error
switch c.Req.Header.Get("Content-Type") {
case "text/plain":
s, errReadingString := readString[string](c.Req.Context(), c.Req.Body, c.readOptions)
body = any(s).(B)
err = errReadingString
case "application/x-www-form-urlencoded", "multipart/form-data":
body, err = readURLEncoded[B](c.Req, c.readOptions)
case "application/xml":
body, err = readXML[B](c.Req.Context(), c.Req.Body, c.readOptions)
case "application/x-yaml", "text/yaml; charset=utf-8", "application/yaml": // https://www.rfc-editor.org/rfc/rfc9512.html
body, err = readYAML[B](c.Req.Context(), c.Req.Body, c.readOptions)
case "application/octet-stream":
// Read c.Req Body to bytes
bytes, err := io.ReadAll(c.Req.Body)
if err != nil {
return body, err
}
respBytes, ok := any(bytes).(B)
if !ok {
return body, fmt.Errorf("could not convert bytes to %T. To read binary data from the request, use []byte as the body type", body)
}
body = respBytes
case "application/json":
fallthrough
default:
body, err = readJSON[B](c.Req.Context(), c.Req.Body, c.readOptions)
}
c.Res.Header().Add("Server-Timing", Timing{"deserialize", time.Since(timeDeserialize), "controller > deserialize"}.String())
return body, err
}