-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
109 lines (87 loc) · 3.16 KB
/
config.go
File metadata and controls
109 lines (87 loc) · 3.16 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
package emir
import (
fastrouter "github.com/fasthttp/router"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
)
func setDefaults(cfg Config) Config {
if cfg.Addr == "" {
cfg.Addr = DefaultAddress
}
if cfg.Network == "" {
cfg.Network = DefaultNetwork
}
if cfg.Name == "" {
cfg.Name = DefaultServerName
}
if cfg.NotFound == nil {
cfg.NotFound = DefaultNotFoundHandler
}
if cfg.MethodNotAllowed == nil {
cfg.MethodNotAllowed = DefaultMethodNotAllowed
}
if cfg.PanicHandler == nil {
cfg.PanicHandler = DefaultPanicHandler
}
if cfg.ErrorHandler == nil {
cfg.ErrorHandler = DefaultErrorHandler
}
if cfg.Network == "" {
cfg.Network = DefaultNetwork
}
if cfg.Logger == nil {
cfg.Logger = DefaultLogger()
}
if cfg.ReadTimeout <= 0 {
cfg.ReadTimeout = DefaultReadTimeout
}
return cfg
}
func newRouter(cfg Config) *fastrouter.Router {
router := fastrouter.New()
router.NotFound = ConvertToFastHTTPHandler(cfg.NotFound)
router.MethodNotAllowed = ConvertToFastHTTPHandler(cfg.MethodNotAllowed)
router.GlobalOPTIONS = ConvertToFastHTTPHandler(cfg.GlobalOPTIONS)
router.PanicHandler = func(ctx *fasthttp.RequestCtx, err interface{}) {
rctx := acquireCtx(ctx) //TODO: emir instancde
defer releaseCtx(rctx)
cfg.PanicHandler(rctx, err)
return
}
router.RedirectFixedPath = cfg.RedirectFixedPath
router.RedirectTrailingSlash = cfg.RedirectTrailingSlash
router.HandleMethodNotAllowed = cfg.HandleMethodNotAllowed
router.HandleOPTIONS = cfg.HandleOPTIONS
router.SaveMatchedRoutePath = cfg.SaveMatchedRoutePath
return router
}
func fasthttpServer(cfg Config) *fasthttp.Server {
return &fasthttp.Server{
Name: cfg.Name,
Concurrency: cfg.Concurrency,
DisableKeepalive: cfg.DisableKeepalive,
ReadBufferSize: cfg.ReadBufferSize,
WriteBufferSize: cfg.WriteBufferSize,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
IdleTimeout: cfg.IdleTimeout,
MaxConnsPerIP: cfg.MaxConnsPerIP,
MaxRequestsPerConn: cfg.MaxRequestsPerConn,
MaxKeepaliveDuration: cfg.MaxKeepaliveDuration,
TCPKeepalive: cfg.TCPKeepalive,
TCPKeepalivePeriod: cfg.TCPKeepalivePeriod,
MaxRequestBodySize: cfg.MaxRequestBodySize,
ReduceMemoryUsage: cfg.ReduceMemoryUsage,
GetOnly: cfg.GetOnly,
DisablePreParseMultipartForm: cfg.DisablePreParseMultipartForm,
LogAllErrors: cfg.LogAllErrors,
DisableHeaderNamesNormalizing: cfg.DisableHeaderNamesNormalizing,
SleepWhenConcurrencyLimitsExceeded: cfg.SleepWhenConcurrencyLimitsExceeded,
NoDefaultServerHeader: cfg.NoDefaultServerHeader,
NoDefaultDate: cfg.NoDefaultDate,
NoDefaultContentType: cfg.NoDefaultContentType,
ConnState: cfg.ConnState,
Logger: zap.NewStdLog(cfg.Logger),
KeepHijackedConns: cfg.KeepHijackedConns,
}
}