/
context.go
1073 lines (876 loc) · 25.2 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package zoox
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"os"
"sync"
"github.com/go-errors/errors"
"time"
"github.com/go-zoox/cache"
"github.com/go-zoox/fs"
"github.com/go-zoox/i18n"
"github.com/go-zoox/proxy"
"github.com/go-zoox/zoox/components/application/cmd"
"github.com/go-zoox/zoox/components/application/cron"
"github.com/go-zoox/zoox/components/application/debug"
"github.com/go-zoox/zoox/components/application/env"
"github.com/go-zoox/zoox/components/application/jobqueue"
"github.com/go-zoox/zoox/components/context/body"
"github.com/go-zoox/zoox/components/context/form"
"github.com/go-zoox/zoox/components/context/mq"
"github.com/go-zoox/zoox/components/context/param"
"github.com/go-zoox/zoox/components/context/pubsub"
"github.com/go-zoox/zoox/components/context/query"
"github.com/go-zoox/zoox/components/context/sse"
"github.com/go-zoox/zoox/components/context/state"
"github.com/go-zoox/zoox/components/context/user"
"github.com/go-zoox/zoox/utils"
"github.com/go-zoox/concurrency"
"github.com/go-zoox/cookie"
"github.com/go-zoox/core-utils/safe"
"github.com/go-zoox/core-utils/strings"
"github.com/go-zoox/fetch"
"github.com/go-zoox/headers"
"github.com/go-zoox/jwt"
"github.com/go-zoox/logger"
"github.com/go-zoox/random"
"github.com/go-zoox/session"
"github.com/go-zoox/tag"
"github.com/go-zoox/tag/datasource"
"gopkg.in/yaml.v3"
)
// Context is the request context
type Context struct {
// Writer is the response writer.
Writer ResponseWriter
// Request is the original request object.
Request *http.Request
// Request is the alias of Writer.
Response ResponseWriter
// request
Method string
Path string
//
param param.Param
query query.Query
form form.Form
body body.Body
// response
sse sse.SSE
//
cookie cookie.Cookie
session session.Session
jwt jwt.Jwt
//
cache cache.Cache
cron cron.Cron
queue jobqueue.JobQueue
//
i18n i18n.I18n
//
pubsub pubsub.PubSub
mq mq.MQ
//
env env.Env
debug debug.Debug
// middleware
handlers []HandlerFunc
index int
//
App *Application
//
Logger *logger.Logger
//
//
state state.State
user user.User
//
cmd cmd.Cmd
// request id
requestID string
//
isUpgrade bool
isUpgradeSet bool
// bodyBytes is used to copy body
bodyBytes []byte
// once
once struct {
debug sync.Once
//
cache sync.Once
queue sync.Once
env sync.Once
//
i18n sync.Once
//
pubsub sync.Once
mq sync.Once
//
cron sync.Once
jwt sync.Once
sse sync.Once
//
cookie sync.Once
session sync.Once
//
query sync.Once
form sync.Once
body sync.Once
//
state sync.Once
user sync.Once
//
cmd sync.Once
}
}
func newContext(app *Application, w http.ResponseWriter, req *http.Request) *Context {
// path := strings.TrimSuffix(req.URL.Path, "/")
path := req.URL.Path
// path := req.URL.Path
// if !strings.HasSuffix(path, "/") {
// path += "/"
// }
writer := newResponseWriter(w)
ctx := &Context{
App: app,
//
Writer: writer,
//
Request: req,
Response: writer,
//
Method: req.Method,
Path: path,
//
index: -1,
}
//
ctx.requestID = ctx.Header().Get(utils.RequestIDHeader)
if ctx.requestID == "" {
ctx.requestID = utils.GenerateRequestID()
}
ctx.Logger = logger.New(&logger.Options{
Level: app.Config.LogLevel,
})
return ctx
}
// Context returns the context
func (ctx *Context) Context() context.Context {
return ctx.Request.Context()
}
// Next runs the next handler in the middleware stack
func (ctx *Context) Next() {
ctx.index++
s := len(ctx.handlers)
// for ; ctx.index < s; ctx.index ++ {
// ctx.handlers[ctx.index](ctx)
// }
if ctx.index >= s {
panic("Handler cannot call ctx.Next")
}
ctx.handlers[ctx.index](ctx)
}
// Query returns the query string parameter with the given name.
func (ctx *Context) Query() query.Query {
ctx.once.query.Do(func() {
ctx.query = query.New(ctx.Request)
})
return ctx.query
}
// Param returns the named URL parameter value if it exists.
func (ctx *Context) Param() param.Param {
return ctx.param
}
// Header gets the header value by key.
func (ctx *Context) Header() http.Header {
return ctx.Request.Header
}
// Form returns the form data from POST or PUT request body.
func (ctx *Context) Form() form.Form {
ctx.once.form.Do(func() {
ctx.form = form.New(ctx.Request)
})
return ctx.form
}
// Body returns the request body.
func (ctx *Context) Body() body.Body {
ctx.once.body.Do(func() {
ctx.body = body.New(ctx.Bodies)
})
return ctx.body
}
// Status sets the HTTP response status code.
func (ctx *Context) Status(status int) {
ctx.Writer.WriteHeader(status)
}
// StatusCode returns the HTTP response status code.
func (ctx *Context) StatusCode() int {
return ctx.Writer.Status()
}
// Get alias for ctx.Header.
func (ctx *Context) Get(key string) string {
return ctx.Header().Get(key)
}
// Set alias for ctx.SetHeader.
func (ctx *Context) Set(key string, value string) {
ctx.SetHeader(key, value)
}
// SetHeader sets a header in the response.
func (ctx *Context) SetHeader(key string, value string) {
ctx.Writer.Header().Set(key, value)
}
// AddHeader adds a header to the response.
func (ctx *Context) AddHeader(key string, value string) {
ctx.Writer.Header().Add(key, value)
}
// SSE sets the response header for server-sent events.
func (ctx *Context) SSE() sse.SSE {
ctx.once.sse.Do(func() {
ctx.sse = sse.New(ctx.Writer)
})
return ctx.sse
}
// BasicAuth returns the user/password pair for Basic Authentication.
func (ctx *Context) BasicAuth() (username string, password string, ok bool) {
return ctx.Request.BasicAuth()
}
// Authorization returns the authorization header for auth.
func (ctx *Context) Authorization() string {
return ctx.Header().Get(headers.Authorization)
}
// BearerToken returns the token for bearer authentication.
func (ctx *Context) BearerToken() (token string, ok bool) {
authorization := ctx.Authorization()
if len(authorization) < 8 {
return "", false
}
return authorization[7:], true
}
// Accept returns the request accept header.
func (ctx *Context) Accept() string {
return ctx.Header().Get(headers.Accept)
}
// AcceptLanguage returns the request accept header.
func (ctx *Context) AcceptLanguage() string {
return ctx.Header().Get(headers.AcceptLanguage)
}
// AcceptEncoding returns the request accept header.
func (ctx *Context) AcceptEncoding() string {
return ctx.Header().Get(headers.AcceptEncoding)
}
// Connection return the request connection header.
func (ctx *Context) Connection() string {
return ctx.Header().Get(headers.Connection)
}
// UserAgent return the request user-agent header.
func (ctx *Context) UserAgent() string {
return ctx.Header().Get(headers.UserAgent)
}
// ContentType return the request content-type header.
func (ctx *Context) ContentType() string {
return ctx.Header().Get(headers.ContentType)
}
// XForwardedFor return the request x-forwarded-for header.
func (ctx *Context) XForwardedFor() string {
return ctx.Header().Get(headers.XForwardedFor)
}
// XForwardedProto return the request x-forwarded-proto header.
func (ctx *Context) XForwardedProto() string {
return ctx.Header().Get(headers.XForwardedProto)
}
// XForwardedHost return the request x-forwarded-host header.
func (ctx *Context) XForwardedHost() string {
return ctx.Header().Get(headers.XForwardedHost)
}
// XForwardedPort return the request x-forwarded-port header.
func (ctx *Context) XForwardedPort() string {
return ctx.Header().Get(headers.XForwardedPort)
}
// XRealIP return the request x-real-ip header.
func (ctx *Context) XRealIP() string {
return ctx.Header().Get(headers.XRealIP)
}
// Upgrade return the request upgrade header.
func (ctx *Context) Upgrade() string {
return ctx.Header().Get(headers.Upgrade)
}
// Origin returns the origin of the request.
func (ctx *Context) Origin() string {
return ctx.Header().Get(headers.Origin)
}
// Referrer returns the referrer of the request.
func (ctx *Context) Referrer() string {
return ctx.Header().Get(headers.Referrer)
}
// IsConnectionUpgrade checks if the connection upgrade.
func (ctx *Context) IsConnectionUpgrade() bool {
if !ctx.isUpgradeSet {
ctx.isUpgradeSet = true
ctx.isUpgrade = strings.ToLower(ctx.Connection()) == "upgrade"
}
return ctx.isUpgrade
}
// Write writes the data to the connection.
func (ctx *Context) Write(b []byte) {
ctx.Writer.Write(b)
}
// String writes the given string to the response.
func (ctx *Context) String(status int, format string, values ...interface{}) {
ctx.Status(status)
ctx.Write([]byte(fmt.Sprintf(format, values...)))
}
// JSON serializes the given struct as JSON into the response body.
func (ctx *Context) JSON(status int, obj interface{}) {
ctx.Status(status)
ctx.SetHeader(headers.ContentType, "application/json")
encoder := json.NewEncoder(ctx.Writer)
if err := encoder.Encode(obj); err != nil {
// ctx.Error(http.StatusInternalServerError, err.Error())
ctx.Logger.Errorf("[ctx.JSON] encode error: %s", err)
ctx.String(http.StatusInternalServerError, err.Error())
}
}
// Data writes some data into the body stream and updates the HTTP code.
// Align to gin framework.
func (ctx *Context) Data(status int, contentType string, data []byte) {
ctx.Status(status)
ctx.SetHeader(headers.ContentType, contentType)
ctx.Write(data)
}
// HTML renders the given template with the given data and writes the result
func (ctx *Context) HTML(status int, html string, data ...any) {
ctx.Template(status, func(tc *TemplateConfig) {
tc.ContentType = "text/html"
tc.Content = html
if len(data) > 0 {
tc.Data = data[0]
}
})
}
// Render renders a template with data and writes the result to the response.
func (ctx *Context) Render(status int, name string, data interface{}) {
ctx.Template(status, func(tc *TemplateConfig) {
tc.Name = name
tc.Data = data
})
}
// RenderHTML renders a template with data and writes the result to the response.
func (ctx *Context) RenderHTML(filepath string) {
if !strings.StartsWith(filepath, "/") {
filepath = fs.JoinCurrentDir(filepath)
}
cacheKey := fmt.Sprintf("static_fs:%s", filepath)
html := ""
err := ctx.Cache().Get(cacheKey, &html)
if err != nil {
html, err = fs.ReadFileAsString(filepath)
if err != nil {
ctx.Error(http.StatusInternalServerError, fmt.Errorf("failed to read index.html: %s", err).Error())
return
}
ctx.Cache().Set(cacheKey, &html, 60*time.Second)
}
ctx.HTML(http.StatusOK, html)
}
// RenderIndexHTML renders the index.html file from the static directory.
func (ctx *Context) RenderIndexHTML(dir string) {
ctx.RenderHTML(fs.JoinPath(dir, "index.html"))
}
// RenderStatic renders the static file from the static directory.
func (ctx *Context) RenderStatic(prefix, dir string) {
hf := http.Dir(dir)
hfs := http.StripPrefix(prefix, http.FileServer(hf))
hfs.ServeHTTP(ctx.Writer, ctx.Request)
}
// Error writes the given error to the response.
// Use for system errors
// 1. Internal server error
// 2. Not found
func (ctx *Context) Error(status int, message string) {
// ctx.Status(status)
// ctx.Write([]byte(message))
if ctx.AcceptJSON() {
ctx.JSON(status, H{
"code": status,
"message": message,
"method": ctx.Method,
"path": ctx.Path,
"timestamp": time.Now(),
})
return
}
ctx.String(status, message)
}
// Success writes the given data with code-message-result specification to the response.
func (ctx *Context) Success(result interface{}) {
ctx.JSON(http.StatusOK, H{
"code": 200,
"message": "success",
"result": result,
})
}
// Fail writes the given error with code-message-result specification to the response.
func (ctx *Context) Fail(err error, code int, message string, status ...int) {
statusX := http.StatusBadRequest
if len(status) > 0 {
statusX = status[0]
}
// httprequest, _ := httputil.DumpRequest(ctx.Request, false)
// goErr := errors.Wrap(err, 3)
// reset := string([]byte{27, 91, 48, 109})
// ctx.Logger.Errorf("[Nice ctx.Fail] error:\n\n%s%s\n\n%s%s", httprequest, goErr.Error(), goErr.Stack(), reset)
ctx.Logger.Infof("[ctx.Fail] error: %s (raw: %#v)", err, err)
ctx.JSON(statusX, map[string]any{
"code": code,
"message": message,
})
}
// FailWithError writes the given error with code-message-result specification to the response.
func (ctx *Context) FailWithError(err HTTPError) {
ctx.Fail(err.Raw(), err.Code(), err.Message(), err.Status())
}
// Redirect redirects the request to the given URL.
func (ctx *Context) Redirect(url string, status ...int) {
ctx.SetLocation(url)
code := http.StatusFound
if len(status) == 1 && status[0] != 0 {
code = status[0]
}
ctx.Status(code)
}
// RedirectTemporary redirects the request temporarily to the given URL.
func (ctx *Context) RedirectTemporary(url string) {
ctx.Redirect(url, http.StatusFound)
}
// RedirectPermanent redirects the request permanently to the given URL.
func (ctx *Context) RedirectPermanent(url string) {
ctx.Redirect(url, http.StatusMovedPermanently)
}
// RedirectSeeOther redirects the request to the given URL.
func (ctx *Context) RedirectSeeOther(url string) {
ctx.Redirect(url, http.StatusSeeOther)
}
// RedirectTemporaryWithOriginMethodAndBody redirects the request temporarily to the given URL with the origin method.
func (ctx *Context) RedirectTemporaryWithOriginMethodAndBody(url string) {
ctx.Redirect(url, http.StatusTemporaryRedirect)
}
// RedirectPermanentWithOriginMethodAndBody redirects the request permanently to the given URL with the origin method.
func (ctx *Context) RedirectPermanentWithOriginMethodAndBody(url string) {
ctx.Redirect(url, http.StatusPermanentRedirect)
}
// Protocol returns the protocol, usally http or https
func (ctx *Context) Protocol() string {
return ctx.Request.URL.Scheme
}
// Host gets the host from HTTP Header.
// format: `host:port`
func (ctx *Context) Host() string {
return ctx.Request.Host
}
// Hostname gets the hostname from HTTP Header.
// format: `hostname`
func (ctx *Context) Hostname() string {
hostname := ctx.Request.URL.Hostname()
if hostname != "" {
return hostname
}
hostname, _ = utils.SplitHostPort(ctx.Request.Host)
return hostname
}
// URL is http.Request.RequestURI.
func (ctx *Context) URL() string {
return ctx.Request.RequestURI
}
// IP gets the ip from X-Forwarded-For or X-Real-IP or RemoteIP.
// RemoteIP parses the IP from Request.RemoteAddr, normializes and returns the IP (without the port).
func (ctx *Context) IP() string {
if xForwardedFor := ctx.Header().Get(headers.XForwardedFor); xForwardedFor != "" {
parts := strings.Split(xForwardedFor, ",")
if len(parts) > 0 && parts[0] != "" {
return parts[0]
}
}
if xRealIP := ctx.Header().Get(headers.XRealIP); xRealIP != "" {
return xRealIP
}
ip, _, err := net.SplitHostPort(strings.TrimSpace(ctx.Request.RemoteAddr))
if err != nil {
return ""
}
return ip
}
// IPs gets the ip from X-Forwarded-For or X-Real-IP or RemoteIP.
// RemoteIP parses the IP from Request.RemoteAddr, normializes and returns the IP (without the port).
func (ctx *Context) IPs() []string {
if xForwardedFor := ctx.Header().Get(headers.XForwardedFor); xForwardedFor != "" {
return strings.Split(xForwardedFor, ",")
}
return []string{ctx.IP()}
}
// ClientIP is the client ip.
func (ctx *Context) ClientIP() string {
return ctx.IP()
}
// Headers gets all headers.
func (ctx *Context) Headers() *safe.Map[string, any] {
headers := safe.NewMap[string, any]()
for key, values := range ctx.Request.Header {
headers.Set(key, values[0])
}
return headers
}
// Queries gets all queries.
func (ctx *Context) Queries() *safe.Map[string, any] {
queries := safe.NewMap[string, any]()
for key, values := range ctx.Request.URL.Query() {
queries.Set(key, values[0])
}
return queries
}
// Forms gets all forms.
func (ctx *Context) Forms() *safe.Map[string, any] {
forms := safe.NewMap[string, any]()
if err := ctx.Request.ParseForm(); err != nil {
return forms
}
for key, values := range ctx.Request.Form {
forms.Set(key, values[0])
}
return forms
}
// Params gets all params.
func (ctx *Context) Params() *safe.Map[string, any] {
m := safe.NewMap[string, any]()
if ctx.param == nil {
return m
}
for k, v := range ctx.param.Iterator() {
m.Set(k, v)
}
return m
}
// Bodies gets all bodies.
func (ctx *Context) Bodies() map[string]any {
var bodies map[string]any
if bytes, err := ctx.BodyBytes(); err == nil {
if err := json.Unmarshal(bytes, &bodies); err == nil {
return bodies
}
}
return nil
}
// Cookies gets all cookies.
func (ctx *Context) Cookies() map[string]string {
cookies := map[string]string{}
for _, cookie := range ctx.Request.Cookies() {
cookies[cookie.Name] = cookie.Value
}
return cookies
}
// Files gets all files.
func (ctx *Context) Files() map[string]*multipart.FileHeader {
if err := ctx.Request.ParseMultipartForm(32 << 20); err != nil {
return nil
}
if ctx.Request.MultipartForm == nil || ctx.Request.MultipartForm.File == nil {
return nil
}
files := map[string]*multipart.FileHeader{}
for key, file := range ctx.Request.MultipartForm.File {
if len(file) > 0 {
files[key] = file[0]
}
}
return files
}
// File gets the file by key.
func (ctx *Context) File(key string) (multipart.File, *multipart.FileHeader, error) {
return ctx.Request.FormFile(key)
}
// Stream get the body stream.
func (ctx *Context) Stream() io.ReadCloser {
return ctx.Request.Body
}
// GetRawData returns stream data.
// Align to gin framework.
func (ctx *Context) GetRawData() ([]byte, error) {
return ioutil.ReadAll(ctx.Request.Body)
}
// BindJSON binds the request body into the given struct.
func (ctx *Context) BindJSON(obj interface{}) (err error) {
if !strings.Contains(ctx.Header().Get("Content-Type"), "application/json") {
return errors.New("[BindJSON] content-type is not json")
}
if ctx.Request.Body == nil {
return errors.New("invalid request")
}
if ctx.Env().Get("DEBUG_ZOOX_REQUEST_BODY") != "" {
// refernece: golang复用http.request.body - https://zhuanlan.zhihu.com/p/47313038
_, err = ctx.CloneBody()
if err != nil {
return fmt.Errorf("failed to read request body: %v", err)
}
ctx.Logger.Infof("[debug][ctx.BindJSON] body: %s", ctx.bodyBytes)
}
if err := json.NewDecoder(ctx.Request.Body).Decode(obj); err != nil {
// @TODO allow empty body
if err == io.EOF {
return nil
}
return err
}
return nil
}
// BindYAML binds the request body into the given struct.
func (ctx *Context) BindYAML(obj interface{}) (err error) {
if ctx.Request.Body == nil {
return errors.New("invalid request")
}
if ctx.Debug().IsDebugMode() {
// refernece: golang复用http.request.body - https://zhuanlan.zhihu.com/p/47313038
_, err = ctx.CloneBody()
if err != nil {
return fmt.Errorf("failed to read request body: %v", err)
}
ctx.Logger.Infof("[debug][ctx.BindYAML] body: %v", ctx.bodyBytes)
}
return yaml.NewDecoder(ctx.Request.Body).Decode(obj)
}
// BindForm binds the query into the given struct.
func (ctx *Context) BindForm(obj interface{}) error {
forms := ctx.Forms()
if ctx.Debug().IsDebugMode() {
ctx.Logger.Infof("[debug][ctx.BindForm]")
for k, v := range forms.ToMap() {
ctx.Logger.Infof("[debug][ctx.BindForm][detail] %s = %s", k, v)
}
}
return tag.New("form", datasource.GetterToDataSource(forms)).Decode(obj)
}
// BindParams binds the params into the given struct.
func (ctx *Context) BindParams(obj interface{}) error {
params := ctx.Params()
if ctx.Debug().IsDebugMode() {
ctx.Logger.Infof("[debug][ctx.BindParams]")
for k, v := range params.ToMap() {
ctx.Logger.Infof("[debug][ctx.BindParams][detail] %s = %s", k, v)
}
}
return tag.New("param", datasource.GetterToDataSource(params)).Decode(obj)
}
// BindHeader binds the header into the given struct.
func (ctx *Context) BindHeader(obj interface{}) error {
headers := ctx.Headers()
if ctx.Debug().IsDebugMode() {
ctx.Logger.Infof("[debug][ctx.BindHeader]")
for k, v := range headers.ToMap() {
ctx.Logger.Infof("[debug][ctx.BindHeader][detail] %s = %s", k, v)
}
}
return tag.New("header", datasource.GetterToDataSource(headers)).Decode(obj)
}
// BindQuery binds the query into the given struct.
func (ctx *Context) BindQuery(obj interface{}) error {
queries := ctx.Queries()
if ctx.Debug().IsDebugMode() {
ctx.Logger.Infof("[debug][ctx.BindQuery]")
for k, v := range queries.ToMap() {
ctx.Logger.Infof("[debug][ctx.BindQuery][detail] %s = %s", k, v)
}
}
return tag.New("query", datasource.GetterToDataSource(queries)).Decode(obj)
}
// BindBody binds the body into the given struct.
func (ctx *Context) BindBody(obj interface{}) error {
data := ctx.Bodies()
if ctx.Debug().IsDebugMode() {
ctx.Logger.Infof("[debug][ctx.BindBody]")
for k, v := range data {
ctx.Logger.Infof("[debug][ctx.BindBody][detail] %s = %v", k, v)
}
}
return tag.New("body", datasource.NewMapDataSource(data)).Decode(obj)
}
// SaveFile saves the file to the given path.
func (ctx *Context) SaveFile(key, path string) error {
src, _, err := ctx.Request.FormFile(key)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(path)
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
return nil
}
// AcceptJSON returns true if the request accepts json.
func (ctx *Context) AcceptJSON() bool {
accept := ctx.Header().Get(headers.Accept)
// for curl
if accept == "*/*" {
return true
}
return strings.Contains(accept, "application/json")
}
// AcceptHTML returns true if the request accepts html.
func (ctx *Context) AcceptHTML() bool {
return strings.Contains(ctx.Header().Get(headers.Accept), "text/html")
}
// Cache returns the cache of the application.
func (ctx *Context) Cache() cache.Cache {
ctx.once.cache.Do(func() {
ctx.cache = ctx.App.Cache()
})
return ctx.cache
}
// Cron returns the cache of the application.
func (ctx *Context) Cron() cron.Cron {
ctx.once.cron.Do(func() {
ctx.cron = ctx.App.Cron()
})
return ctx.cron
}
// JobQueue returns the queue of the application.
func (ctx *Context) JobQueue() jobqueue.JobQueue {
ctx.once.queue.Do(func() {
ctx.queue = ctx.App.JobQueue()
})
return ctx.queue
}
// I18n returns the i18n of the application.
func (ctx *Context) I18n() i18n.I18n {
ctx.once.i18n.Do(func() {
ctx.i18n = ctx.App.I18n()
})
return ctx.i18n
}
// Debug returns the debug of the app.
func (ctx *Context) Debug() debug.Debug {
ctx.once.debug.Do(func() {
ctx.debug = ctx.App.Debug()
})
return ctx.debug
}
// Env returns the env of the
func (ctx *Context) Env() env.Env {
ctx.once.env.Do(func() {
ctx.env = ctx.App.Env
})
return ctx.env
}
// State returns the state of the
func (ctx *Context) State() state.State {
ctx.once.state.Do(func() {
ctx.state = state.New()
})
return ctx.state
}
// User returns the user of the
func (ctx *Context) User() user.User {
ctx.once.user.Do(func() {
ctx.user = user.New()
})
return ctx.user
}
// Cmd returns the cmd of the request.
func (ctx *Context) Cmd() cmd.Cmd {
ctx.once.cmd.Do(func() {
ctx.cmd = cmd.New(ctx.Context())
})
return ctx.cmd
}
// Cookie returns the cookie of the request.
func (ctx *Context) Cookie() cookie.Cookie {
ctx.once.cookie.Do(func() {
ctx.cookie = cookie.New(
ctx.Writer,
ctx.Request,
)
})
return ctx.cookie
}
// Session returns the session of the request.
func (ctx *Context) Session() session.Session {
ctx.once.session.Do(func() {
secretKey := ctx.App.Config.SecretKey
if secretKey == "" {
secretKey = "go-zoox_" + random.String(24)
}
ctx.session = session.New(ctx.Cookie(), secretKey, &ctx.App.Config.Session)
})
return ctx.session
}
// Jwt returns the jwt of the request.
func (ctx *Context) Jwt() jwt.Jwt {
ctx.once.jwt.Do(func() {
secretKey := ctx.App.Config.SecretKey