/
app.go
619 lines (533 loc) · 16.8 KB
/
app.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package cidre
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"runtime"
"runtime/debug"
"strings"
"sync/atomic"
"text/template"
"time"
)
/* Context {{{ */
// Context is a per-request context object. It allows us to share variables between middlewares.
type Context struct {
Dict
App *App
Session *Session
Id string
Route *Route
PathParams *url.Values
StartedAt time.Time
ResponseTime time.Duration
MiddlewareChain *MiddlewareChain
}
type contextBody struct {
io.ReadCloser
Context *Context
}
// Returns a new Context object.
func NewContext(app *App, id string, r *http.Request) *Context {
tmp := r.Body
context := &Context{
Dict: NewDict(),
App: app,
Id: id,
PathParams: &url.Values{},
}
r.Body = &contextBody{tmp, context}
return context
}
// Returns true if the matched route is dynamic, false if there is no matched
// routes or the matched route is for static files.
func (ctx *Context) IsDynamicRoute() bool {
return ctx.Route != nil && !ctx.Route.IsStatic
}
// Returns a contenxt object associated with the given request.
func RequestContext(r *http.Request) *Context {
return r.Body.(*contextBody).Context
}
/* }}} */
/* Hooks {{{ */
// Hooks is a container of Hook objects.
type Hooks map[string][]Hook
// Hook is a mechanism for customization of cidre.
// Hook is a function, to be called on some well-defined occasion.
type Hook func(http.ResponseWriter, *http.Request, interface{})
// HookDirection represents execution order of Hooks.
type HookDirection int
const (
// from front to back
HookDirectionNormal HookDirection = iota
// from back to front
HookDirectionReverse
)
// Executes hooks associated with the given name.
func (hooks Hooks) Run(name string, direction HookDirection, w http.ResponseWriter, r *http.Request, data interface{}) {
if direction == HookDirectionNormal {
for _, hook := range hooks[name] {
hook(w, r, data)
}
} else {
s := hooks[name]
for i := len(s) - 1; i >= 0; i-- {
s[i](w, r, data)
}
}
}
// Registers a hook to be executed at the given hook point.
func (hooks Hooks) Add(name string, hook Hook) {
_, ok := hooks[name]
if !ok {
hooks[name] = make([]Hook, 0, 10)
}
hooks[name] = append(hooks[name], hook)
}
/* }}} */
/* ResponseWriter {{{ */
// ResponseWriter is a wrapper around http.ResponseWriter that provides extra methods about the response.
//
// Hook points:
// - before_write_header(self, nil, status int)
// - after_write_header(self, nil, status int)
// - before_write_content(self, nil, content []byte)
type ResponseWriter interface {
http.ResponseWriter
SetHeader(int)
ContentLength() int
Status() int
Hooks() Hooks
}
type responseWriter struct {
http.ResponseWriter
status int
contentLength int
hooks Hooks
headerWritten bool
}
// Returns a new ResponseWriter object wrap around the given http.ResponseWriter object.
func NewResponseWriter(w http.ResponseWriter) ResponseWriter {
self := &responseWriter{w, 0, 0, make(Hooks), false}
return self
}
func (w *responseWriter) Hooks() Hooks {
return w.hooks
}
func (w *responseWriter) SetHeader(status int) {
w.status = status
}
func (w *responseWriter) WriteHeader(status int) {
if w.headerWritten {
return
}
w.Hooks().Run("before_write_header", HookDirectionReverse, w, nil, status)
w.status = status
w.headerWritten = true
w.ResponseWriter.WriteHeader(status)
w.Hooks().Run("after_write_header", HookDirectionReverse, w, nil, status)
}
func (w *responseWriter) Write(b []byte) (int, error) {
if !w.headerWritten {
if w.status == 0 {
w.status = 200
}
w.WriteHeader(w.status)
}
if w.ContentLength() == 0 {
w.Hooks().Run("before_write_content", HookDirectionReverse, w, nil, b)
}
i, err := w.ResponseWriter.Write(b)
if err == nil {
w.contentLength += len(b)
}
return i, err
}
func (w *responseWriter) ContentLength() int {
return w.contentLength
}
func (w *responseWriter) Status() int {
return w.status
}
/* }}} */
/* Middleware {{{ */
// Middleware is an ailias for the http.Handler interface.
// In ServeHTTP, you should yield to the next middleware in the chain.
type Middleware http.Handler
// MiddlewareChain represents an invocation chain of a middleware.
// Middlewares use the MiddlewareChain to invoke the next middleware in the chain,
// or if the calling middleware is the last middleware in the chain,
// to invoke the handler at the end of the chain.
type MiddlewareChain struct {
middlewares []Middleware
sp int
}
// Returns a new MiddlewareChain object.
func NewMiddlewareChain(middlewares []Middleware) *MiddlewareChain {
return &MiddlewareChain{middlewares, -1}
}
// Returns a copy of the MiddlewareChain object.
func (mc *MiddlewareChain) Copy() *MiddlewareChain {
return NewMiddlewareChain(mc.middlewares)
}
// Causes the next middleware in the chain to be invoked, or if the calling middleware is
// the last middleware in the chain, causes the handler at the end of the chain to be invoked.
func (mc *MiddlewareChain) DoNext(w http.ResponseWriter, r *http.Request) {
mc.sp += 1
mc.middlewares[mc.sp].ServeHTTP(w, r)
}
func MiddlewareOf(arg interface{}) Middleware {
switch arg.(type) {
case http.Handler:
return arg.(Middleware)
default:
return Middleware(http.HandlerFunc(arg.(func(http.ResponseWriter, *http.Request))))
}
}
func MiddlewaresOf(args ...interface{}) []Middleware {
result := make([]Middleware, 0, len(args))
for _, arg := range args {
result = append(result, MiddlewareOf(arg))
}
return result
}
/* }}} */
/* Logger {{{ */
type Logger func(LogLevel, string)
type LogLevel int
const (
LogLevelUnknown LogLevel = iota
LogLevelDebug
LogLevelInfo
LogLevelWarn
LogLevelError
LogLevelCrit
)
var logLevelStrings = map[LogLevel]string{
LogLevelDebug: "DEBUG", LogLevelInfo: "INFO",
LogLevelWarn: "WARN", LogLevelError: "ERROR", LogLevelCrit: "CRIT",
}
func (ll LogLevel) String() string {
if v, ok := logLevelStrings[ll]; !ok {
return "UNKNOWN"
} else {
return v
}
}
func DefaultLogger(level LogLevel, message string) {
fmt.Fprintln(os.Stdout, BuildString(256, time.Now().Format(time.RFC3339), "\t", level.String(), "\t", message))
}
/* }}} */
/* Route {{{ */
// Route represents a Route in cidre. Route implements the Middleware interface.
type Route struct {
Name string
PathParamNames []string
Method string
Pattern *regexp.Regexp
PatternString string
IsStatic bool
MiddlewareChain *MiddlewareChain
Meta Dict
}
var NopMiddleware = Middleware(MiddlewareOf(func(w http.ResponseWriter, r *http.Request) {}))
func NewRoute(n, p, m string, s bool, handler http.Handler, middlewares ...Middleware) *Route {
self := &Route{
Name: n,
Pattern: regexp.MustCompile("^" + p + "$"),
PatternString: p,
Method: m,
IsStatic: s,
Meta: make(Dict),
}
reg := regexp.MustCompile("\\?P<([^<]+)>")
for _, lst := range reg.FindAllStringSubmatch(p, -1) {
self.PathParamNames = append(self.PathParamNames, lst[1])
}
mds := make([]Middleware, 0, 20)
mds = append(mds, middlewares...)
mds = append(mds, Middleware(handler), NopMiddleware)
self.MiddlewareChain = NewMiddlewareChain(mds)
return self
}
func (route *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := RequestContext(r)
ctx.MiddlewareChain = route.MiddlewareChain.Copy()
ctx.MiddlewareChain.DoNext(w, r)
}
/* }}} */
/* MountPoint {{{ */
// MountPoint represents a group of routes that has same URL prefix and
// a set of middlewares.
type MountPoint struct {
App *App
Path string
Middlewares []Middleware
}
// Adds a middleware to the end of the middleware chain.
func (mt *MountPoint) Use(middlewares ...interface{}) {
mt.Middlewares = append(mt.Middlewares, MiddlewaresOf(middlewares...)...)
}
// Registers a http.HandlerFunc and middlewares with the given path pattern and method.
func (mt *MountPoint) Route(n, p, m string, s bool, h http.HandlerFunc, middlewares ...interface{}) *Route {
mds := make([]Middleware, 0, 10)
mds = append(mds, mt.Middlewares...)
mds = append(mds, MiddlewaresOf(middlewares...)...)
route := NewRoute(n, mt.Path+p, m, s, http.HandlerFunc(h), mds...)
mt.App.Routes[n] = route
return route
}
// Shortcut for Route(name, pattern, "GET", false, handler, ...Middleware)
func (mt *MountPoint) Get(n, p string, h http.HandlerFunc, middlewares ...interface{}) *Route {
return mt.Route(n, p, "GET", false, h, middlewares...)
}
// Shortcut for Route(name, pattern, "POST", false, handler, ...Middleware)
func (mt *MountPoint) Post(n, p string, h http.HandlerFunc, middlewares ...interface{}) *Route {
return mt.Route(n, p, "POST", false, h, middlewares...)
}
// Shortcut for Route(name, pattern, "Put", false, handler, ...Middleware)
func (mt *MountPoint) Put(n, p string, h http.HandlerFunc, middlewares ...interface{}) *Route {
return mt.Route(n, p, "PUT", false, h, middlewares...)
}
// Shortcut for Route(name, pattern, "DELETE", false, handler, ...Middleware)
func (mt *MountPoint) Delete(n, p string, h http.HandlerFunc, middlewares ...interface{}) *Route {
return mt.Route(n, p, "DELETE", false, h, middlewares...)
}
// Registers a handler that serves static files.
func (mt *MountPoint) Static(n, p, local string, middlewares ...interface{}) *Route {
path := strings.Trim(p, "/")
server := http.StripPrefix(mt.Path+path, http.FileServer(http.Dir(local)))
rt := mt.Route(n, path+"/(?P<path>.*)", "GET", true, server.ServeHTTP, middlewares...)
rt.Meta.Set("local", local)
return rt
}
/* }}} */
/* App {{{ */
// AppConfig is a configuration object for the App struct.
type AppConfig struct {
// default : false
Debug bool
// Server address, default:"127.0.0.1:8080"
Addr string
// default: ""
TemplateDirectory string
// default: true, if this value is true, cidre will treat a "_method" parameter as a HTTP method name.
AllowHttpMethodOverwrite bool
// cidre uses text/template to format access logs.
// default: "{{.c.Id}} {{.req.RemoteAddr}} {{.req.Method}} {{.req.RequestURI}} {{.req.Proto}} {{.res.Status}} {{.res.ContentLength}} {{.c.ResponseTime}}"
AccessLogFormat string
// default: 180s
ReadTimeout time.Duration
// default: 180s
WriteTimeout time.Duration
// default: 8192
MaxHeaderBytes int
// default: false
KeepAlive bool
// calls runtime.GOMAXPROCS(runtime.NumCPU()) when server starts if AutoMaxProcs is true.
// default: true
AutoMaxProcs bool
}
// Returns a new AppConfig object that has default values set.
// If an 'init' function object argument is not nil, this function
// will call the function with the AppConfig object.
func DefaultAppConfig(init ...func(*AppConfig)) *AppConfig {
self := &AppConfig{
Debug: false,
Addr: "127.0.0.1:8080",
TemplateDirectory: "",
AllowHttpMethodOverwrite: true,
AccessLogFormat: "{{.c.Id}} {{.req.RemoteAddr}} {{.req.Method}} {{.req.RequestURI}} {{.req.Proto}} {{.res.Status}} {{.res.ContentLength}} {{.c.ResponseTime}}",
ReadTimeout: time.Second * 180,
WriteTimeout: time.Second * 180,
MaxHeaderBytes: 8192,
KeepAlive: false,
AutoMaxProcs: true,
}
if len(init) > 0 {
init[0](self)
}
return self
}
// App represents a web application.
// Hooks:
// - setup(nil, nil, self)
// - start_server(nil, nil, self)
// - start_request(http.ResponseWriter, *http.Request, nil)
// - start_action(http.ResponseWriter, *http.Request, nil)
// - end_action(http.ResponseWriter, *http.Request, nil)
// - end_request(http.ResponseWriter, *http.Request, nil)
type App struct {
Config *AppConfig
Routes map[string]*Route
Middlewares []Middleware
Logger Logger
AccessLogger Logger
// handlers to be called if errors was occurred during a request.
OnPanic func(http.ResponseWriter, *http.Request, interface{})
// handlers to be called if no suitable routes found.
OnNotFound func(http.ResponseWriter, *http.Request)
Renderer Renderer
Hooks Hooks
contextIdSeq uint32
accessLogTemplate *template.Template
}
// Returns a new App object.
func NewApp(config *AppConfig) *App {
self := &App{
Config: config,
Routes: make(map[string]*Route),
Middlewares: make([]Middleware, 0, 5),
Logger: DefaultLogger,
AccessLogger: DefaultLogger,
Renderer: nil,
contextIdSeq: 0,
Hooks: make(Hooks),
}
self.OnPanic = self.DefaultOnPanic
self.OnNotFound = self.DefaultOnNotFound
return self
}
func (app *App) newContextId() string {
now := time.Now()
return fmt.Sprintf("%04d%02d%02d%02d%02d%010d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), atomic.AddUint32(&(app.contextIdSeq), 1))
}
func (app *App) DefaultOnPanic(w http.ResponseWriter, r *http.Request, rcv interface{}) {
if app.Config.Debug {
http.Error(w, fmt.Sprintf("%v:\n\n%s", rcv, debug.Stack()), http.StatusInternalServerError)
} else {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
func (app *App) DefaultOnNotFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
// Builds an url for the given named route with path parameters.
func (app *App) BuildUrl(n string, args ...string) string {
route, ok := app.Routes[n]
if !ok {
panic(fmt.Sprintf("Route '%v' not defined.", n))
}
reg := regexp.MustCompile(`\(\?P<([^<]+)>[^\)]+\)`)
counter := -1
return reg.ReplaceAllStringFunc(route.PatternString, func(m string) string {
counter += 1
return args[counter]
})
}
// Adds a middleware to the end of the middleware chain.
func (app *App) Use(middlewares ...interface{}) {
app.Middlewares = append(app.Middlewares, MiddlewaresOf(middlewares...)...)
}
// Returns a new MountPoint object associated the given path.
func (app *App) MountPoint(path string) *MountPoint {
mp := &MountPoint{app, strings.TrimRight(path, "/") + "/", make([]Middleware, 0, len(app.Middlewares)+5)}
mp.Middlewares = append(mp.Middlewares, app.Middlewares...)
return mp
}
func (app *App) cleanup(w http.ResponseWriter, r *http.Request) {
if rcv := recover(); rcv != nil {
app.OnPanic(w, r, rcv)
}
ctx := RequestContext(r)
ctx.ResponseTime = time.Now().Sub(ctx.StartedAt)
app.Hooks.Run("end_request", HookDirectionReverse, w, r, nil)
}
func (app *App) ServeHTTP(ww http.ResponseWriter, r *http.Request) {
w := NewResponseWriter(ww)
ctx := NewContext(app, app.newContextId(), r)
ctx.StartedAt = time.Now()
defer app.cleanup(w, r)
app.Hooks.Run("start_request", HookDirectionNormal, w, r, nil)
path := r.URL.Path
method := r.Method
if app.Config.AllowHttpMethodOverwrite {
if overwrittenMethod := r.PostFormValue("_method"); len(overwrittenMethod) > 0 {
method = overwrittenMethod
}
}
for _, route := range app.Routes {
if strings.ToUpper(method) != strings.ToUpper(route.Method) {
continue
}
submatches := route.Pattern.FindStringSubmatch(path)
if len(submatches) > 0 {
for i, pathParamName := range route.PathParamNames {
ctx.PathParams.Add(pathParamName, submatches[i+1])
}
ctx.Route = route
}
if ctx.Route != nil {
break
}
}
if ctx.Route == nil {
app.OnNotFound(w, r)
return
}
app.Hooks.Run("start_action", HookDirectionNormal, w, r, nil)
ctx.Route.ServeHTTP(w, r)
app.Hooks.Run("end_action", HookDirectionReverse, w, r, nil)
}
func (app *App) writeAccessLog(w http.ResponseWriter, r *http.Request, d interface{}) {
data := map[string]interface{}{
"c": RequestContext(r),
"res": w,
"req": r,
}
var b bytes.Buffer
app.accessLogTemplate.Execute(&b, data)
s := b.String()
app.AccessLogger(LogLevelInfo, s)
}
//
func (app *App) Setup() {
if app.Renderer == nil {
cfg := DefaultHtmlTemplateRendererConfig()
cfg.TemplateDirectory = app.Config.TemplateDirectory
app.Renderer = NewHtmlTemplateRenderer(cfg)
}
app.Hooks.Add("end_request", app.writeAccessLog)
app.Hooks.Run("setup", HookDirectionNormal, nil, nil, app)
if app.Config.AutoMaxProcs {
runtime.GOMAXPROCS(runtime.NumCPU())
}
app.Renderer.Compile()
tmpl, err := template.New("cidre.acccesslog").Parse(app.Config.AccessLogFormat)
if err != nil {
panic(err)
}
app.accessLogTemplate = tmpl
}
// Returns a new http.Server object.
func (app *App) Server() *http.Server {
server := &http.Server{
Addr: app.Config.Addr,
Handler: app,
ReadTimeout: app.Config.ReadTimeout,
WriteTimeout: app.Config.WriteTimeout,
MaxHeaderBytes: app.Config.MaxHeaderBytes,
}
server.SetKeepAlivesEnabled(app.Config.KeepAlive)
return server
}
// Run the http.Server. If _server is not passed, App.Server() will be used as a http.Server object.
func (app *App) Run(_server ...*http.Server) {
if app.accessLogTemplate == nil {
app.Setup()
}
var server *http.Server
if len(_server) > 0 {
server = _server[0]
} else {
server = app.Server()
}
app.Hooks.Run("start_server", HookDirectionNormal, nil, nil, app)
app.Logger(LogLevelInfo, fmt.Sprintf("Server started: addr=%v", app.Config.Addr))
server.ListenAndServe()
}
/* }}} */