-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
atreugo.go
242 lines (201 loc) · 6.63 KB
/
atreugo.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
package atreugo
import (
"fmt"
"io"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/fasthttp/router"
logger "github.com/savsgio/go-logger"
"github.com/valyala/fasthttp"
)
var allowedHTTPMethods = []string{"GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH", "DELETE"}
// New create a new instance of Atreugo Server
func New(cfg *Config) *Atreugo {
if cfg.Fasthttp == nil {
cfg.Fasthttp = new(FasthttpConfig)
}
if cfg.Fasthttp.Name == "" {
cfg.Fasthttp.Name = defaultServerName
}
if cfg.LogLevel == "" {
cfg.LogLevel = logger.INFO
}
if cfg.GracefulShutdown && cfg.Fasthttp.ReadTimeout <= 0 {
cfg.Fasthttp.ReadTimeout = defaultReadTimeout
}
if cfg.LogName == "" {
cfg.LogName = defaultLogName
}
r := router.New()
if cfg.NotFoundHandler != nil {
r.NotFound = cfg.NotFoundHandler
}
handler := r.Handler
if cfg.Compress {
handler = fasthttp.CompressHandler(handler)
}
log := logger.New(cfg.LogName, cfg.LogLevel, os.Stderr)
server := &Atreugo{
router: r,
server: &fasthttp.Server{
Handler: handler,
Name: cfg.Fasthttp.Name,
Concurrency: cfg.Fasthttp.Concurrency,
DisableKeepalive: cfg.Fasthttp.DisableKeepalive,
ReadBufferSize: cfg.Fasthttp.ReadBufferSize,
WriteBufferSize: cfg.Fasthttp.WriteBufferSize,
ReadTimeout: cfg.Fasthttp.ReadTimeout,
WriteTimeout: cfg.Fasthttp.WriteTimeout,
IdleTimeout: cfg.Fasthttp.IdleTimeout,
MaxConnsPerIP: cfg.Fasthttp.MaxConnsPerIP,
MaxRequestsPerConn: cfg.Fasthttp.MaxRequestsPerConn,
MaxKeepaliveDuration: cfg.Fasthttp.MaxKeepaliveDuration,
MaxRequestBodySize: cfg.Fasthttp.MaxRequestBodySize,
ReduceMemoryUsage: cfg.Fasthttp.ReduceMemoryUsage,
GetOnly: cfg.Fasthttp.GetOnly,
LogAllErrors: cfg.Fasthttp.LogAllErrors,
DisableHeaderNamesNormalizing: cfg.Fasthttp.DisableHeaderNamesNormalizing,
SleepWhenConcurrencyLimitsExceeded: cfg.Fasthttp.SleepWhenConcurrencyLimitsExceeded,
NoDefaultServerHeader: cfg.Fasthttp.NoDefaultServerHeader,
NoDefaultContentType: cfg.Fasthttp.NoDefaultContentType,
ConnState: cfg.Fasthttp.ConnState,
KeepHijackedConns: cfg.Fasthttp.KeepHijackedConns,
Logger: log,
},
log: log,
cfg: cfg,
}
return server
}
func (s *Atreugo) handler(viewFn View) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
actx := acquireRequestCtx(ctx)
if s.log.DebugEnabled() {
s.log.Debugf("%s %s", actx.Method(), actx.URI())
}
for _, middlewareFn := range s.middlewares {
if statusCode, err := middlewareFn(actx); err != nil {
s.log.Errorf("%s %s - %s", actx.Method(), actx.URI(), err)
actx.Error(err.Error(), statusCode)
releaseRequestCtx(actx)
return
}
}
if err := viewFn(actx); err != nil {
s.log.Error(err)
actx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
releaseRequestCtx(actx)
}
}
// Serve serves incoming connections from the given listener.
//
// Serve blocks until the given listener returns permanent error.
//
// If use a custom Listener, will be updated your atreugo configuration
// with the Listener address automatically
func (s *Atreugo) Serve(ln net.Listener) error {
schema := "http"
if s.cfg.TLSEnable {
schema = "https"
}
addr := ln.Addr().String()
if addr != s.lnAddr {
s.log.Info("Updating address config with the new listener address")
sAddr := strings.Split(addr, ":")
s.cfg.Host = sAddr[0]
if len(sAddr) > 1 {
s.cfg.Port, _ = strconv.Atoi(sAddr[1])
} else {
s.cfg.Port = 0
}
s.lnAddr = addr
}
s.log.Infof("Listening on: %s://%s/", schema, s.lnAddr)
if s.cfg.TLSEnable {
return s.server.ServeTLS(ln, s.cfg.CertFile, s.cfg.CertKey)
}
return s.server.Serve(ln)
}
// ServeGracefully serves incoming connections from the given listener with graceful shutdown
//
// ServeGracefully blocks until the given listener returns permanent error.
//
// If use a custom Listener, will be updated your atreugo configuration
// with the Listener address and setting GracefulShutdown to true automatically.
func (s *Atreugo) ServeGracefully(ln net.Listener) error {
if !s.cfg.GracefulShutdown {
s.log.Info("Updating GracefulShutdown config to 'true'")
s.cfg.GracefulShutdown = true
if s.server.ReadTimeout <= 0 {
s.log.Infof("Updating ReadTimeout config to '%v'", defaultReadTimeout)
s.server.ReadTimeout = defaultReadTimeout
s.cfg.Fasthttp.ReadTimeout = defaultReadTimeout
}
}
listenErr := make(chan error, 1)
go func() {
listenErr <- s.Serve(ln)
}()
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-listenErr:
return err
case <-osSignals:
s.log.Infof("Shutdown signal received")
if err := s.server.Shutdown(); err != nil {
return err
}
s.log.Infof("Server gracefully stopped")
}
return nil
}
// Static serves static files from the given file system root.
func (s *Atreugo) Static(url, rootPath string) {
if strings.HasSuffix(url, "/") {
url = url[:len(url)-1]
}
s.router.ServeFiles(url+"/*filepath", rootPath)
}
// ServeFile serves a file from the given system path
func (s *Atreugo) ServeFile(url, filePath string) {
s.Path("GET", url, func(ctx *RequestCtx) error {
fasthttp.ServeFile(ctx.RequestCtx, filePath)
return nil
})
}
// Path add the view to serve from the given path and method
func (s *Atreugo) Path(httpMethod string, url string, viewFn View) {
if !include(allowedHTTPMethods, httpMethod) {
panic("Invalid http method '" + httpMethod + "' for the url " + url)
}
s.router.Handle(httpMethod, url, s.handler(viewFn))
}
// UseMiddleware register middleware functions in the order you want to execute them
func (s *Atreugo) UseMiddleware(fns ...Middleware) {
s.middlewares = append(s.middlewares, fns...)
}
// SetLogOutput set log output of server
func (s *Atreugo) SetLogOutput(output io.Writer) {
s.log.SetOutput(output)
}
// ListenAndServe serves HTTP/HTTPS requests from the given TCP4 addr in the atreugo configuration.
//
// Pass custom listener to Serve/ServeGracefully if you need listening on non-TCP4 media
// such as IPv6.
func (s *Atreugo) ListenAndServe() error {
s.lnAddr = fmt.Sprintf("%s:%d", s.cfg.Host, s.cfg.Port)
ln, err := s.getListener()
if err != nil {
return err
}
if s.cfg.GracefulShutdown {
return s.ServeGracefully(ln)
}
return s.Serve(ln)
}