-
Notifications
You must be signed in to change notification settings - Fork 1
/
middle.go
79 lines (69 loc) · 1.88 KB
/
middle.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
package main
import (
"log"
"net/http"
"strings"
"time"
"github.com/gorilla/context"
"gopkg.in/mgo.v2/bson"
)
func GetResponse(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slug := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
var response Response
c := controller.session.DB("test").C("responses")
query := c.Find(bson.M{"slug": slug})
err := query.One(&response)
if err != nil {
log.Println("looking for slug:", slug)
log.Println("in getReponse middleware", err)
notFound(w, r)
return
}
context.Set(r, "response", response)
inner.ServeHTTP(w, r)
})
}
func GetSlug(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slug := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
context.Set(r, "slug", slug)
inner.ServeHTTP(w, r)
})
}
func GetCookie(inner http.Handler, cookieName string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var value string
cookie, err := r.Cookie(cookieName)
if err != nil {
log.Println("WARNING: could not find cookie", cookieName)
goto setAndReturn
}
err = cookieHandler.Decode(cookieName, cookie.Value, &value)
if err != nil {
log.Println("WARNING:found cookie but could not decode")
}
setAndReturn:
context.Set(r, cookieName, value)
inner.ServeHTTP(w, r)
})
}
func GetAuthorFromCookie(inner http.Handler) http.Handler {
return GetCookie(inner, "membraneAuthor")
}
func GetReaderFromCookie(inner http.Handler) http.Handler {
return GetCookie(inner, "membraneReader")
}
func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
inner.ServeHTTP(w, r)
log.Printf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}