PageRenderTime 58ms CodeModel.GetById 41ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/bloody.go

http://github.com/TheOnly92/bloody.go
Go | 164 lines | 145 code | 18 blank | 1 comment | 18 complexity | 0ed279250800f6e51986c52399e2ba40 MD5 | raw file
  1package main
  2
  3import (
  4	"web"
  5	"launchpad.net/mgo"
  6	"mustache"
  7	"regexp"
  8	"session"
  9	"bytes"
 10	"os"
 11)
 12
 13type User struct {
 14	Username	string
 15	Password	string
 16}
 17
 18var mSession *mgo.Session
 19var mongoInit = false
 20var h *session.MHandler
 21var config *Config
 22var layoutChanged int64
 23
 24var layout *mustache.Template
 25
 26var blogConfig *PreferenceModel
 27
 28func initMongo() {
 29	var err error
 30	mSession, err = mgo.Mongo(config.Get("mongohost"))
 31	if err != nil {
 32		panic(err)
 33	}
 34}
 35
 36func getLayoutChanged() int64 {
 37	dir, _ := os.Getwd()
 38	f, err := os.Open(dir+"/templates/layout.mustache")
 39	if err != nil {
 40		panic(err)
 41	}
 42	defer f.Close()
 43
 44	info, _ := f.Stat()
 45	return info.ModTime().Unix()
 46}
 47
 48func initLayout() {
 49	layoutChanged = getLayoutChanged()
 50	layout, _ = mustache.ParseFile("templates/layout.mustache")
 51}
 52
 53func render(output string, title string) string {
 54	vars := make(map[string]interface{})
 55	vars["Body"] = output
 56	if title != "" {
 57		vars["Title"] = map[string]string {"Name": title}
 58	}
 59	p := PageModelInit()
 60	vars["SidePages"] = p.Sidebar()
 61
 62	// Check if layout is changed
 63	if getLayoutChanged() != layoutChanged {
 64		layoutChanged = getLayoutChanged()
 65		layout, _ = mustache.ParseFile("templates/layout.mustache")
 66	}
 67
 68	return layout.Render(vars)
 69}
 70
 71func objectIdHex(objectId string) string {
 72	var rx_objecthex = regexp.MustCompile("ObjectIdHex\\(\"([A-Za-z0-9]+)\"\\)")
 73	match := rx_objecthex.FindStringSubmatch(objectId)
 74	return match[1]
 75}
 76
 77func toAscii(str string) string {
 78	var rx_ascii = regexp.MustCompile("[^a-zA-Z0-9/_|+ \\-]")
 79	var rx_chars = regexp.MustCompile("[/_|+ \\-]+")
 80	rt := rx_ascii.ReplaceAllString(str, "")
 81	rt = string(bytes.ToLower([]byte(rt)))
 82	rt = rx_chars.ReplaceAllString(rt, "-")
 83	return rt
 84}
 85
 86func pagination(page int, totPages int) map[string]interface{} {
 87	start := 1
 88	if page > 3 {
 89		start = page - 2
 90	}
 91	end := start + 4
 92	if end > totPages {
 93		end = totPages
 94	}
 95	length := 5
 96	if totPages < length {
 97		length = totPages
 98	}
 99	pages := make([]map[string]int, length)
100	cnt := 0
101	for i:=start; i <= end; i++ {
102		temp := map[string]int{"page": i}
103		if i == page {
104			temp["current"] = 1
105		}
106		pages[cnt] = temp
107		cnt++
108	}
109	
110	before := true
111	beforePage := page - 1
112	after := true
113	afterPage := page + 1
114	lastPage := totPages
115	if page == 1 {
116		before = false
117		beforePage = 0
118	}
119	if page == totPages {
120		after = false
121	}
122	
123	return map[string]interface{} {"Pages": pages, "Before": before, "BeforePage": beforePage, "After": after, "AfterPage": afterPage, "LastPage": lastPage}
124}
125
126func main() {
127	config = loadConfig()
128	initMongo()
129	initLayout()
130	h = new(session.MHandler)
131	h.SetSession(mSession)
132	blogConfig = PreferenceInit()
133	path, _ := os.Getwd()
134	web.Config.StaticDir = path + "/" + config.Get("staticdir")
135	i := &Index{}
136	a := &Admin{}
137	web.Get("/", web.MethodHandler(i, "Index"))
138	web.Get("/post/list", web.MethodHandler(i, "ListPosts"))
139	web.Get("/post/([A-Za-z0-9]+)", web.MethodHandler(i, "ReadPost"))
140	web.Get("/page/([a-z0-9\\-]+)\\.html", web.MethodHandler(i, "ReadPage"))
141	web.Post("/post/([A-Za-z0-9]+)/comment/new", web.MethodHandler(i, "NewComment"))
142	web.Get("/admin", web.MethodHandler(a, "IndexGet"))
143	web.Get("/admin/preferences", web.MethodHandler(a, "PreferencesGet"))
144	web.Post("/admin/preferences", web.MethodHandler(a, "PreferencesPost"))
145	web.Get("/admin/post/new", web.MethodHandler(a, "NewPostGet"))
146	web.Post("/admin/post/new", web.MethodHandler(a, "NewPostPost"))
147	web.Get("/admin/post/list", web.MethodHandler(a, "ListPost"))
148	web.Post("/admin/post/list", web.MethodHandler(a, "BulkActions"))
149	web.Get("/admin/post/edit/(.*)", web.MethodHandler(a, "EditPostGet"))
150	web.Post("/admin/post/edit/(.*)", web.MethodHandler(a, "EditPostPost"))
151	web.Get("/admin/post/del/(.*)", web.MethodHandler(a, "DelPost"))
152	web.Get("/admin/login", web.MethodHandler(a, "LoginGet"))
153	web.Post("/admin/login", web.MethodHandler(a, "LoginPost"))
154	web.Get("/admin/page/new", web.MethodHandler(a, "NewPageGet"))
155	web.Post("/admin/page/new", web.MethodHandler(a, "NewPagePost"))
156	web.Get("/admin/page/list", web.MethodHandler(a, "ListPagesGet"))
157	web.Get("/admin/page/edit/(.*)", web.MethodHandler(a, "EditPageGet"))
158	web.Post("/admin/page/edit/(.*)", web.MethodHandler(a, "EditPagePost"))
159	web.Get("/admin/page/del/(.*)", web.MethodHandler(a, "DelPage"))
160	web.Get("/admin/comment/del/(.*)/(.*)", web.MethodHandler(a, "DelComment"))
161	web.Get("/admin/bloody/restart", web.MethodHandler(a, "RestartBloody"))
162	web.Get("/rss", web.MethodHandler(i, "RSS"))
163	web.Run(config.Get("host")+":"+config.Get("port"))
164}