-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
184 lines (152 loc) · 5.53 KB
/
types.go
File metadata and controls
184 lines (152 loc) · 5.53 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
178
179
180
181
182
183
184
package emir
import (
"net"
"time"
stdUrl "net/url"
fastrouter "github.com/fasthttp/router"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
)
type (
// Emir is the top-level framework instance
Emir struct {
server *fasthttp.Server
fastrouter *fastrouter.Router
errorHandler ErrorHandler
hosts map[string]*virtualHost
cfg Config
Logger *zap.Logger
Router
}
// Router is the registry of all registered routes.
Router interface {
// Handle registers given request handlers with the given path and method
// There are shortcuts for some methods you can use them.
Handle(path, method string, handlers ...RequestHandler) *Route
// Use registers given middleware handlers to router
// Given handlers will be executed by given order
Use(handlers ...RequestHandler) Router
// After registers given handlers to router
After(handlers ...RequestHandler) Router
// GET is a shortcut for router.Handle(fasthttp.MethodGet, path, handlers)
GET(path string, handlers ...RequestHandler) *Route
// POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handlers)
POST(path string, handlers ...RequestHandler) *Route
// PUT is a shortcut for router.Handle(fasthttp.MethodPut, path, handlers)
PUT(path string, handlers ...RequestHandler) *Route
// PATCH is a shortcut for router.Handle(fasthttp.MethodPatch, path, handlers)
PATCH(path string, handlers ...RequestHandler) *Route
// DELETE is a shortcut for router.Handle(fasthttp.MethodDelete, path, handlers)
DELETE(path string, handlers ...RequestHandler) *Route
// HEAD is a shortcut for router.Handle(fasthttp.MethodHead, path, handlers)
HEAD(path string, handlers ...RequestHandler) *Route
// TRACE is a shortcut for router.Handle(fasthttp.MethodTrace, path, handlers)
TRACE(path string, handlers ...RequestHandler) *Route
// Handler gives routers request handler.
// Returns un-nil function only if the router is a virtual host router.
Handler() fasthttp.RequestHandler
// NewGroup creates a subrouter for given path.
NewGroup(path string) Router
// Validate registers given validator to the router
Validate(v Validator)
// Bind registers given binder to the router
Bind(b Binder)
// HandleError registers given error handler to the router
HandleError(handler ErrorHandler)
}
// Binder is the interface that wraps the Bind method.
Binder interface {
Bind(c *Context, v interface{}) error
}
// DefaultBinder is the default implementation of the Binder interface.
DefaultBinder struct {
}
// Config carries configuration for FasthHTTP server and Router
Config struct {
Network string
Addr string
Compress bool
TLS bool
CertFile string
CertKeyFile string
GracefulShutdown bool
ErrorHandler ErrorHandler
Logger *zap.Logger
Name string
Concurrency int
DisableKeepalive bool
ReadBufferSize int
WriteBufferSize int
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxConnsPerIP int
MaxRequestsPerConn int
MaxKeepaliveDuration time.Duration
TCPKeepalive bool
TCPKeepalivePeriod time.Duration
MaxRequestBodySize int
ReduceMemoryUsage bool
GetOnly bool
DisablePreParseMultipartForm bool
LogAllErrors bool
DisableHeaderNamesNormalizing bool
SleepWhenConcurrencyLimitsExceeded time.Duration
NoDefaultServerHeader bool
NoDefaultDate bool
NoDefaultContentType bool
ConnState func(net.Conn, fasthttp.ConnState)
KeepHijackedConns bool
//Router settings
SaveMatchedRoutePath bool
RedirectTrailingSlash bool
RedirectFixedPath bool
HandleMethodNotAllowed bool
HandleOPTIONS bool
GlobalOPTIONS RequestHandler
NotFound RequestHandler
MethodNotAllowed RequestHandler
PanicHandler func(*Context, interface{})
}
//Context context wrapper of fasthttp.RequestCtx to adds extra functionality
Context struct {
*fasthttp.RequestCtx
next bool
err bool
deferFuncs []func()
route *Route
emir *Emir
stdURL *stdUrl.URL
//TODO: response writer
}
// BasicError represents an error that ocured while handling a request
BasicError struct {
StatusCode int
ErrorMessage string `json:"message"`
ErrorCode interface{} `json:"code"`
}
// Validator is the interface that wraps the Validate method.
Validator interface {
Validate(i interface{}) error
}
// Route represents a route in router
// It carries route's path, method, handlers, middlewares and error handlers.
Route struct {
RouteName string
Path string
Method string
Middlewares []RequestHandler
Handlers []RequestHandler
AfterMiddlewares []RequestHandler
ErrorHandler ErrorHandler
Binder Binder
Validator Validator
}
ComplexRequestHandler interface {
Handle(*Context) error
}
// RequestHandler must process incoming requests
RequestHandler func(*Context) error
// ErrorHandler must process errror returned by RequestHandler.
ErrorHandler func(*Context, error)
)