-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
122 lines (97 loc) · 2.72 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
package godzilla
import (
"fmt"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
const (
MimeApplicationJSON = "application/json"
)
type Context interface {
Next()
Context() *fasthttp.RequestCtx
Param(key string) string
Query(key string) string
SendBytes(value []byte) Context
SendString(value string) Context
SendJSON(in interface{}) error
Status(status int) Context
Set(key string, value string)
Get(key string) string
SetLocal(key string, value interface{})
GetLocal(key string) interface{}
Body() string
ParseBody(out interface{}) error
}
type handlerFunc func(ctx Context)
type handlersChain []handlerFunc
type context struct {
requestCtx *fasthttp.RequestCtx
paramValues map[string]string
handlers handlersChain
index int
}
func (ctx *context) Next() {
ctx.index++
if ctx.index < len(ctx.handlers) {
ctx.handlers[ctx.index](ctx)
}
}
func (ctx *context) Param(key string) string {
return ctx.paramValues[key]
}
func (ctx *context) Context() *fasthttp.RequestCtx {
return ctx.requestCtx
}
func (ctx *context) SendBytes(value []byte) Context {
ctx.requestCtx.Response.SetBodyRaw(value)
return ctx
}
func (ctx *context) SendString(value string) Context {
ctx.requestCtx.SetBodyString(value)
return ctx
}
func (ctx *context) SendJSON(in interface{}) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
raw, err := json.Marshal(in)
if err != nil {
return err
}
ctx.requestCtx.Response.Header.SetContentType(MimeApplicationJSON)
ctx.requestCtx.Response.SetBodyRaw(raw)
return nil
}
func (ctx *context) Status(status int) Context {
ctx.requestCtx.Response.SetStatusCode(status)
return ctx
}
func (ctx *context) Get(key string) string {
return GetString(ctx.requestCtx.Request.Header.Peek(key))
}
func (ctx *context) Set(key, value string) {
ctx.requestCtx.Response.Header.Set(key, value)
}
func (ctx *context) Query(key string) string {
return GetString(ctx.requestCtx.QueryArgs().Peek(key))
}
func (ctx *context) Body() string {
return GetString(ctx.requestCtx.Request.Body())
}
func (ctx *context) SetLocal(key string, value interface{}) {
ctx.requestCtx.SetUserValue(key, value)
}
func (ctx *context) GetLocal(key string) interface{} {
return ctx.requestCtx.UserValue(key)
}
func (ctx *context) ParseBody(out interface{}) error {
contentType := GetString(ctx.requestCtx.Request.Header.ContentType())
if strings.HasPrefix(contentType, MimeApplicationJSON) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(ctx.requestCtx.Request.Body(), out)
}
return fmt.Errorf("content type '%s' is not supported, "+
"please open a request to support it "+
"(https://github.com/godzillaframework//godzilla/issues",
contentType)
}