-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathship.go
executable file
·328 lines (275 loc) · 8.86 KB
/
ship.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
// Copyright 2020 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ship
import (
"bytes"
"net/http"
"net/url"
"os"
"sync"
"github.com/xgfone/ship/v5/router"
"github.com/xgfone/ship/v5/router/echo"
)
var noop = func(interface{}) error { return nil }
// DefaultShip is the default global ship.
var DefaultShip = Default()
// Router is the alias of router.Router.
type Router = router.Router
// Ship is an app to be used to manage the router.
type Ship struct {
// Name is the name of the ship.
//
// Default: ""
Name string
// Prefix is the default prefix of the paths of all the routes.
//
// Default: ""
Prefix string
// The initialization capacity of Context.Data.
//
// Default: 0
CtxDataInitCap int
// The maximum number of the url paramters of the route.
//
// Default: 4
URLParamMaxNum int
// The maximum number of the middlewares.
//
// Default: 256
MiddlewareMaxNum int
// Router is the route manager to manage all the routes.
//
// Default: echo.NewRouter(&echo.Config{RemoveTrailingSlash: true})
Router Router
// The default handler when not finding the route.
//
// Default: NotFoundHandler()
NotFound Handler
// Filter the route if returning true when registering and unregistering it.
//
// Default: nil
RouteFilter func(Route) bool
// Modify the route before registering and unregistering it.
//
// Default: nil
RouteModifier func(Route) Route
// HandleError is used to handle the error at last
// if the handler or middleware returns an error.
//
// Default: respond the error to the client if not responding.
HandleError func(c *Context, err error)
// Context Settings.
Session Session // Default: NewMemorySession()
Logger Logger // Default: NewLoggerFromWriter(os.Stderr, "")
Binder Binder // Default: nil
Renderer Renderer // Default: nil
Validator Validator // Default: nil
Defaulter Defaulter // Default: SetStructFieldToDefault
BindQuery func(dst interface{}, src url.Values) error // Default: BindURLValues(dst, src, "query")
Responder func(c *Context, args ...interface{}) error // Default: nil
mws []Middleware
pmws []Middleware
handler Handler
cpool sync.Pool
bpool sync.Pool
bsize int
}
// New returns a new Ship.
func New() *Ship {
s := &Ship{
Router: echo.NewRouter(&echo.Config{RemoveTrailingSlash: true}),
Logger: NewLoggerFromWriter(os.Stderr, ""),
Session: NewMemorySession(),
NotFound: NotFoundHandler(),
HandleError: handleErrorDefault,
Defaulter: DefaulterFunc(SetStructFieldToDefault),
BindQuery: bindQuery,
URLParamMaxNum: 4,
MiddlewareMaxNum: 256,
}
s.handler = s.handleRequest
s.cpool.New = func() interface{} { return s.NewContext() }
s.bpool.New = func() interface{} {
return bytes.NewBuffer(make([]byte, 0, s.bsize))
}
s.SetBufferSize(2048)
return s
}
// Default returns a new ship with MuxBinder and MuxRenderer
// as the binder and renderer.
func Default() *Ship {
mb := NewMuxBinder()
mb.Add(MIMEApplicationJSON, JSONBinder())
mb.Add(MIMETextXML, XMLBinder())
mb.Add(MIMEApplicationXML, XMLBinder())
mb.Add(MIMEMultipartForm, FormBinder(MaxMemoryLimit))
mb.Add(MIMEApplicationForm, FormBinder(MaxMemoryLimit))
s := New()
s.Binder = mb
s.Renderer = NewMuxRenderer()
return s
}
// GetName returns the name of the ship router.
func (s *Ship) GetName() string { return s.Name }
// GetLogger returns the logger of the ship router.
func (s *Ship) GetLogger() Logger { return s.Logger }
// Clone clones itself to a new one with the new name and the new router.
//
// If router is nil, create a new default one automatically.
func (s *Ship) Clone(name string, router Router) *Ship {
if router == nil {
router = echo.NewRouter(&echo.Config{RemoveTrailingSlash: true})
}
newShip := &Ship{
Name: name,
Router: router,
// Public
Prefix: s.Prefix,
NotFound: s.NotFound,
HandleError: s.HandleError,
RouteFilter: s.RouteFilter,
RouteModifier: s.RouteModifier,
CtxDataInitCap: s.CtxDataInitCap,
URLParamMaxNum: s.URLParamMaxNum,
MiddlewareMaxNum: s.MiddlewareMaxNum,
// Context
Binder: s.Binder,
Logger: s.Logger,
Session: s.Session,
Renderer: s.Renderer,
BindQuery: s.BindQuery,
Validator: s.Validator,
Responder: s.Responder,
Defaulter: s.Defaulter,
}
// Private
newShip.handler = newShip.handleRequest
newShip.cpool.New = func() interface{} { return newShip.NewContext() }
newShip.bpool.New = func() interface{} {
return bytes.NewBuffer(make([]byte, 0, newShip.bsize))
}
newShip.Use(s.mws...)
newShip.Pre(s.pmws...)
newShip.SetBufferSize(2048)
return newShip
}
// SetBufferSize resets the size of the buffer. The default is 2048.
func (s *Ship) SetBufferSize(size int) {
if size < 0 {
panic("the buffer size must not be a negative")
}
s.bsize = size
}
//----------------------------------------------------------------------------
// Context & Buffer
//----------------------------------------------------------------------------
// NewContext news a Context.
func (s *Ship) NewContext() *Context {
c := NewContext(s.URLParamMaxNum, s.CtxDataInitCap)
c.BufferAllocator = s
c.Logger = s.Logger
c.Router = s.Router
c.Session = s.Session
c.NotFound = s.NotFound
c.Binder = s.Binder
c.Renderer = s.Renderer
c.Responder = s.Responder
c.QueryBinder = s.BindQuery
if s.Defaulter == nil {
c.Defaulter = NothingDefaulter()
} else {
c.Defaulter = s.Defaulter
}
if s.Validator == nil {
c.Validator = NothingValidator()
} else {
c.Validator = s.Validator
}
return c
}
// AcquireContext gets a Context from the pool.
func (s *Ship) AcquireContext(r *http.Request, w http.ResponseWriter) *Context {
c := s.cpool.Get().(*Context)
c.req, c.res.ResponseWriter = r, w
return c
}
// ReleaseContext puts a Context into the pool.
func (s *Ship) ReleaseContext(c *Context) {
c.Reset()
s.cpool.Put(c)
}
// AcquireBuffer gets a Buffer from the pool.
func (s *Ship) AcquireBuffer() *bytes.Buffer {
return s.bpool.Get().(*bytes.Buffer)
}
// ReleaseBuffer puts a Buffer into the pool.
func (s *Ship) ReleaseBuffer(buf *bytes.Buffer) {
buf.Reset()
s.bpool.Put(buf)
}
//----------------------------------------------------------------------------
// Middleware
//----------------------------------------------------------------------------
// ResetMiddlewares resets the global middlewares to mdws.
func (s *Ship) ResetMiddlewares(middlewares ...Middleware) {
s.mws = append([]Middleware{}, middlewares...)
}
// ResetPreMiddlewares resets the global pre-middlewares to mdws.
func (s *Ship) ResetPreMiddlewares(middlewares ...Middleware) {
s.updatePreMiddlewares(append([]Middleware{}, middlewares...)...)
}
// Pre registers the pre-middlewares, which are executed before finding the route.
func (s *Ship) Pre(middlewares ...Middleware) {
s.updatePreMiddlewares(append(s.pmws, middlewares...)...)
}
func (s *Ship) updatePreMiddlewares(middlewares ...Middleware) {
s.pmws = middlewares
s.handler = s.handleRequest
for i := len(s.pmws) - 1; i >= 0; i-- {
s.handler = s.pmws[i](s.handler)
}
}
// Use registers the global middlewares, which must be registered
// before adding the routes using these middlewares.
func (s *Ship) Use(middlewares ...Middleware) {
s.mws = append(s.mws, middlewares...)
}
//----------------------------------------------------------------------------
// Handle Request
//----------------------------------------------------------------------------
func handleErrorDefault(ctx *Context, err error) {
if !ctx.res.Wrote {
if se, ok := err.(HTTPServerError); !ok {
ctx.NoContent(http.StatusInternalServerError)
} else if se.CT == "" {
ctx.BlobText(se.Code, MIMETextPlain, se.Error())
} else {
ctx.BlobText(se.Code, se.CT, se.Error())
}
}
}
// HandleRequest is the same as ServeHTTP, but handles the request
// with the Context.
func (s *Ship) HandleRequest(c *Context) error { return s.handler(c) }
func (s *Ship) handleRequest(c *Context) error { return c.Execute() }
// ServeHTTP implements the interface http.Handler.
func (s *Ship) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
c := s.AcquireContext(req, resp)
switch err := s.handler(c); err {
case nil, ErrSkip:
default:
s.HandleError(c, err)
}
s.ReleaseContext(c)
}