padArchiver/webServer/main.go
webServer/main.goRaw
1package main
2
3import (
4 "bytes"
5 "html/template"
6 "log"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strings"
11
12 padArchiver ".."
13 "github.com/gorilla/mux"
14)
15
16//ItemModel is the model for each one of the files and directories in the directory of the pads
17type ItemModel struct {
18 Name string
19 Path string
20 IsDir bool
21}
22
23//MenuModel is the struct for all the menu, containing Items
24type MenuModel struct {
25 PageTitle string
26 Items []ItemModel
27}
28
29//PageModel is the data model used in the html templates
30type PageModel struct {
31 Title string
32 MenuContent template.HTML
33 Content template.HTML
34}
35
36func main() {
37
38 router := mux.NewRouter()
39 router.HandleFunc("/", getDir).Methods("GET")
40 router.HandleFunc("/dir/{path}", getDir).Methods("GET")
41 router.HandleFunc("/page/{path}", getPage).Methods("GET")
42
43 log.Println("padArchiver web server running")
44 log.Print("port: 8080")
45 log.Fatal(http.ListenAndServe(":8080", router))
46}
47
48func generateMenuItems(dirpath string) MenuModel {
49 var menuPage MenuModel
50 menuPage.PageTitle = "padArchiver - Menu"
51 _ = filepath.Walk(padArchiver.Storage+dirpath, func(path string, f os.FileInfo, err error) error {
52 if path != padArchiver.Storage {
53 path = strings.Replace(path, padArchiver.Storage, "", -1)
54 var item ItemModel
55 item.Name = path
56 path = strings.Replace(path, "/", "%", -1)
57 if f.IsDir() {
58 item.Path = "/dir/" + path
59 } else {
60 item.Path = "/page/" + path
61 }
62 item.IsDir = f.IsDir()
63 menuPage.Items = append(menuPage.Items, item)
64 }
65 return nil
66 })
67 return menuPage
68}
69func generateMenuHTML(dirpath string) template.HTML {
70 menuItems := generateMenuItems(dirpath)
71 tmplMenu := template.Must(template.ParseFiles("templates/menuTemplate.html"))
72 var tpl bytes.Buffer
73 err := tmplMenu.Execute(&tpl, menuItems)
74 check(err)
75 return template.HTML(tpl.String())
76}
77func getDir(w http.ResponseWriter, r *http.Request) {
78 vars := mux.Vars(r)
79 var dirpath string
80 if _, ok := vars["path"]; ok {
81 dirpath = vars["path"]
82 dirpath = strings.Replace(dirpath, "%", "/", -1)
83 }
84 var page PageModel
85 page.Title = dirpath
86 if dirpath == "" {
87 page.Title = "padArchiver"
88 }
89 page.MenuContent = generateMenuHTML(dirpath)
90 content, err := fileToHTML("templates/wellcome.md")
91 check(err)
92 page.Content = template.HTML(content)
93
94 tmpl := template.Must(template.ParseFiles("templates/pageTemplate.html"))
95 tmpl.Execute(w, page)
96}
97
98func getPage(w http.ResponseWriter, r *http.Request) {
99 vars := mux.Vars(r)
100 path := vars["path"]
101 path = strings.Replace(path, "%", "/", -1)
102 path = padArchiver.Storage + path
103
104 content, err := fileToHTML(path)
105 check(err)
106
107 var page PageModel
108 page.Title = strings.Replace(path, padArchiver.Storage, "", -1)
109 page.Content = template.HTML(content)
110
111 page.MenuContent = generateMenuHTML("")
112
113 tmplPage := template.Must(template.ParseFiles("templates/pageTemplate.html"))
114 tmplPage.Execute(w, page)
115}