-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
127 lines (113 loc) · 2.98 KB
/
handlers.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
package main
import (
"html/template"
"io"
"log"
"net/http"
"os"
"path"
"strings"
)
func viewHandler(w http.ResponseWriter, r *http.Request) {
entries := Store.AllEntries()
tmpl := template.New("base")
template.Must(tmpl.Parse(BaseTmplStr))
template.Must(tmpl.Parse(ViewTmplStr))
tmpl.Execute(w, entries)
}
func cssHandler(w http.ResponseWriter, r *http.Request) {
name := path.Base(r.URL.Path)
asset, ok := AssetMap[name]
if ok {
w.Header().Set("Content-Type", "text/css")
io.WriteString(w, asset)
} else {
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "asset not defined")
}
}
func jsHandler(w http.ResponseWriter, r *http.Request) {
name := path.Base(r.URL.Path)
asset, ok := AssetMap[name]
if ok {
w.Header().Set("Content-Type", "application/javascript")
io.WriteString(w, asset)
} else {
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "asset not defined")
}
}
func newHandler(w http.ResponseWriter, r *http.Request) {
entry := NewEntry("", "", "")
tmpl := template.New("base")
template.Must(tmpl.Parse(BaseTmplStr))
template.Must(tmpl.Parse(AddTmplStr))
tmpl.Execute(w, entry)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
id := path.Base(r.URL.Path)
entry := Store.GetEntry(id)
tmpl := template.New("base")
template.Must(tmpl.Parse(BaseTmplStr))
template.Must(tmpl.Parse(EditTmplStr))
tmpl.Execute(w, entry)
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
err1 := r.ParseMultipartForm(32 << 20)
if err1 != nil {
return
}
file, handler, err := r.FormFile("files")
if err == nil {
defer file.Close()
f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
io.Copy(f, file)
}
entry := NewEntryFromReq(r)
entry.Endpoint = strings.TrimSpace(entry.Endpoint)
replaced := strings.NewReplacer(" ", "%20")
entry.Endpoint = replaced.Replace(entry.Endpoint)
Store.SaveEntry(entry)
Skeddy.ReStart(Store.AllEntries())
http.Redirect(w, r, "/", 301)
}
func addHandler(w http.ResponseWriter, r *http.Request) {
err1 := r.ParseMultipartForm(32 << 20)
if err1 != nil {
return
}
file, handler, err := r.FormFile("files")
if err == nil {
defer file.Close()
f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
io.Copy(f, file)
}
entry := NewEntryFromReq(r)
entry.Endpoint = strings.TrimSpace(entry.Endpoint)
replaced := strings.NewReplacer(" ", "%20")
entry.Endpoint = replaced.Replace(entry.Endpoint)
Store.SaveEntry(entry)
Skeddy.AddEntry(entry)
http.Redirect(w, r, "/", 301)
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
id := path.Base(r.URL.Path)
Store.DeleteEntry(id)
Skeddy.ReStart(Store.AllEntries())
http.Redirect(w, r, "/", 301)
}
func validateExpression(w http.ResponseWriter, r *http.Request) {
expression := path.Base(r.URL.Path)
err := ParseExpression(expression)
if err != nil {
w.Write([]byte(err.Error()))
}
}