This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
309 lines (251 loc) · 8.08 KB
/
server.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
package server
import (
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/giantswarm/request-context"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/juju/errgo"
)
const (
DefaultCloseListenerDelay = 0
DefaultOsExitDelay = 5
DefaultOsExitCode = 0
RequestIDKey = "request-id"
RequestIDHeader = "X-Request-ID"
)
type CtxConstructor func() interface{}
// Middleware is a http handler method.
type Middleware func(res http.ResponseWriter, req *http.Request, ctx *Context) error
// Context is a map getting through all middlewares.
type Context struct {
// Contains all placeholders from the route.
MuxVars map[string]string
// Helper to quickly write results to the `http.ResponseWriter`.
Response Response
// A middleware should call Next() to signal that no problem was encountered
// and the next middleware in the chain can be executed after this middleware
// finished. Always returns `nil`, so it can be convieniently used with
// return to quit the middleware.
Next func() error
// The app context for this request. Gets prefilled by the
// CtxConstructor, if set in the server.
App interface{}
Request requestcontext.Ctx
}
// RequestID returns ID for the current request.
// If none is found, an empty string is returned.
func (c *Context) RequestID() string {
idRaw := c.Request[RequestIDKey]
if id, ok := idRaw.(string); ok {
return id
} else {
// ID not found in map
return ""
}
}
// SetRequestID overwrites the request ID of the current request
// with the given ID.
func (c *Context) SetRequestID(ID string) {
c.Request[RequestIDKey] = ID
}
type Server struct {
// The address to listen on.
addr string
logLevel string
logColor bool
Logger requestcontext.Logger
listener net.Listener
extendAccessLogging bool
preHTTPHandler AccessReporter
postHTTPHandler AccessReporter
alreadyRegisteredRoutes bool
Router *mux.Router
ctxConstructor CtxConstructor
signalCounter uint32
closeListenerDelay time.Duration
osExitDelay time.Duration
osExitCode int
IDFactory func() string
}
func NewServer(host, port string) *Server {
// We want to apply route names and need the context to be kept.
router := mux.NewRouter()
router.KeepContext = true
s := &Server{
addr: host + ":" + port,
Router: router,
IDFactory: NewIDFactory(),
logColor: true,
}
s.SetLogger(requestcontext.MustGetLogger(requestcontext.LoggerConfig{Name: "server", Color: s.logColor}))
s.SetCloseListenerDelay(DefaultCloseListenerDelay)
s.SetOsExitDelay(DefaultOsExitDelay)
s.SetOsExitCode(DefaultOsExitCode)
return s
}
func (s *Server) Serve(method, urlPath string, middlewares ...Middleware) {
if len(middlewares) == 0 {
panic("Missing at least one Middleware-Handler.")
}
handler := s.NewMiddlewareHandler(middlewares)
s.Router.Methods(method).Path(urlPath).Handler(handler).Name(method + " " + urlPath)
}
// ServeStatis registers a middleware that serves files from the filesystem.
// Example: s.ServeStatic("/v1/public", "./public_html/v1/")
func (s *Server) ServeStatic(urlPath, fsPath string) {
handler := http.StripPrefix(urlPath, http.FileServer(http.Dir(fsPath)))
s.Router.Methods("GET").PathPrefix(urlPath).Handler(handler)
}
func (s *Server) ServeNotFound(middlewares ...Middleware) {
if len(middlewares) == 0 {
panic("Missing at least one NotFound-Handler. Aborting...")
}
s.Router.NotFoundHandler = s.NewMiddlewareHandler(middlewares)
}
// ExtendAccessLogging turns on the usage of ExtendedAccessLogger
func (s *Server) ExtendAccessLogging() {
s.extendAccessLogging = true
}
func (s *Server) RegisterRoutes(mux *http.ServeMux, prefix string) {
if s.alreadyRegisteredRoutes {
return
}
var handler http.Handler = s.Router
// Always cleanup gorilla context request variables
handler = context.ClearHandler(handler)
// http.mux handlers need a trailing slash while gorilla's mux does not need one
// because they have different matching algorithms.
prefix = strings.TrimSuffix(prefix, "/")
mux.Handle(prefix+"/", http.StripPrefix(prefix, handler))
s.alreadyRegisteredRoutes = true
}
func (s *Server) Listen() {
mux := http.NewServeMux()
s.RegisterRoutes(mux, "/")
var err error
if s.listener, err = net.Listen("tcp", s.addr); err != nil {
panic(err)
}
go func() {
if err := http.Serve(s.listener, mux); err != nil {
if _, ok := err.(*net.OpError); ok {
// We ignore the error "use of closed network connection", because it is
// caused by us when shutting down the server.
} else {
s.Logger.Error(nil, "%#v", errgo.Mask(err))
}
}
}()
s.listenSignals()
}
func (s *Server) listenSignals() {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
// Block until a signal is received.
for {
select {
case sig := <-c:
s.Logger.Info(nil, "server received signal %s", sig)
go s.Close()
}
}
}
// Closing returns true when the server is shutting down, false otherwise.
func (s *Server) Closing() bool {
return s.signalCounter > 0
}
func (s *Server) Close() {
// Interrupt the process when closing is requested twice.
if atomic.AddUint32(&s.signalCounter, 1) >= 2 {
s.ExitProcess()
}
s.Logger.Info(nil, "closing tcp listener in %s", s.closeListenerDelay.String())
time.Sleep(s.closeListenerDelay)
s.listener.Close()
s.Logger.Info(nil, "shutting down server in %s", s.osExitDelay.String())
time.Sleep(s.osExitDelay)
s.ExitProcess()
}
func (s *Server) ExitProcess() {
s.Logger.Info(nil, "shutting down server with exit code %d", s.osExitCode)
os.Exit(s.osExitCode)
}
// NewMiddlewareHandler wraps the middlewares in a http.Handler. The handler,
// on activation, calls each middleware in order, if no error was returned and
// `ctx.Next()` was called. If a middleware wants to finish the processing, it
// can just write to the `http.ResponseWriter` or use the `ctx.Response` for
// convienience.
func (s *Server) NewMiddlewareHandler(middlewares []Middleware) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
// prepare request
requestID := req.Header.Get(RequestIDHeader)
// TODO: This is just for backward compatibility. Currently clients are
// sending both, client and request ID's. We just changed our concept and
// pushed client changes too fast, so we need to support them for a while
// to not be confused by our received data.
clientID := req.Header.Get("X-Client-ID")
if clientID != "" && requestID != "" {
requestID = clientID
}
if requestID == "" {
requestID = s.IDFactory()
} else {
requestID += ", " + s.IDFactory()
}
requestCtx := requestcontext.Ctx{
RequestIDKey: requestID,
}
// create handler that actually processes the middlewares
middlewareHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
ctx := &Context{
MuxVars: mux.Vars(req),
Request: requestCtx,
Response: Response{
w: res,
},
}
if s.ctxConstructor != nil {
ctx.App = s.ctxConstructor()
}
for _, middleware := range middlewares {
nextCalled := false
ctx.Next = func() error {
nextCalled = true
return nil
}
// End the request with an error and stop calling further middlewares.
if err := middleware(res, req, ctx); err != nil {
s.Logger.Error(requestCtx, "%s %s %#v", req.Method, req.URL, errgo.Mask(err))
ctx.Response.Error(err.Error(), http.StatusInternalServerError)
break
}
if !nextCalled {
break
}
}
})
// do access-logging by wrapping the middleware handler
reporter := DefaultAccessReporter(requestCtx, s.Logger)
if s.extendAccessLogging {
reporter = ExtendedAccessReporter(requestCtx, s.Logger)
}
handler := NewLogAccessHandler(
reporter,
s.preHTTPHandler,
s.postHTTPHandler,
middlewareHandler,
).(http.HandlerFunc)
// handle request
handler.ServeHTTP(res, req)
})
}