forked from jhvst/vertigo
/
main.go
183 lines (156 loc) · 6.92 KB
/
main.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
package main
import (
"html/template"
"os"
"runtime"
"time"
. "github.com/9uuso/vertigo/databases/gorm"
. "github.com/9uuso/vertigo/misc"
. "github.com/9uuso/vertigo/routes"
. "github.com/9uuso/vertigo/settings"
"github.com/9uuso/timezone"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"github.com/martini-contrib/strict"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) // defining gomaxprocs is proven to add performance by few percentages
}
// NewServer spaws a new Vertigo server
func NewServer() *martini.ClassicMartini {
helpers := template.FuncMap{
// unescape unescapes HTML of s.
// Used in templates such as "/post/display.tmpl"
"unescape": func(s string) template.HTML {
return template.HTML(s)
},
// title renders post's Title as the HTML document's title.
"title": func(t interface{}) string {
post, exists := t.(Post)
if exists {
return post.Title
}
return Settings.Name
},
// description renders page description.
// If none is defined, returns "Blog in Go" instead.
"description": func() string {
if Settings.Description == "" {
return "Blog in Go"
}
return Settings.Description
},
// updated checks if post has been updated.
"updated": func(p Post) bool {
if p.Updated > p.Created {
return true
}
return false
},
// date calculates unix date from d and offset in format: Monday, January 2, 2006 3:04PM (-0700 GMT)
"date": func(d int64, offset int) string {
return time.Unix(d, 0).UTC().In(time.FixedZone("", offset)).Format("Monday, January 2, 2006 3:04PM (-0700 GMT)")
},
// env returns environment variable of s.
"env": func(s string) string {
return os.Getenv(s)
},
// timezones returns all 416 valid IANA timezone locations.
"timezones": func() [416]timezone.Timezone {
return timezone.Locations
},
}
m := martini.Classic()
store := sessions.NewCookieStore([]byte(Settings.CookieHash))
m.Use(sessions.Sessions("user", store))
m.Use(Sessionchecker())
m.Use(strict.Strict)
m.Use(martini.Static("public", martini.StaticOptions{
SkipLogging: true,
// Adds 7 day Expire header for static files.
Expires: func() string {
return time.Now().Add(time.Hour * 168).UTC().Format("Mon, Jan 2 2006 15:04:05 GMT")
},
}))
m.Use(render.Renderer(render.Options{
Layout: "layout",
Funcs: []template.FuncMap{helpers}, // Specify helper function maps for templates to access.
}))
m.Get("/", Homepage)
m.Get("/rss", ReadFeed)
m.Group("/post", func(r martini.Router) {
// Please note that `/new` route has to be before the `/:slug` route. Otherwise the program will try
// to fetch for Post named "new".
// For now I'll keep it this way to streamline route naming.
r.Get("/new", ProtectedPage, func(res render.Render) {
res.HTML(200, "post/new", nil)
})
r.Get("/:slug", ReadPost)
r.Get("/:slug/edit", ProtectedPage, EditPost)
r.Post("/:slug/edit", ProtectedPage, strict.ContentType("application/x-www-form-urlencoded"), binding.Form(Post{}), binding.ErrorHandler, UpdatePost)
r.Get("/:slug/delete", ProtectedPage, DeletePost)
r.Get("/:slug/publish", ProtectedPage, PublishPost)
r.Get("/:slug/unpublish", ProtectedPage, UnpublishPost)
r.Post("/new", ProtectedPage, strict.ContentType("application/x-www-form-urlencoded"), binding.Form(Post{}), binding.ErrorHandler, CreatePost)
r.Post("/search", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(Search{}), binding.ErrorHandler, SearchPost)
})
m.Group("/user", func(r martini.Router) {
r.Get("", ProtectedPage, ReadUser)
//r.Post("/delete", strict.ContentType("application/x-www-form-urlencoded"), ProtectedPage, binding.Form(User{}), DeleteUser)
r.Get("/settings", ProtectedPage, ReadSettings)
r.Post("/settings", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(Vertigo{}), binding.ErrorHandler, ProtectedPage, UpdateSettings)
r.Post("/installation", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(Vertigo{}), binding.ErrorHandler, UpdateSettings)
r.Get("/register", SessionRedirect, func(res render.Render) {
res.HTML(200, "user/register", nil)
})
r.Post("/register", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(User{}), binding.ErrorHandler, CreateUser)
r.Get("/recover", SessionRedirect, func(res render.Render) {
res.HTML(200, "user/recover", nil)
})
r.Post("/recover", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(User{}), RecoverUser)
r.Get("/reset/:id/:recovery", SessionRedirect, func(res render.Render) {
res.HTML(200, "user/reset", nil)
})
r.Post("/reset/:id/:recovery", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(User{}), ResetUserPassword)
r.Get("/login", SessionRedirect, func(res render.Render) {
res.HTML(200, "user/login", nil)
})
r.Post("/login", strict.ContentType("application/x-www-form-urlencoded"), binding.Form(User{}), LoginUser)
r.Get("/logout", LogoutUser)
})
m.Group("/api", func(r martini.Router) {
r.Get("", func(res render.Render) {
res.HTML(200, "api/index", nil)
})
r.Get("/settings", ProtectedPage, ReadSettings)
r.Post("/settings", strict.ContentType("application/json"), binding.Json(Vertigo{}), binding.ErrorHandler, ProtectedPage, UpdateSettings)
r.Post("/installation", strict.ContentType("application/json"), binding.Json(Vertigo{}), binding.ErrorHandler, UpdateSettings)
r.Get("/users", ReadUsers)
r.Get("/user/logout", LogoutUser)
r.Get("/user/:id", ReadUser)
//r.Delete("/user", DeleteUser)
r.Post("/user", strict.ContentType("application/json"), binding.Json(User{}), binding.ErrorHandler, CreateUser)
r.Post("/user/login", strict.ContentType("application/json"), binding.Json(User{}), binding.ErrorHandler, LoginUser)
r.Post("/user/recover", strict.ContentType("application/json"), binding.Json(User{}), RecoverUser)
r.Post("/user/reset/:id/:recovery", strict.ContentType("application/json"), binding.Json(User{}), ResetUserPassword)
r.Get("/posts", ReadPosts)
r.Get("/post/:slug", ReadPost)
r.Post("/post", strict.ContentType("application/json"), binding.Json(Post{}), binding.ErrorHandler, ProtectedPage, CreatePost)
r.Get("/post/:slug/publish", ProtectedPage, PublishPost)
r.Get("/post/:slug/unpublish", ProtectedPage, UnpublishPost)
r.Post("/post/:slug/edit", strict.ContentType("application/json"), binding.Json(Post{}), binding.ErrorHandler, ProtectedPage, UpdatePost)
r.Get("/post/:slug/delete", ProtectedPage, DeletePost)
r.Post("/post", strict.ContentType("application/json"), binding.Json(Post{}), binding.ErrorHandler, ProtectedPage, CreatePost)
r.Post("/post/search", strict.ContentType("application/json"), binding.Json(Search{}), binding.ErrorHandler, SearchPost)
})
m.Router.NotFound(strict.MethodNotAllowed, func(res render.Render) {
res.HTML(404, "404", nil)
})
return m
}
func main() {
server := NewServer()
server.Run()
}