-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
78 lines (65 loc) · 2.66 KB
/
helper.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
package page
import (
"net/http"
"runtime/debug"
"strings"
"github.com/rur/good/baseline/starter_test/site"
"github.com/rur/treetop"
)
// ViewHandlerWithEnv is the signature of a treetop view with injected Env instance (see BindEnv)
type ViewHandlerWithEnv func(*site.Env, treetop.Response, *http.Request) interface{}
// Helper is passed to page Routes functions at startup for binding handlers and paths
type Helper struct {
Env *site.Env
// EDITME: if you wish to use a different router library
Mux *http.ServeMux
}
// BindEnv will inject the sever environment to a treetop request handler using closures
func (hlp Helper) BindEnv(fn ViewHandlerWithEnv) treetop.ViewHandlerFunc {
return func(rsp treetop.Response, req *http.Request) interface{} {
defer func() {
if err := recover(); err != nil {
// EDITME: you might wish to handle panics in your own way
hlp.Env.ErrorLog.Println("[runtime panic]", err, "\nStack Trace:\n", string(debug.Stack()))
if !rsp.Finished() {
// the response has not been written yet, do so now
http.Error(rsp, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
}()
return fn(hlp.Env, rsp, req)
}
}
// HandleGET registers a suppled handler with a pattern accepting GET/HEAD requests only,
// the semantics of how the pattern are handled depends on your router.
func (hlp Helper) HandleGET(pattern string, hdr http.Handler) {
hlp.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
switch strings.ToUpper(r.Method) {
case "GET", "HEAD":
hdr.ServeHTTP(w, r)
case "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE":
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
})
}
// HandlePOST registers a suppled handler with a pattern accepting POST requests only,
// the semantics of how the pattern are handled depends on your router.
func (hlp Helper) HandlePOST(pattern string, hdl http.Handler) {
hlp.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
switch strings.ToUpper(r.Method) {
case "POST":
hdl.ServeHTTP(w, r)
case "GET", "HEAD", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE":
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
})
}
// Handle will register a pattern without filtering requests by method, the
// request handler is responsible for differentiating
func (hlp Helper) Handle(pattern string, hdl http.Handler) {
hlp.Mux.Handle(pattern, hdl)
}