-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.go
253 lines (217 loc) · 7.52 KB
/
httpserver.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
package httpserver
import (
"crypto/tls"
"io"
"log"
"net/http"
"os"
"runtime/debug"
"time"
_uuid "github.com/google/uuid"
_router "github.com/julienschmidt/httprouter"
_cors "github.com/rs/cors"
)
type Server struct {
handlers *_router.Router
errChan chan error
port uint16
idleTimeout time.Duration
logger *log.Logger
logWriter io.Writer
tls *tls.Config
cors *_cors.Cors
middlewares []Middleware
panicHandler PanicHandler
notFoundHandler http.Handler
}
type Middleware func(next http.HandlerFunc, params ...interface{}) http.HandlerFunc
type PanicHandler func(w http.ResponseWriter, r *http.Request, rcv ...interface{})
type Opts struct {
Port uint16
// EnableLogger enable logging for incoming requests
EnableLogger bool
// Logger logger file
LogWriter io.Writer
// IdleTimeout keep-alive timeout while waiting for the next request coming. If empty then no timeout.
IdleTimeout time.Duration
// TLS to enable HTTPS
TLS *tls.Config
// Cors optional, can be nil, if nil then default will be set.
Cors *Cors
// PanicHandler triggered if panic happened.
// rcv: first param is argument retrieved from `recover()` function.
PanicHandler PanicHandler
// NotFoundHandler triggered if path not found.
// If empty then default is used.
NotFoundHandler http.HandlerFunc
}
// Cors corst options
type Cors struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
ExposedHeaders []string
MaxAge int
AllowCredentials bool
IsDebug bool
}
func New(opts *Opts) *Server {
h := _router.New()
var cors *_cors.Cors
if opts.Cors != nil {
cors = _cors.New(_cors.Options{
AllowedOrigins: opts.Cors.AllowedOrigins,
AllowedMethods: opts.Cors.AllowedMethods,
AllowedHeaders: opts.Cors.AllowedHeaders,
ExposedHeaders: opts.Cors.ExposedHeaders,
MaxAge: opts.Cors.MaxAge,
AllowCredentials: opts.Cors.AllowCredentials,
OptionsPassthrough: true,
Debug: opts.Cors.IsDebug,
})
}
var notFoundHandler http.Handler
if opts.NotFoundHandler != nil {
notFoundHandler = ¬Found{opts.NotFoundHandler}
}
srv := &Server{
handlers: h,
port: opts.Port,
idleTimeout: opts.IdleTimeout,
logger: log.New(os.Stderr, "", 0),
logWriter: os.Stderr,
middlewares: make([]Middleware, 0),
tls: opts.TLS,
cors: cors,
errChan: make(chan error),
panicHandler: opts.PanicHandler,
notFoundHandler: notFoundHandler,
}
if opts.LogWriter != nil {
srv.logWriter = opts.LogWriter
}
if opts.EnableLogger {
buff := make(buffer, 10<<20)
go write(buff, srv.logWriter)
srv.logger = log.New(buff, "", 0)
srv.middlewares = append(srv.middlewares, srv.log)
}
return srv
}
// Run the server. Blocking.
func (s *Server) Run() {
s.logger.Printf("%s | httpserver | server is starting...", time.Now().Format(time.RFC3339))
s.logger.Printf("%s | httpserver | server is running on port %d", time.Now().Format(time.RFC3339), s.port)
if err := s.serve(); err != nil {
s.logger.Printf("%s | httpserver | server failed with error: %v", time.Now().Format(time.RFC3339), err)
s.errChan <- err
}
}
func (s *Server) ListenError() <-chan error {
return s.errChan
}
type notFound struct {
handler http.HandlerFunc
}
func (n *notFound) ServeHTTP(w http.ResponseWriter, r *http.Request) {
n.handler(w, r)
}
// TLSConfig generate certificate config using provided certificate and private key.
// It will overwrite the one set in Opts.
func (s *Server) TLSConfig(cert, key string) error {
certificate, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return err
}
s.tls = &tls.Config{
Certificates: []tls.Certificate{certificate},
}
return nil
}
type responseWriter struct {
http.ResponseWriter
statusCode int
requestID string
xRequestID string
}
func (rw *responseWriter) WriteHeader(statusCode int) {
rw.statusCode = statusCode
rw.ResponseWriter.WriteHeader(statusCode)
}
func newResponseWriter(w http.ResponseWriter, reqID string, xReqID string) *responseWriter {
// default if not set is 200
return &responseWriter{w, http.StatusOK, reqID, xReqID}
}
func f(next http.HandlerFunc) _router.Handle {
return func(w http.ResponseWriter, r *http.Request, ps _router.Params) {
if r.Header.Get("Request-Id") == "" && r.Header.Get("X-Request-Id") == "" {
r.Header.Set("Request-Id", _uuid.New().String())
}
if r.Header.Get("Request-Id") == "" && r.Header.Get("X-Request-Id") != "" {
r.Header.Set("Request-Id", r.Header.Get("X-Request-Id"))
}
if len(ps) > 0 {
urlValues := r.URL.Query()
for i := range ps {
urlValues.Add(ps[i].Key, ps[i].Value)
}
r.URL.RawQuery = urlValues.Encode()
}
rw := newResponseWriter(w, r.Header.Get("Request-Id"), r.Header.Get("X-Request-Id"))
next(rw, r)
}
}
func (s *Server) recoverPanic(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rcv := recover(); rcv != nil {
if s.panicHandler != nil {
s.panicHandler(w, r, rcv)
} else {
ResponseString(w, http.StatusInternalServerError, "httpserver got panic")
}
s.logger.Printf("%s | httpserver | %s | %s | %s | %s\n", time.Now().Format(time.RFC3339), "PANIC", r.Method, r.URL.Path, r.Header.Get("Request-Id"))
s.logger.Printf("☠️ ☠️ ☠️ ☠️ ☠️ ☠️ PANIC START (%s) ☠️ ☠️ ☠️ ☠️ ☠️ ☠️", r.Header.Get("Request-Id"))
debug.PrintStack()
s.logger.Printf("☠️ ☠️ ☠️ ☠️ ☠️ ☠️ PANIC END (%s) ☠️ ☠️ ☠️ ☠️ ☠️ ☠️", r.Header.Get("Request-Id"))
return
}
}()
next(w, r)
}
}
func (s *Server) GET(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.GET(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
func (s *Server) HEAD(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.HEAD(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
func (s *Server) POST(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.POST(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
func (s *Server) PUT(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.PUT(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
func (s *Server) DELETE(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.DELETE(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
func (s *Server) PATCH(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.PATCH(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
func (s *Server) OPTIONS(path string, handler http.HandlerFunc, middlewares ...Middleware) {
s.handlers.OPTIONS(path, f(s.recoverPanic(s.chainMiddlewares(handler, middlewares...))))
}
// FILES serve files from 1 directory dynamically.
// @filePath: must end with '/*filepath' as placeholder for filename to be accessed.
// @rootPath: root directory where @filepath locate.
func (s *Server) FILES(filePath string, rootPath string, middlewares ...Middleware) {
if len(filePath) < 10 || filePath[len(filePath)-10:] != "/*filepath" {
panic("path must end with /*filepath in path '" + filePath + "'")
}
rootDir := http.Dir(rootPath)
fileServer := http.FileServer(rootDir)
s.GET(filePath, func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = r.URL.Query().Get("filepath")
fileServer.ServeHTTP(w, r)
}, middlewares...)
}