-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (49 loc) · 1.09 KB
/
main.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
package main
import (
"HTML/template"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"path"
"github.com/AWare/awblog/post"
"github.com/codegangsta/negroni"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", renderPost())
n := negroni.Classic()
n.UseHandler(mux)
n.Run(":3000")
}
func renderIndex(rw http.ResponseWriter, r *http.Request) {
files, err := ioutil.ReadDir("markdown/")
if err != nil {
log.Fatal("FILE ERROR")
}
for _, f := range files {
fmt.Fprintf(rw, f.Name(), html.EscapeString(r.URL.Path))
}
}
func renderPost() func(rw http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/index.tmpl")
if err != nil {
log.Fatalln(err)
}
return func(rw http.ResponseWriter, r *http.Request) {
a := path.Clean(r.URL.String())
fmt.Println(a)
if a == "/" {
renderIndex(rw, r)
return
}
p, err := post.NewPostFromFile("markdown/" + a)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
if err := t.Execute(rw, p); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
}