forked from willnix/tinkerBlog
/
site.go
68 lines (57 loc) · 1.46 KB
/
site.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
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"html/template"
"labix.org/v2/mgo"
"time"
)
// Middleware handler for mongodb
func Mongo() martini.Handler {
session, err := mgo.Dial("localhost/blog")
if err != nil {
panic(err)
}
return func(c martini.Context) {
reqSession := session.Clone()
c.Map(reqSession.DB("blog"))
defer reqSession.Close()
c.Next()
}
}
func main() {
// BasicAuth credentials for admin functions
username := "user"
password := "password"
m := martini.Classic()
//needs import ("time")
m.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout",
Funcs: []template.FuncMap{
{
"formatTime": func(args ...interface{}) string {
t1 := time.Time(args[0].(time.Time))
return t1.Format("Jan 2, 2006 at 3:04pm (MST)")
},
"unescaped": func(args ...interface{}) template.HTML {
return template.HTML(args[0].(string))
},
},
},
}))
// Middleware for mongodb connection
m.Use(Mongo())
// Setup static file serving
m.Use(martini.Static("assets"))
// Setup routing
m.Get("/", BlogEntryList)
m.Post("/blog/add/submit", auth.Basic(username, password), binding.Form(dbBlogEntry{}), addBlogEntrySubmit)
m.Get("/blog/add", auth.Basic(username, password), addBlogEntry)
m.Get("/post/:Id", BlogEntry)
m.Get("/about", About)
m.Get("/impressum", Impressum)
m.Run()
}