-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
godzilla.go
507 lines (411 loc) · 15 KB
/
godzilla.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
package godzilla
import (
"log"
"net"
"os"
"strings"
"sync"
"time"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/prefork"
)
const (
version = "2.1.0"
name = "Godzilla Framework"
banner = `
Server Running on %s`
)
const (
// defaultCacheSize is number of entries that can cache hold
defaultCacheSize = 1000
// defaultConcurrency is the maximum number of concurrent connections
defaultConcurrency = 256 * 1024
// defaultMaxRequestBodySize is the maximum request body size the server
defaultMaxRequestBodySize = 4 * 1024 * 1024
// defaultMaxRouteParams is the maximum number of routes params
defaultMaxRouteParams = 1024
// defaultMaxRequestURLLength is the maximum request url length
defaultMaxRequestURLLength = 2048
)
// HTTP methods were copied from net/http.
const (
MethodGet = "GET" // RFC 7231, 4.3.1
MethodHead = "HEAD" // RFC 7231, 4.3.2
MethodPost = "POST" // RFC 7231, 4.3.3
MethodPut = "PUT" // RFC 7231, 4.3.4
MethodPatch = "PATCH" // RFC 5789
MethodDelete = "DELETE" // RFC 7231, 4.3.5
MethodConnect = "CONNECT" // RFC 7231, 4.3.6
MethodOptions = "OPTIONS" // RFC 7231, 4.3.7
MethodTrace = "TRACE" // RFC 7231, 4.3.8
)
// HTTP status codes were copied from net/http.
const (
StatusContinue = 100 // RFC 7231, 6.2.1
StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
StatusProcessing = 102 // RFC 2518, 10.1
StatusOK = 200 // RFC 7231, 6.3.1
StatusCreated = 201 // RFC 7231, 6.3.2
StatusAccepted = 202 // RFC 7231, 6.3.3
StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4
StatusNoContent = 204 // RFC 7231, 6.3.5
StatusResetContent = 205 // RFC 7231, 6.3.6
StatusPartialContent = 206 // RFC 7233, 4.1
StatusMultiStatus = 207 // RFC 4918, 11.1
StatusAlreadyReported = 208 // RFC 5842, 7.1
StatusIMUsed = 226 // RFC 3229, 10.4.1
StatusMultipleChoices = 300 // RFC 7231, 6.4.1
StatusMovedPermanently = 301 // RFC 7231, 6.4.2
StatusFound = 302 // RFC 7231, 6.4.3
StatusSeeOther = 303 // RFC 7231, 6.4.4
StatusNotModified = 304 // RFC 7232, 4.1
StatusUseProxy = 305 // RFC 7231, 6.4.5
_ = 306 // RFC 7231, 6.4.6 (Unused)
StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
StatusPermanentRedirect = 308 // RFC 7538, 3
StatusBadRequest = 400 // RFC 7231, 6.5.1
StatusUnauthorized = 401 // RFC 7235, 3.1
StatusPaymentRequired = 402 // RFC 7231, 6.5.2
StatusForbidden = 403 // RFC 7231, 6.5.3
StatusNotFound = 404 // RFC 7231, 6.5.4
StatusMethodNotAllowed = 405 // RFC 7231, 6.5.5
StatusNotAcceptable = 406 // RFC 7231, 6.5.6
StatusProxyAuthRequired = 407 // RFC 7235, 3.2
StatusRequestTimeout = 408 // RFC 7231, 6.5.7
StatusConflict = 409 // RFC 7231, 6.5.8
StatusGone = 410 // RFC 7231, 6.5.9
StatusLengthRequired = 411 // RFC 7231, 6.5.10
StatusPreconditionFailed = 412 // RFC 7232, 4.2
StatusRequestEntityTooLarge = 413 // RFC 7231, 6.5.11
StatusRequestURITooLong = 414 // RFC 7231, 6.5.12
StatusUnsupportedMediaType = 415 // RFC 7231, 6.5.13
StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4
StatusExpectationFailed = 417 // RFC 7231, 6.5.14
StatusTeapot = 418 // RFC 7168, 2.3.3
StatusUnprocessableEntity = 422 // RFC 4918, 11.2
StatusLocked = 423 // RFC 4918, 11.3
StatusFailedDependency = 424 // RFC 4918, 11.4
StatusUpgradeRequired = 426 // RFC 7231, 6.5.15
StatusPreconditionRequired = 428 // RFC 6585, 3
StatusTooManyRequests = 429 // RFC 6585, 4
StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5
StatusUnavailableForLegalReasons = 451 // RFC 7725, 3
StatusInternalServerError = 500 // RFC 7231, 6.6.1
StatusNotImplemented = 501 // RFC 7231, 6.6.2
StatusBadGateway = 502 // RFC 7231, 6.6.3
StatusServiceUnavailable = 503 // RFC 7231, 6.6.4
StatusGatewayTimeout = 504 // RFC 7231, 6.6.5
StatusHTTPVersionNotSupported = 505 // RFC 7231, 6.6.6
StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1
StatusInsufficientStorage = 507 // RFC 4918, 11.5
StatusLoopDetected = 508 // RFC 5842, 7.2
StatusNotExtended = 510 // RFC 2774, 7
StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6
)
type Godzilla interface {
Start(address string) error
Stop() error
Get(path string, handlers ...handlerFunc) *Route
Head(path string, handlers ...handlerFunc) *Route
Post(path string, handlers ...handlerFunc) *Route
Put(path string, handlers ...handlerFunc) *Route
Patch(path string, handlers ...handlerFunc) *Route
Delete(path string, handlers ...handlerFunc) *Route
Connect(path string, handlers ...handlerFunc) *Route
Options(path string, handlers ...handlerFunc) *Route
Trace(path string, handlers ...handlerFunc) *Route
Group(prefix string, routes []*Route) []*Route
Static(prefix, root string)
NotFound(handlers ...handlerFunc)
Use(middlewares ...handlerFunc)
}
type godzilla struct {
httpServer *fasthttp.Server
router *router
registeredRoutes []*Route
address string // server address
middlewares handlersChain
settings *Settings
}
// Settings struct holds server settings
type Settings struct {
ReadBufferSize int
CaseInSensitive bool
CacheSize int
HandleMethodNotAllowed bool
HandleOPTIONS bool
AutoRecover bool // default false
// ServerName for sending in response headers
ServerName string // default ""
// Maximum request body size
MaxRequestBodySize int // default 4 * 1024 * 1024
// Maximum number of route params count
MaxRouteParams int // default 1024
// Max request url length
MaxRequestURLLength int // default 2048
// Maximum number of concurrent connections
Concurrency int // default 256 * 1024
// This will spawn multiple Go processes listening on the same port
// Default: false
Prefork bool
// LRU caching used to speed up routing
DisableCaching bool // default false
DisableStartupMessage bool // default false
// Disable keep-alive connections, the server will close incoming connections after sending the first response to client
DisableKeepalive bool // default false
// When set to true causes the default date header to be excluded from the response
DisableDefaultDate bool // default false
// When set to true, causes the default Content-Type header to be excluded from the Response
DisableDefaultContentType bool // default false
// By default all header names are normalized: conteNT-tYPE -> Content-Type
DisableHeaderNormalizing bool // default false
// The amount of time allowed to read the full request including body
ReadTimeout time.Duration // default unlimited
// The maximum duration before timing out writes of the response
WriteTimeout time.Duration // default unlimited
// The maximum amount of time to wait for the next request when keep-alive is enabled
IdleTimeout time.Duration // default unlimited
// Enable TLS or not
TLSEnabled bool // default false
// The path of the TLS certificate
TLSCertPath string // default ""
// The path of the TLS key
TLSKeyPath string // default ""
}
// Route struct which holds each route info
type Route struct {
Method string
Path string
Handlers handlersChain
}
func New(settings ...*Settings) Godzilla {
gz := new(godzilla)
gz.registeredRoutes = make([]*Route, 0)
if len(settings) > 0 {
gz.settings = settings[0]
} else {
gz.settings = &Settings{}
}
// set default settings for settings that don't have values set
if gz.settings.CacheSize <= 0 {
gz.settings.CacheSize = defaultCacheSize
}
if gz.settings.MaxRequestBodySize <= 0 {
gz.settings.MaxRequestBodySize = defaultMaxRequestBodySize
}
if gz.settings.MaxRouteParams <= 0 || gz.settings.MaxRouteParams > defaultMaxRouteParams {
gz.settings.MaxRouteParams = defaultMaxRouteParams
}
if gz.settings.MaxRequestURLLength <= 0 || gz.settings.MaxRequestURLLength > defaultMaxRequestURLLength {
gz.settings.MaxRequestURLLength = defaultMaxRequestURLLength
}
if gz.settings.Concurrency <= 0 {
gz.settings.Concurrency = defaultConcurrency
}
// Initialize router
gz.router = &router{
settings: gz.settings,
cache: make(map[string]*matchResult),
pool: sync.Pool{
New: func() interface{} {
return new(context)
},
},
}
gz.httpServer = gz.newHTTPServer()
return gz
}
// Start handling requests
func (gz *godzilla) Start(address string) error {
// Setup router
gz.setupRouter()
if gz.settings.Prefork {
if !gz.settings.DisableStartupMessage {
printStartupMessage(address)
}
pf := prefork.New(gz.httpServer)
pf.Reuseport = true
pf.Network = "tcp4"
if gz.settings.TLSEnabled {
return pf.ListenAndServeTLS(address, gz.settings.TLSCertPath, gz.settings.TLSKeyPath)
}
return pf.ListenAndServe(address)
}
ln, err := net.Listen("tcp4", address)
if err != nil {
return err
}
gz.address = address
if !gz.settings.DisableStartupMessage {
printStartupMessage(address)
}
if gz.settings.TLSEnabled {
return gz.httpServer.ServeTLS(ln, gz.settings.TLSCertPath, gz.settings.TLSKeyPath)
}
return gz.httpServer.Serve(ln)
}
// customLogger Customized logger used to filter logging messages
type customLogger struct{}
func (dl *customLogger) Printf(format string, args ...interface{}) {
}
// newHTTPServer returns a new instance of fasthttp server
func (gz *godzilla) newHTTPServer() *fasthttp.Server {
return &fasthttp.Server{
Handler: gz.router.Handler,
Logger: &customLogger{},
LogAllErrors: false,
Name: gz.settings.ServerName,
Concurrency: gz.settings.Concurrency,
NoDefaultDate: gz.settings.DisableDefaultDate,
NoDefaultContentType: gz.settings.DisableDefaultContentType,
DisableHeaderNamesNormalizing: gz.settings.DisableHeaderNormalizing,
DisableKeepalive: gz.settings.DisableKeepalive,
NoDefaultServerHeader: gz.settings.ServerName == "",
ReadTimeout: gz.settings.ReadTimeout,
WriteTimeout: gz.settings.WriteTimeout,
IdleTimeout: gz.settings.IdleTimeout,
ReadBufferSize: gz.settings.ReadBufferSize,
}
}
// registerRoute registers handlers with method and path
func (gz *godzilla) registerRoute(method, path string, handlers handlersChain) *Route {
if gz.settings.CaseInSensitive {
path = strings.ToLower(path)
}
route := &Route{
Path: path,
Method: method,
Handlers: handlers,
}
// Add route to registered routes
gz.registeredRoutes = append(gz.registeredRoutes, route)
return route
}
// setupRouter initializes router with registered routes
func (gz *godzilla) setupRouter() {
for _, route := range gz.registeredRoutes {
gz.router.handle(route.Method, route.Path, append(gz.middlewares, route.Handlers...))
}
// Frees intermediate stores after initializing router
gz.registeredRoutes = nil
gz.middlewares = nil
}
// Stop serving
func (gz *godzilla) Stop() error {
err := gz.httpServer.Shutdown()
// check if shutdown was ok and server had valid address
if err == nil && gz.address != "" {
log.Printf("%s stopped listening on %s", name, gz.address)
return nil
}
return err
}
// Get registers an http relevant method
func (gz *godzilla) Get(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodGet, path, handlers)
}
// Head registers an http relevant method
func (gz *godzilla) Head(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodHead, path, handlers)
}
// Post registers an http relevant method
func (gz *godzilla) Post(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodPost, path, handlers)
}
// Put registers an http relevant method
func (gz *godzilla) Put(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodPut, path, handlers)
}
// Patch registers an http relevant method
func (gz *godzilla) Patch(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodPatch, path, handlers)
}
// Delete registers an http relevant method
func (gz *godzilla) Delete(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodDelete, path, handlers)
}
// Connect registers an http relevant method
func (gz *godzilla) Connect(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodConnect, path, handlers)
}
// Options registers an http relevant method
func (gz *godzilla) Options(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodOptions, path, handlers)
}
// Trace registers an http relevant method
func (gz *godzilla) Trace(path string, handlers ...handlerFunc) *Route {
return gz.registerRoute(MethodTrace, path, handlers)
}
// Group appends a prefix to registered routes
func (gz *godzilla) Group(prefix string, routes []*Route) []*Route {
for _, route := range routes {
route.Path = prefix + route.Path
}
return routes
}
// Static serves files in root directory under specific prefix
func (gz *godzilla) Static(prefix, root string) {
if gz.settings.CaseInSensitive {
prefix = strings.ToLower(prefix)
}
// remove trailing slash
if len(root) > 1 && root[len(root)-1] == '/' {
root = root[:len(root)-1]
}
if len(prefix) > 1 && prefix[len(prefix)-1] == '/' {
prefix = prefix[:len(prefix)-1]
}
fs := &fasthttp.FS{
Root: root,
IndexNames: []string{"index.html"},
PathRewrite: func(ctx *fasthttp.RequestCtx) []byte {
path := ctx.Path()
if len(path) >= len(prefix) {
path = path[len(prefix):]
}
if len(path) > 0 && path[0] != '/' {
path = append([]byte("/"), path...)
} else if len(path) == 0 {
path = []byte("/")
}
return path
},
}
fileHandler := fs.NewRequestHandler()
handler := func(ctx Context) {
fctx := ctx.Context()
fileHandler(fctx)
status := fctx.Response.StatusCode()
if status != StatusNotFound && status != StatusForbidden {
return
}
// Pass to custom not found handlers if there are
if gz.router.notFound != nil {
gz.router.notFound[0](ctx)
return
}
// Default Not Found response
fctx.Error(fasthttp.StatusMessage(fasthttp.StatusNotFound),
fasthttp.StatusNotFound)
}
// TODO: Improve
gz.Get(prefix, handler)
if len(prefix) > 1 && prefix[len(prefix)-1] != '*' {
gz.Get(prefix+"/*", handler)
}
}
// NotFound registers an http handlers that will be called when no other routes
// match with request
func (gz *godzilla) NotFound(handlers ...handlerFunc) {
gz.router.SetNotFound(handlers)
}
func (gz *godzilla) Use(middlewares ...handlerFunc) {
gz.middlewares = append(gz.middlewares, middlewares...)
}
func printStartupMessage(addr string) {
if prefork.IsChild() {
log.Printf("Started child proc #%v\n", os.Getpid())
} else {
log.Println(banner, addr, version)
}
}