This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
/
bloody.go
164 lines (146 loc) · 4.42 KB
/
bloody.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
package main
import (
"web"
"launchpad.net/mgo"
"mustache"
"regexp"
"session"
"bytes"
"os"
)
type User struct {
Username string
Password string
}
var mSession *mgo.Session
var mongoInit = false
var h *session.MHandler
var config *Config
var layoutChanged int64
var layout *mustache.Template
var blogConfig *PreferenceModel
func initMongo() {
var err error
mSession, err = mgo.Mongo(config.Get("mongohost"))
if err != nil {
panic(err)
}
}
func getLayoutChanged() int64 {
dir, _ := os.Getwd()
f, err := os.Open(dir+"/templates/layout.mustache")
if err != nil {
panic(err)
}
defer f.Close()
info, _ := f.Stat()
return info.ModTime().Unix()
}
func initLayout() {
layoutChanged = getLayoutChanged()
layout, _ = mustache.ParseFile("templates/layout.mustache")
}
func render(output string, title string) string {
vars := make(map[string]interface{})
vars["Body"] = output
if title != "" {
vars["Title"] = map[string]string {"Name": title}
}
p := PageModelInit()
vars["SidePages"] = p.Sidebar()
// Check if layout is changed
if getLayoutChanged() != layoutChanged {
layoutChanged = getLayoutChanged()
layout, _ = mustache.ParseFile("templates/layout.mustache")
}
return layout.Render(vars)
}
func objectIdHex(objectId string) string {
var rx_objecthex = regexp.MustCompile("ObjectIdHex\\(\"([A-Za-z0-9]+)\"\\)")
match := rx_objecthex.FindStringSubmatch(objectId)
return match[1]
}
func toAscii(str string) string {
var rx_ascii = regexp.MustCompile("[^a-zA-Z0-9/_|+ \\-]")
var rx_chars = regexp.MustCompile("[/_|+ \\-]+")
rt := rx_ascii.ReplaceAllString(str, "")
rt = string(bytes.ToLower([]byte(rt)))
rt = rx_chars.ReplaceAllString(rt, "-")
return rt
}
func pagination(page int, totPages int) map[string]interface{} {
start := 1
if page > 3 {
start = page - 2
}
end := start + 4
if end > totPages {
end = totPages
}
length := 5
if totPages < length {
length = totPages
}
pages := make([]map[string]int, length)
cnt := 0
for i:=start; i <= end; i++ {
temp := map[string]int{"page": i}
if i == page {
temp["current"] = 1
}
pages[cnt] = temp
cnt++
}
before := true
beforePage := page - 1
after := true
afterPage := page + 1
lastPage := totPages
if page == 1 {
before = false
beforePage = 0
}
if page == totPages {
after = false
}
return map[string]interface{} {"Pages": pages, "Before": before, "BeforePage": beforePage, "After": after, "AfterPage": afterPage, "LastPage": lastPage}
}
func main() {
config = loadConfig()
initMongo()
initLayout()
h = new(session.MHandler)
h.SetSession(mSession)
blogConfig = PreferenceInit()
path, _ := os.Getwd()
web.Config.StaticDir = path + "/" + config.Get("staticdir")
i := &Index{}
a := &Admin{}
web.Get("/", web.MethodHandler(i, "Index"))
web.Get("/post/list", web.MethodHandler(i, "ListPosts"))
web.Get("/post/([A-Za-z0-9]+)", web.MethodHandler(i, "ReadPost"))
web.Get("/page/([a-z0-9\\-]+)\\.html", web.MethodHandler(i, "ReadPage"))
web.Post("/post/([A-Za-z0-9]+)/comment/new", web.MethodHandler(i, "NewComment"))
web.Get("/admin", web.MethodHandler(a, "IndexGet"))
web.Get("/admin/preferences", web.MethodHandler(a, "PreferencesGet"))
web.Post("/admin/preferences", web.MethodHandler(a, "PreferencesPost"))
web.Get("/admin/post/new", web.MethodHandler(a, "NewPostGet"))
web.Post("/admin/post/new", web.MethodHandler(a, "NewPostPost"))
web.Get("/admin/post/list", web.MethodHandler(a, "ListPost"))
web.Post("/admin/post/list", web.MethodHandler(a, "BulkActions"))
web.Get("/admin/post/edit/(.*)", web.MethodHandler(a, "EditPostGet"))
web.Post("/admin/post/edit/(.*)", web.MethodHandler(a, "EditPostPost"))
web.Get("/admin/post/del/(.*)", web.MethodHandler(a, "DelPost"))
web.Get("/admin/login", web.MethodHandler(a, "LoginGet"))
web.Post("/admin/login", web.MethodHandler(a, "LoginPost"))
web.Get("/admin/page/new", web.MethodHandler(a, "NewPageGet"))
web.Post("/admin/page/new", web.MethodHandler(a, "NewPagePost"))
web.Get("/admin/page/list", web.MethodHandler(a, "ListPagesGet"))
web.Get("/admin/page/edit/(.*)", web.MethodHandler(a, "EditPageGet"))
web.Post("/admin/page/edit/(.*)", web.MethodHandler(a, "EditPagePost"))
web.Get("/admin/page/del/(.*)", web.MethodHandler(a, "DelPage"))
web.Get("/admin/comment/del/(.*)/(.*)", web.MethodHandler(a, "DelComment"))
web.Get("/admin/bloody/restart", web.MethodHandler(a, "RestartBloody"))
web.Get("/rss", web.MethodHandler(i, "RSS"))
web.Run(config.Get("host")+":"+config.Get("port"))
}