/bloody.go

http://github.com/TheOnly92/bloody.go · Go · 164 lines · 145 code · 18 blank · 1 comment · 19 complexity · 0ed279250800f6e51986c52399e2ba40 MD5 · raw file

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