-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
87 lines (70 loc) · 2.14 KB
/
middleware.go
File metadata and controls
87 lines (70 loc) · 2.14 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
package web
import (
"io"
"net/http"
"github.com/bhenderson/web/flush"
"github.com/bhenderson/web/head"
"github.com/bhenderson/web/log"
"github.com/bhenderson/web/session"
)
// return a HandlerFunc because that's the common use case.
// if we changed it to return a Handler, we'd have to typecast every time
// we create an annonymous function.
// we accept a Handler however, because HandlerFunc is also a Handler. booyah!
type Middleware func(http.Handler) http.HandlerFunc
type Stack []Middleware
func (s *Stack) Use(ms ...Middleware) {
*s = append(*s, ms...)
}
// Run takes a http.Handler (http.DefaultServeMux if nil) and builds the
// middleware stack to return a new http.Handler.
func (s *Stack) Run(app http.Handler) (f http.Handler) {
if app == nil {
f = http.DefaultServeMux
} else {
f = app
}
ms := *s
// reverse
for i := len(ms) - 1; i >= 0; i-- {
// The simple case
f = ms[i](f)
}
return
}
var defaultStack = &Stack{}
// Use adds Middleware to the default stack.
func Use(ms ...Middleware) {
defaultStack.Use(ms...)
}
// Run compiles the default stack of middleware and returns an http.Handler.
func Run(app http.Handler) http.Handler {
return defaultStack.Run(app)
}
//go:generate go run genplusser.go
// WrapResponseWriter takes an http.ResponseWriter (wr) and wraps it with the
// functionality provided by wn. The return value tries hard to implement any
// extra methods that wr might also implement.
func WrapResponseWriter(wn, wr http.ResponseWriter) http.ResponseWriter {
return newPlusser(wn, wr)
}
const (
// Log formats
CombinedLog = log.Combined
CommonLog = log.Common
)
// Flush implements Middleware. See flush.FlushMiddleware for usage.
func Flush(next http.Handler) http.HandlerFunc {
return flush.FlushMiddleware(next)
}
// Head implements Middleware. See head.HeadMiddleware for usage.
func Head(next http.Handler) http.HandlerFunc {
return head.HeadMiddleware(next)
}
// Log returns a Middleware. See log.LogMiddleware for usage.
func Log(w io.Writer, t string) Middleware {
return log.LogMiddleware(w, t)
}
func Session(secret, name string) Middleware {
return session.SessionMiddleware(secret, name)
}