-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
169 lines (147 loc) · 4.12 KB
/
http.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
package main
import (
"io"
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
)
type errorNotFound string
func (e errorNotFound) Error() string {
return string(e)
}
var errNotFound = errorNotFound("Not found")
type httpHandler struct {
helpTmpl *help
cache *caches
config *config
crawler *crawler
indexer *mailIndexer
paths []string
}
func newHttpHandler(help *help, cache *caches, config *config, crawler *crawler, indexer *mailIndexer) *httpHandler {
return &httpHandler{
helpTmpl: help,
cache: cache,
config: config,
crawler: crawler,
indexer: indexer,
paths: config.keys.all(),
}
}
func (h *httpHandler) run(srv *http.Server) error {
r := mux.NewRouter()
r.HandleFunc("/", h.forward("latest/0"))
r.HandleFunc("/help", h.help)
r.HandleFunc("/latest/{selector}", h.messages("", false))
r.HandleFunc("/oldest/{selector}", h.messages("", true))
for key := range h.config.keys {
if key == "" {
continue
}
prefix := "/" + key
r.HandleFunc(prefix, h.list(key))
r.HandleFunc(prefix+"/", h.forward(""))
r.HandleFunc(prefix+"/{value}", h.forward("/latest/0"))
r.HandleFunc(prefix+"/{value}/latest", h.forward("/0"))
r.HandleFunc(prefix+"/{value}/oldest", h.forward("/0"))
r.HandleFunc(prefix+"/{value}/latest/{selector}", h.messages(key, false))
r.HandleFunc(prefix+"/{value}/oldest/{selector}", h.messages(key, true))
}
srv.Handler = r
return srv.ListenAndServe()
}
func (httpHandler) forward(url string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
url = strings.TrimRight(r.URL.Path+url, "/")
// TODO: Is this the right code for POST/DELETE requests?
http.Redirect(w, r, url, 307)
}
}
func (h *httpHandler) help(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not supported", 405)
return
}
tmpl := newTemplate()
if err := tmpl.render(w, h.helpTmpl); err != nil {
http.Error(w, err.Error(), 500)
log.Print(err)
}
}
func (h *httpHandler) messages(key string, oldest bool) func(w http.ResponseWriter, r *http.Request) {
return h.handler(func(h *httpHandler, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
cr := newCacheRequest()
cr.oldest = oldest
cr.header = key
cr.value = vars["value"]
if !h.indexer.keys.has(cr.header) {
return errNotFound
}
if vars["selector"] == "" {
return errNotFound // XXX: bad request
}
if err := selector(vars["selector"]).parse(cr); err != nil {
return errNotFound // XXX: bad request
}
w.Header().Set("Content-Type", "text/plain")
if r.Method == "DELETE" {
return h.deleteMessages(cr)
}
return h.writeMessages(cr, w)
})
}
func (h *httpHandler) list(k string) func(w http.ResponseWriter, r *http.Request) {
return h.handler(func(h *httpHandler, w http.ResponseWriter, r *http.Request) error {
cr, err := makeCacheListRequest(k)
if err != nil {
return err
}
h.cache.listCh <- cr
data := <-cr.data
if data == nil || len(data) == 0 {
return errNotFound
}
tmpl := newTemplate()
tw := newListTemplate(cr.header, data)
w.Header().Set("Content-Type", "text/html")
return tmpl.render(w, tw)
})
}
func (h *httpHandler) handler(fn func(h *httpHandler, w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch err := fn(h, w, r); err {
case nil:
// XXX: Log successful requests?
return
case errNotFound:
log.Print(r.URL.Path, ": ", err)
http.NotFound(w, r)
default:
log.Print(r.URL.Path, ": ", err)
http.Error(w, err.Error(), 500)
}
}
}
func (h *httpHandler) writeMessages(cr *cacheRequest, w io.Writer) error {
cr.match = h.indexer.keys.keyType(cr.header)
h.cache.requestCh <- cr
data := <-cr.data
if data == nil || len(data) == 0 {
return errNotFound
}
data.writeTo(w, h.config)
return nil
}
func (h *httpHandler) deleteMessages(cr *cacheRequest) error {
cr.match = h.indexer.keys.keyType(cr.header)
h.cache.requestCh <- cr
msgs := <-cr.data
if msgs == nil || len(msgs) == 0 {
return errNotFound
}
msgs.delete()
h.crawler.rescan()
return nil
}