/
blog.go
77 lines (68 loc) · 1.75 KB
/
blog.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
package main
import (
"fmt"
"labix.org/v2/mgo"
//"labix.org/v2/mgo/bson"
"net/http"
"github.com/russross/blackfriday"
"github.com/hoisie/mustache"
"io/ioutil"
"strings"
)
type Note struct {
Url string
Title string
Date string
Body string
}
var notes []Note = loadNotes()
var lenPath = len("/note/")
// less than ideal
var home, _ = ioutil.ReadFile("home.md")
var homeMarkup = string(blackfriday.MarkdownCommon(home))
func loadNotes() ([]Note) {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
c := session.DB("test").C("notes")
notes := []Note{}
iter := c.Find(nil).Iter()
err = iter.All(¬es)
if err != nil {
panic(iter.Err())
}
return notes
}
func loadTemplate(name string) string {
file, _ := ioutil.ReadFile(name + ".html")
return string(file)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
var data struct {
Notes []Note
}
data.Notes = notes
rendered := mustache.RenderInLayout(homeMarkup, loadTemplate("home"), data)
fmt.Fprintf(w, rendered)
}
func noteHandler(w http.ResponseWriter, r *http.Request) {
title := strings.Replace(r.URL.Path[lenPath:], ".html", "", -1)
var rendered string
for _, note := range notes {
if note.Url == title {
rendered = mustache.RenderInLayout(note.Body, loadTemplate("note"), note)
}
}
if len(rendered) == 0 {
fmt.Fprintf(w, "404 page not found")
}
fmt.Fprintf(w, rendered)
}
func main() {
http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets"))))
http.HandleFunc("/", indexHandler)
http.HandleFunc("/note/", noteHandler)
http.ListenAndServe(":8080", nil)
}