-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
178 lines (144 loc) · 5.02 KB
/
api.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
// Package don provides a fast and efficient API framework.
package don
import (
"bytes"
"net"
"net/http"
"github.com/abemedia/httprouter"
"github.com/valyala/fasthttp"
)
// DefaultEncoding contains the media type of the default encoding to fall back
// on if no `Accept` or `Content-Type` header was provided.
var DefaultEncoding = "text/plain"
type Middleware func(fasthttp.RequestHandler) fasthttp.RequestHandler
type Router interface {
Get(path string, handle httprouter.Handle)
Post(path string, handle httprouter.Handle)
Put(path string, handle httprouter.Handle)
Patch(path string, handle httprouter.Handle)
Delete(path string, handle httprouter.Handle)
Handle(method, path string, handle httprouter.Handle)
Handler(method, path string, handle http.Handler)
HandleFunc(method, path string, handle http.HandlerFunc)
Group(path string) Router
Use(mw ...Middleware)
}
type API struct {
NotFound fasthttp.RequestHandler
MethodNotAllowed fasthttp.RequestHandler
PanicHandler func(*fasthttp.RequestCtx, any)
router *httprouter.Router
config *Config
mw []Middleware
}
type Config struct {
// DefaultEncoding contains the mime type of the default encoding to fall
// back on if no `Accept` or `Content-Type` header was provided.
DefaultEncoding string
// DisableNoContent controls whether a nil or zero value response should
// automatically return 204 No Content with an empty body.
DisableNoContent bool
}
// New creates a new API instance.
func New(c *Config) *API {
if c == nil {
c = &Config{}
}
if c.DefaultEncoding == "" {
c.DefaultEncoding = DefaultEncoding
}
return &API{
router: httprouter.New(),
config: c,
NotFound: E(ErrNotFound),
MethodNotAllowed: E(ErrMethodNotAllowed),
}
}
// Get is a shortcut for router.Handle(http.MethodGet, path, handle).
func (r *API) Get(path string, handle httprouter.Handle) {
r.Handle(http.MethodGet, path, handle)
}
// Post is a shortcut for router.Handle(http.MethodPost, path, handle).
func (r *API) Post(path string, handle httprouter.Handle) {
r.Handle(http.MethodPost, path, handle)
}
// Put is a shortcut for router.Handle(http.MethodPut, path, handle).
func (r *API) Put(path string, handle httprouter.Handle) {
r.Handle(http.MethodPut, path, handle)
}
// Patch is a shortcut for router.Handle(http.MethodPatch, path, handle).
func (r *API) Patch(path string, handle httprouter.Handle) {
r.Handle(http.MethodPatch, path, handle)
}
// Delete is a shortcut for router.Handle(http.MethodDelete, path, handle).
func (r *API) Delete(path string, handle httprouter.Handle) {
r.Handle(http.MethodDelete, path, handle)
}
// Handle registers a new request handle with the given path and method.
func (r *API) Handle(method, path string, handle httprouter.Handle) {
r.router.Handle(method, path, handle)
}
// Handler is an adapter which allows the usage of an http.Handler as a request handle.
func (r *API) Handler(method, path string, handle http.Handler) {
r.router.Handler(method, path, handle)
}
// HandleFunc is an adapter which allows the usage of an http.HandlerFunc as a request handle.
func (r *API) HandleFunc(method, path string, handle http.HandlerFunc) {
r.router.HandlerFunc(method, path, handle)
}
// Group creates a new sub-router with a common prefix.
func (r *API) Group(path string) Router {
return &group{prefix: path, r: r}
}
// Use registers a middleware.
func (r *API) Use(mw ...Middleware) {
r.mw = append(r.mw, mw...)
}
// RequestHandler creates a fasthttp.RequestHandler for the API.
func (r *API) RequestHandler() fasthttp.RequestHandler {
r.router.NotFound = r.NotFound
r.router.MethodNotAllowed = r.MethodNotAllowed
r.router.PanicHandler = r.PanicHandler
h := r.router.HandleFastHTTP
for _, mw := range r.mw {
h = mw(h)
}
return func(ctx *fasthttp.RequestCtx) {
ct := ctx.Request.Header.ContentType()
if len(ct) == 0 || bytes.HasPrefix(ct, anyEncoding) {
ctx.Request.Header.SetContentType(r.config.DefaultEncoding)
}
ac := ctx.Request.Header.Peek(fasthttp.HeaderAccept)
if len(ac) == 0 || bytes.HasPrefix(ac, anyEncoding) {
ctx.Request.Header.Set(fasthttp.HeaderAccept, r.config.DefaultEncoding)
}
h(ctx)
// Content-Length of -3 means handler returned nil.
if ctx.Response.Header.ContentLength() == -3 {
ctx.Response.Header.Del(fasthttp.HeaderTransferEncoding)
if !r.config.DisableNoContent {
ctx.Response.SetBody(nil)
if ctx.Response.StatusCode() == fasthttp.StatusOK {
ctx.Response.SetStatusCode(fasthttp.StatusNoContent)
}
}
}
}
}
// ListenAndServe serves HTTP requests from the given TCP4 addr.
func (r *API) ListenAndServe(addr string) error {
return newServer(r).ListenAndServe(addr)
}
// Serve serves incoming connections from the given listener.
func (r *API) Serve(ln net.Listener) error {
return newServer(r).Serve(ln)
}
func newServer(r *API) *fasthttp.Server {
return &fasthttp.Server{
Handler: r.RequestHandler(),
StreamRequestBody: true,
NoDefaultContentType: true,
NoDefaultServerHeader: true,
}
}
var anyEncoding = []byte("*/*")