/
run.go
92 lines (82 loc) · 2.15 KB
/
run.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Binary run runs a simple wiki server.
package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"github.com/jaeyeom/gofiletable/table"
"github.com/jaeyeom/orgmode-wiki/parser"
)
var (
addr = flag.String("addr", ":8000", "address of server")
dbPath = flag.String("db_path", "/tmp/wiki-db/", "Path to wiki db")
)
var wikiTable *table.Table
type document struct {
Title string
Content string
ContentHTML template.HTML
}
// viewHandler is view/edit page handler.
func viewHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
// Index page
http.Redirect(w, r, "/view/Main", http.StatusSeeOther)
return
}
action := r.URL.Path[1:5] // view or edit
title := r.URL.Path[6:]
if r.Method != "GET" || title == "" {
return
}
content, _ := wikiTable.Get([]byte(title))
if len(content) == 0 && action == "view" {
// Index page
http.Redirect(w, r, "/edit/"+title, http.StatusSeeOther)
return
}
w.Header().Set("Content-Type", "text/html")
t, _ := template.ParseFiles(fmt.Sprintf("template/%s.html", action))
p := parser.Parser{}
p.Parse(bytes.NewBuffer(content))
contentHTML := &bytes.Buffer{}
p.Write(parser.NewHTMLWriter(contentHTML), false)
t.Execute(w, document{
Title: title,
Content: string(content),
ContentHTML: template.HTML(contentHTML.String()),
})
}
// modifyHandler is modify action handler.
func modifyHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[8:]
if r.Method != "POST" || title == "" {
return
}
content := r.PostFormValue("content")
if err := wikiTable.Put([]byte(title), []byte(content)); err != nil {
log.Println(err)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusSeeOther)
}
func main() {
flag.Parse()
var err error
wikiTable, err = table.Create(table.TableOption{*dbPath, nil, true})
if err != nil {
log.Println(err)
return
}
http.HandleFunc("/", viewHandler)
http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/edit/", viewHandler)
http.HandleFunc("/modify/", modifyHandler)
log.Printf("Server being ready: %s\n", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Println(err)
}
}