-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdecorators.go
77 lines (64 loc) · 1.99 KB
/
decorators.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
package fireball
import (
"log"
"net/http"
)
// A Decorator wraps logic around a Handler
type Decorator func(Handler) Handler
// Decorate is a helper function that decorates each Handler in each Route with the given Decorators
func Decorate(routes []*Route, decorators ...Decorator) []*Route {
decorated := make([]*Route, len(routes))
for i, route := range routes {
decorated[i] = &Route{
Path: route.Path,
Handlers: map[string]Handler{},
}
for method, handler := range route.Handlers {
for _, decorator := range decorators {
handler = decorator(handler)
}
decorated[i].Handlers[method] = handler
}
}
return decorated
}
// LogDecorator will print the method and url of each request
func LogDecorator() Decorator {
return func(handler Handler) Handler {
return func(c *Context) (Response, error) {
log.Printf("%s %s \n", c.Request.Method, c.Request.URL.String())
return handler(c)
}
}
}
// BasicAuthDecorator will add basic authentication using the specified username and password
func BasicAuthDecorator(username, password string) Decorator {
return func(handler Handler) Handler {
return func(c *Context) (Response, error) {
user, pass, ok := c.Request.BasicAuth()
if ok && user == username && pass == password {
return handler(c)
}
headers := map[string]string{"WWW-Authenticate": "Basic realm=\"Restricted\""}
response := NewResponse(401, []byte("401 Unauthorized\n"), headers)
return response, nil
}
}
}
// HeaderResponseDecorator will add the specified headers to each response
func HeaderResponseDecorator(headers map[string]string) Decorator {
return func(handler Handler) Handler {
return func(c *Context) (Response, error) {
response, err := handler(c)
var wrappedResponse ResponseFunc = func(w http.ResponseWriter, r *http.Request) {
for key, val := range headers {
if v := w.Header().Get(key); v == "" {
w.Header().Set(key, val)
}
}
response.Write(w, r)
}
return wrappedResponse, err
}
}
}