/
handler.go
78 lines (68 loc) · 1.65 KB
/
handler.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"path"
)
func HomeHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("base")
template.Must(tmpl.Parse(BaseTmpl))
template.Must(tmpl.Parse(ConfigTemp))
nodes := GetScattrData()
tmpl.Execute(w, nodes)
}
func AddHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Fprintf(os.Stderr, "ParseForm failed: %s\n", err.Error())
}
url := r.FormValue("url")
addUrl(url)
http.Redirect(w, r, "/", http.StatusFound)
}
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
id := path.Base(r.URL.Path)
deleteUrl(id)
http.Redirect(w, r, "/", http.StatusFound)
}
func SaveScriptHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Fprintf(os.Stderr, "ParseForm failed: %s\n", err.Error())
}
SavingScript(r.FormValue("script"))
http.Redirect(w, r, "/", http.StatusFound)
}
func CssAssetHandler(w http.ResponseWriter, r *http.Request) {
asset := r.URL.Path[1:]
data, err := Asset(asset)
if err != nil {
log.Println("ERROR:", err)
return
}
w.Header().Set("Content-Type", "text/css")
w.Write(data)
}
func JsAssetHandler(w http.ResponseWriter, r *http.Request) {
asset := r.URL.Path[1:]
data, err := Asset(asset)
if err != nil {
log.Println("ERROR:", err)
return
}
w.Header().Set("Content-Type", "application/javascript")
w.Write(data)
}
func FontsAssetHandler(w http.ResponseWriter, r *http.Request) {
asset := r.URL.Path[1:]
data, err := Asset(asset)
if err != nil {
log.Println("ERROR:", err)
return
}
w.Header().Set("Content-Type", "font/opentype")
w.Write(data)
}