-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
executable file
·38 lines (29 loc) · 1010 Bytes
/
handler.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
package scaffold
import (
"net/http"
"golang.org/x/net/context"
)
// Handler is a context aware handler
type Handler interface {
CtxServeHTTP(context.Context, http.ResponseWriter, *http.Request)
}
// HandlerFunc wraps a handler letting it implement Handler
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)
// CtxServeHTTP implements Handler.CtxServeHTTP
func (h HandlerFunc) CtxServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
h(ctx, w, r)
}
// HandlerList is an array of handlers
type HandlerList []Handler
// CtxServeHTTP implements Handler.CtxServeHTTP
func (h HandlerList) CtxServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, handler := range h {
handler.CtxServeHTTP(ctx, w, r)
}
}
// NotFound wraps http.NotFound
func NotFound(ctx context.Context, w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
// NotFoundHandler is a not found handler
var NotFoundHandler Handler = HandlerFunc(NotFound)