forked from DHowett/spectre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
148 lines (127 loc) · 3.41 KB
/
render.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
package main
import (
"encoding/base64"
"encoding/json"
"io"
"math/rand"
"net/http"
"net/url"
"github.com/golang/glog"
)
type Model interface{}
type CustomTemplateError interface {
ErrorTemplateName() string
}
type HTTPError interface {
StatusCode() int
}
type DeferLookupError struct {
Interstitial *url.URL
}
func (d DeferLookupError) Error() string {
return ""
}
type ModelRenderFunc func(Model, http.ResponseWriter, *http.Request)
type ModelLookupFunc func(*http.Request) (Model, error)
func RenderError(e error, statusCode int, w http.ResponseWriter) {
w.WriteHeader(statusCode)
page := "error"
if cte, ok := e.(CustomTemplateError); ok {
page = cte.ErrorTemplateName()
}
RenderPage(w, nil, page, e)
}
func errorRecoveryHandler(w http.ResponseWriter) {
if err := recover(); err != nil {
status := http.StatusInternalServerError
if weberr, ok := err.(HTTPError); ok {
status = weberr.StatusCode()
}
RenderError(err.(error), status, w)
}
}
// renderModelWith takes a template name and
// returns a function that takes a single model object,
// which when called will render the given template using that object.
func RenderPageForModel(page string) ModelRenderFunc {
// We don't defer the error handler here because it happened a step up
return func(o Model, w http.ResponseWriter, r *http.Request) {
RenderPage(w, r, page, o)
}
}
func RenderPageHandler(page string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer errorRecoveryHandler(w)
RenderPage(w, r, page, nil)
})
}
func RenderPage(w io.Writer, r *http.Request, page string, obj interface{}) {
ExecuteTemplate(w, "tmpl_page", &RenderContext{Request: r, Page: page, Obj: obj})
}
func RenderPartialHandler(page string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
RenderPartial(w, r, "error", err.(error))
}
}()
RenderPartial(w, r, page, nil)
})
}
func RenderPartial(w io.Writer, r *http.Request, name string, obj interface{}) {
ExecuteTemplate(w, "partial_"+name, &RenderContext{Request: r, Page: name, Obj: obj})
}
func RequiredModelObjectHandler(lookup ModelLookupFunc, fn ModelRenderFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer errorRecoveryHandler(w)
if obj, err := lookup(r); err != nil {
if dle, ok := err.(DeferLookupError); ok {
http.SetCookie(w, &http.Cookie{
Name: "destination",
Value: r.URL.String(),
Path: "/",
})
w.Header().Set("Location", dle.Interstitial.String())
w.WriteHeader(http.StatusFound)
} else {
panic(err)
}
} else {
fn(obj, w, r)
}
})
}
func SetFlash(w http.ResponseWriter, kind, body string) {
flashBody, err := json.Marshal(map[string]string{
"type": kind,
"body": body,
})
if err != nil {
return
}
http.SetCookie(w, &http.Cookie{
Name: "flash",
Value: base64.URLEncoding.EncodeToString(flashBody),
Path: "/",
MaxAge: 60,
})
}
var ghosts []string
func init() {
RegisterReloadFunction(func() {
ghosts = []string{}
err := YAMLUnmarshalFile("ghosts.yml", &ghosts)
if err != nil {
glog.Error(err)
}
for i, v := range ghosts {
ghosts[i] = " " + v[1:]
}
})
RegisterTemplateFunction("randomGhost", func() string {
if len(ghosts) == 0 {
return "[no ghosts found :(]"
}
return ghosts[rand.Intn(len(ghosts))]
})
}