/
main.go
135 lines (126 loc) · 2.9 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"flag"
"github.com/russross/blackfriday"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
conf = flag.String("f", "markdown.json", "server config")
mimeconf = flag.String("m", "mime.json", "mime config")
)
type Setting struct {
Bind string `json:"bind"`
Port string `json:"port"`
Hosts map[string]string `json:"hosts"`
}
var setting *Setting
var errorTemplate, _ = template.ParseFiles("error.html")
var mime MIMETypes
func main() {
flag.Parse()
reloadchan := make(chan os.Signal, 1)
signal.Notify(reloadchan, syscall.SIGHUP)
go func() {
<-reloadchan
log.Println("reload config")
m := NewMIME(*mimeconf)
if m != nil {
mime = m
}
s := read_condig(*conf)
if s != nil {
setting = s
}
}()
mime = NewMIME(*mimeconf)
if mime == nil {
log.Println("mime config not found")
os.Exit(1)
}
setting = read_condig(*conf)
if setting == nil {
log.Println("server config not found")
os.Exit(1)
}
http.HandleFunc("/", routing)
server := &http.Server{
Addr: setting.Bind + ":" + setting.Port,
Handler: nil,
ReadTimeout: time.Second * 5,
WriteTimeout: time.Second * 5,
}
server.ListenAndServe()
}
//url routing
func routing(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", mime.DetectMIME(r.URL.Path))
if root, ok := setting.Hosts[r.Host]; ok {
root_dir := http.Dir(root)
servermarkdown(w, r, root_dir)
} else {
w.WriteHeader(http.StatusNotFound)
errorTemplate.Execute(w, nil)
}
log.Println(r.RemoteAddr, r.Method, r.RequestURI, r.Host)
}
//returm markdown file
func servermarkdown(w http.ResponseWriter, r *http.Request, root_dir http.Dir) {
file, ismarkdow := get_file(root_dir, r.URL.Path)
if file == nil {
w.WriteHeader(http.StatusNotFound)
error_page, err := template.ParseFiles(string(root_dir) +
"/error.html")
if err == nil {
error_page.Execute(w, nil)
}
} else {
defer file.Close()
body, err := ioutil.ReadAll(file)
if err != nil {
log.Println(err)
return
}
if ismarkdow {
headertemplate, err := template.ParseFiles(
string(root_dir) + "/templates/header.tpl")
if err == nil {
headertemplate.Execute(w, nil)
} else {
log.Println("header error", err)
}
w.Write(blackfriday.MarkdownCommon(body))
footertemplate, err := template.ParseFiles(
string(root_dir) + "/templates/footer.tpl")
if err == nil {
footertemplate.Execute(w, nil)
} else {
log.Println("footer error", err)
}
} else {
w.Write(body)
}
}
}
//try get file
func get_file(root http.Dir, path string) (http.File, bool) {
if path[len(path)-1] == '/' {
path += "index"
}
if fd, err := root.Open(path); err == nil {
return fd, false
}
if fd, err := root.Open(path + ".html"); err == nil {
return fd, false
}
if fd, err := root.Open(path + ".md"); err == nil {
return fd, true
}
return nil, false
}