/
bantam.go
executable file
·254 lines (227 loc) · 7.55 KB
/
bantam.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package bantam
import (
"container/list"
"regexp"
"strconv"
"strings"
"github.com/hoisie/web.go"
"github.com/hoisie/mustache.go"
"github.com/stathat/jconfig"
"launchpad.net/goforms"
"github.com/griffy/birdbrain"
"github.com/griffy/birdbrain/store"
"github.com/griffy/htmlfiller"
)
var sessionStore birdbrain.SessionStore
var paramNameRegex *regexp.Regexp
func init() {
// FIXME: should use lazy qualifier when Go
// has more advanced regexp package
paramNameRegex = regexp.MustCompile("\\{.*\\}")
}
type view interface {
Uri() string
}
type viewHelper struct {
view // anonymous field so all its fields are still accessible
Flash bool
FlashType, FlashMessage string
}
type Conn struct {
*web.Context
RouteParams map[string]string
Session *birdbrain.Session
Tmpl map[string]interface{}
}
func deflattenParams(params map[string]string) map[string][]string {
fullParams := make(map[string][]string)
for name, val := range params {
fullParams[name] = []string{val}
}
return fullParams
}
func (c *Conn) ValidParams(form *forms.Form) bool {
// TODO: add a 'background' xsrf check here
form.SetFormData(c.FullParams)
// calling IsValid results in form's CleanedData being
// populated if it passes validation, otherwise
// form's errors are populated
return form.IsValid()
}
func (c *Conn) ValidRouteParams(form *forms.Form) bool {
fullParams := deflattenParams(c.RouteParams)
form.SetFormData(fullParams)
return form.IsValid()
}
func (c *Conn) getFlash() (string, string) {
flash, err := c.Session.Get("_flash")
if err == nil {
c.Session.Delete("_flash")
flashPieces := strings.SplitN(flash, ":", 2)
return flashPieces[0], flashPieces[1]
}
return "", ""
}
func (c *Conn) addTemplateFlash() {
flashType, flashMsg := c.getFlash()
// if it hasn't already been set
if _, ok := c.Tmpl["Flash"]; !ok {
c.Tmpl["Flash"] = (flashMsg != "")
c.Tmpl["FlashType"] = flashType
c.Tmpl["FlashMessage"] = flashMsg
}
}
func (c *Conn) addViewFlash(v view) *viewHelper {
c.addTemplateFlash()
return &viewHelper{v,
c.Tmpl["Flash"].(bool),
c.Tmpl["FlashType"].(string),
c.Tmpl["FlashMessage"].(string)}
}
func (c *Conn) Render(file interface{}, forms ...*forms.Form) {
// TODO: c.Tmpl["h"] = &helpers.Helpers{}
res := ""
switch (file.(type)) {
case string:
uri, _ := file.(string)
// add flash to the template context if it exists
c.addTemplateFlash()
res = mustache.RenderFile(uri, c.Tmpl)
case view:
v, _ := file.(view)
// add flash to the viewhelper view to be rendered
view := c.addViewFlash(v)
res = mustache.RenderFile(view.Uri(), view)
}
// if there are forms supplied, the user failed
// at providing valid input, so we must fill in the
// appropriate form values along with the error messages
for _, form := range forms {
defaultVals := make(map[string]string)
for name, field := range form.Fields {
defaultVals[name] = field.Value()
}
res = htmlfiller.Fill(res, defaultVals, form.Errors)
}
c.WriteString(res)
}
func (c *Conn) Redirect(addr string, args ...int) {
if len(args) > 0 {
c.Context.Redirect(args[0], addr)
} else {
c.Context.Redirect(302, addr)
}
}
func (c *Conn) Flash(typeof, message string) {
c.Session.Set("_flash", typeof + ":" + message)
// also add it to the current template context in case
// Render is called right afterward rather than Redirect
// and this is the first time session has been used (cookie
// has not been sent to the user yet)
c.Tmpl["Flash"] = true
c.Tmpl["FlashType"] = typeof
c.Tmpl["FlashMessage"] = message
}
type RouteHandler func(*Conn)
func buildHandler(handler RouteHandler, paramNames []string) func(*web.Context, ...string) {
// web.go looks to see if the first parameter to a
// handler function is a Context object before deciding
// to pass it, so we must do so
return func(ctx *web.Context, params ...string) {
// use the context to either start a new session or resume
// an existing one
session := birdbrain.NewSession(ctx, sessionStore)
// create a Conn object that holds context, params from
// the route/url, session, and a
// map of keys to values that will be passed to the
// page template for rendering
paramMap := make(map[string]string)
for i, p := range paramNames {
paramMap[p] = params[i]
}
c := &Conn{ctx, paramMap, session, make(map[string]interface{})}
handler(c)
}
}
func removeTrailingSlash(uri string) string {
if strings.HasSuffix(uri, "/") {
uri = uri[:len(uri)-1]
}
return uri
}
func extractParamNames(route string) []string {
names := []string{}
queue := list.New()
useQueue := false
for _, char := range route {
if char == '{' {
useQueue = true
queue = list.New()
} else if char == '}' {
useQueue = false
paramName := ""
for e := queue.Back(); e != nil; e = e.Prev() {
codePoint, _ := e.Value.(int)
paramName += string([]int{codePoint})
}
names = append(names, paramName)
} else {
if useQueue {
queue.PushFront(char)
}
}
}
return names
}
func toRegex(route string) (regex string) {
// FIXME: should use lazy qualifier when Go
// has more advanced regexp package
regex = paramNameRegex.ReplaceAllString(route, "(.*)")
// make the final slash optional, directing to the same route
// FIXME: should we instead issue redirects rather than
// allowing two urls to point to the same resource?
regex += "/?"
return
}
func prepareRoute(route string) (string, []string) {
route = removeTrailingSlash(route)
paramNames := extractParamNames(route)
regexRoute := toRegex(route)
return regexRoute, paramNames
}
func Get(route string, handler RouteHandler) {
regexRoute, paramNames := prepareRoute(route)
web.Get(regexRoute, buildHandler(handler, paramNames))
}
func Post(route string, handler RouteHandler) {
regexRoute, paramNames := prepareRoute(route)
web.Post(regexRoute, buildHandler(handler, paramNames))
}
func Put(route string, handler RouteHandler) {
regexRoute, paramNames := prepareRoute(route)
web.Put(regexRoute, buildHandler(handler, paramNames))
}
func Delete(route string, handler RouteHandler) {
regexRoute, paramNames := prepareRoute(route)
web.Delete(regexRoute, buildHandler(handler, paramNames))
}
func Run() {
// TODO: set up logger and config here
//InitLogger()
//InitMemcache()
//web.SetLogger(Logger)
// TODO: add error checking
config := jconfig.LoadConfig("app.conf")
web.Config.StaticDir = config.GetString("StaticDirectory")
web.Config.Addr = config.GetString("Host")
web.Config.Port = config.GetInt("Port")
web.Config.CookieSecret = config.GetString("CookieSecret")
switch config.GetString("SessionStore") {
case "redis":
host := config.GetString("SessionHost")
port := strconv.Itoa(config.GetInt("SessionPort"))
sessionStore = store.NewRedisStore("tcp:" + host + ":" + port)
}
addr := web.Config.Addr + ":" + strconv.Itoa(web.Config.Port)
web.Run(addr)
}