-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
87 lines (74 loc) · 2.02 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
package main
import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"github.com/gobuffalo/packr"
"github.com/gorilla/mux"
"./assets"
"./manager"
)
type MarkdownsHandler struct {
markdownsManeger *manager.MarkdownsManeger
templatesPath string
}
func (h *MarkdownsHandler) Index(w http.ResponseWriter, req *http.Request) {
t := template.New("index.html")
if h.templatesPath == "" {
templateBody, err := assets.Asset("assets/index.html")
if err != nil {
w.Write([]byte(err.Error()))
}
t, _ = t.Parse(string(templateBody))
} else {
t, _ = t.ParseFiles(fmt.Sprintf("%s%sindex.html", h.templatesPath, string(os.PathSeparator)))
}
t.Execute(w, h.markdownsManeger.GetFileList())
}
func (h *MarkdownsHandler) ReaderHander(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fileName, ok := vars["filename"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
body, err := h.markdownsManeger.GetFile(fileName)
if err != nil {
w.WriteHeader(http.StatusNotFound)
} else {
w.Write(body)
}
}
func (h *MarkdownsHandler) UpdateHandler(w http.ResponseWriter, req *http.Request) {
if h.markdownsManeger.Reflesh() {
w.Write([]byte("success"))
} else {
w.Write([]byte("under refleshing"))
}
}
var (
hander *MarkdownsHandler
TemplateFloder, MarkdownFloder, HttpPort string
DefaultTemplateBox packr.Box
)
func init() {
flag.StringVar(&HttpPort, "p", "8000", "service port")
flag.StringVar(&TemplateFloder, "t", "", "path for the floder containning custom html file,not required")
flag.StringVar(&MarkdownFloder, "f", "markdowns", "the markdows files floder, default: markdows")
}
func main() {
flag.Parse()
m := manager.New(MarkdownFloder)
hander = &MarkdownsHandler{
&m,
TemplateFloder,
}
hander.markdownsManeger.Reflesh()
r := mux.NewRouter()
r.HandleFunc("/", hander.Index)
r.HandleFunc("/file/{filename}/", hander.ReaderHander)
r.HandleFunc("/update", hander.UpdateHandler)
http.ListenAndServe(fmt.Sprintf(":%s", HttpPort), r)
}