main.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package main
  2. import (
  3. "net/http"
  4. //"fmt"
  5. "os"
  6. "io/ioutil"
  7. "regexp"
  8. "encoding/json"
  9. "html/template"
  10. "github.com/russross/blackfriday"
  11. //"bytes"
  12. )
  13. var validArchivePath = regexp.MustCompile("^/archive/?$")
  14. var validHomePath = regexp.MustCompile("^/?$")
  15. var validPostPath = regexp.MustCompile("^/([a-z0-9-]+)/?$")
  16. // var templates = template.Must(template.ParseFiles("templates/menu.html"))
  17. func homeHandler(w http.ResponseWriter, r *http.Request) {
  18. // read the json list of posts
  19. // match home and do accordingly
  20. if validHomePath.MatchString(r.URL.Path) {
  21. list, err:=loadList()
  22. if err != nil {
  23. http.Error(w, err.Error(), http.StatusInternalServerError)
  24. return
  25. }
  26. http.Redirect(w, r, path + "/"+list[0].Url, http.StatusFound)
  27. return
  28. }
  29. m:=validPostPath.FindStringSubmatch(r.URL.Path)
  30. if m == nil {
  31. http.NotFound(w, r)
  32. return
  33. }
  34. // match
  35. list, err:=loadList()
  36. if err != nil {
  37. http.Error(w, err.Error(), http.StatusInternalServerError)
  38. return
  39. }
  40. found := false
  41. index :=-1
  42. for i, item:= range list {
  43. if item.Url == m[1] {
  44. found = true
  45. index = i
  46. break;
  47. }
  48. }
  49. if found {
  50. content, err := loadPost(list[index].Url)
  51. if err != nil {
  52. http.Error(w, err.Error(), http.StatusInternalServerError)
  53. return
  54. }
  55. ////
  56. type Post struct {
  57. Body template.HTML
  58. Title string
  59. Path string
  60. Prev string
  61. Next string
  62. }
  63. var prev string
  64. var next string
  65. if index > 0 {
  66. next=list[index-1].Url
  67. }
  68. if index < len(list)-1 {
  69. prev=list[index+1].Url
  70. }
  71. tmpl := template.Must(template.New("post.html").ParseFiles("templates/post.html", "templates/menu.html"))
  72. p:=Post{
  73. Body: template.HTML(content),
  74. Title: list[index].Title,
  75. Path: path,
  76. Prev: prev,
  77. Next: next,
  78. }
  79. err2 := tmpl.ExecuteTemplate(w, "post.html", p)
  80. if err2 != nil {
  81. http.Error(w, err2.Error(), http.StatusInternalServerError)
  82. }
  83. ////
  84. return
  85. }
  86. http.NotFound(w,r)
  87. return
  88. }
  89. func archiveHandler(w http.ResponseWriter, r *http.Request) {
  90. if !validArchivePath.MatchString(r.URL.Path) {
  91. http.NotFound(w, r)
  92. return
  93. }
  94. list, err:=loadList()
  95. if err != nil {
  96. http.Error(w, err.Error(), http.StatusInternalServerError)
  97. return
  98. }
  99. ////
  100. type Page struct {
  101. PostList []*PostItem
  102. Path string
  103. }
  104. tmpl := template.Must(template.New("archive.html").ParseFiles("templates/archive.html", "templates/menu.html"))
  105. p:=Page{PostList: list, Path: path}
  106. err2 := tmpl.ExecuteTemplate(w, "archive.html", p)
  107. if err2 != nil {
  108. http.Error(w, err2.Error(), http.StatusInternalServerError)
  109. }
  110. ////
  111. }
  112. func main() {
  113. http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css"))))
  114. http.HandleFunc("/archive", archiveHandler)
  115. http.HandleFunc("/archive/", archiveHandler)
  116. http.HandleFunc("/", homeHandler)
  117. http.ListenAndServe(":8106", nil)
  118. }
  119. type PostItem struct {
  120. Date string `json:"date"`
  121. Title string `json:"title"`
  122. Url string `json:"url"`
  123. }
  124. // function to load the json list of posts
  125. func loadList() ([]*PostItem, error) {
  126. posts, err := os.Open("posts.json")
  127. defer func() {
  128. if err := posts.Close(); err != nil {
  129. panic(err)
  130. }
  131. }()
  132. if err != nil {
  133. return nil, err
  134. }
  135. var list []*PostItem
  136. json.NewDecoder(posts).Decode(&list)
  137. return list, nil
  138. }
  139. func loadPost(url string) (string, error) {
  140. filename := "./posts/"+url+".md"
  141. rawContent, err:= ioutil.ReadFile(filename)
  142. if err != nil {
  143. return "", err
  144. }
  145. content:=blackfriday.MarkdownCommon(rawContent)
  146. return string(content), nil
  147. }
  148. /*
  149. type Page struct {
  150. Content template.HTML //Content string
  151. }
  152. func main() {
  153. s := "<p>Hello!</p>"
  154. t, err := template.New("foo").Parse(`before {{.Content}} after`)
  155. var buf bytes.Buffer
  156. err = t.ExecuteTemplate(&buf, "foo", Page{Content: template.HTML(s)})
  157. //err = t.ExecuteTemplate(&buf, "foo", Page{Content: s})
  158. if err != nil {
  159. fmt.Println("template error:", err)
  160. }
  161. fmt.Println(string(buf.Bytes()))
  162. }
  163. func markDowner(args ...interface{}) template.HTML {
  164. s := blackfriday.MarkdownCommon([]byte(fmt.Sprintf("%s", args...)))
  165. return template.HTML(s)
  166. }
  167. tmpl := template.Must(template.New("page.html").Funcs(template.FuncMap{"markDown": markDowner}).ParseFiles("page.html"))
  168. err := tmpl.ExecuteTemplate(w, "page.html", p)
  169. if err != nil {
  170. http.Error(w, err.Error(), http.StatusInternalServerError)
  171. }
  172. {{.Body | markDown}}
  173. */