forked from bittersweet/notifilter-receive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
125 lines (102 loc) · 2.81 KB
/
endpoints.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"time"
"github.com/jmoiron/sqlx/types"
)
func trackTime(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s\n", name, elapsed)
}
func handleFavicon(w http.ResponseWriter, r *http.Request) {
defer trackTime(time.Now(), "handleFavicon")
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
defer trackTime(time.Now(), "handleIndex")
t, err := template.ParseFiles("templates/index.html")
if err != nil {
log.Fatal("ParseFiles", err)
}
notifiers := []Notifier{}
err = db.Select(¬ifiers, "SELECT * FROM notifiers")
if err != nil {
log.Fatal("db.Select notifiers ", err)
}
incoming := []Incoming{}
err = db.Select(&incoming, "SELECT * FROM incoming ORDER BY id DESC LIMIT 10")
if err != nil {
log.Fatal("db.Select incoming ", err)
}
data := map[string]interface{}{
"notifiers": notifiers,
"incoming": incoming,
}
err = t.Execute(w, data)
if err != nil {
log.Fatal("t.Execute", err)
}
}
func handleNew(w http.ResponseWriter, r *http.Request) {
defer trackTime(time.Now(), "handleNew")
classes := []string{}
err := db.Select(&classes, "SELECT distinct(class) FROM incoming")
if err != nil {
log.Fatal("db.Select incoming ", err)
}
t, err := template.ParseFiles("templates/new.html")
if err != nil {
log.Fatal("ParseFiles", err)
}
data := map[string]interface{}{
"classes": classes,
}
err = t.Execute(w, data)
if err != nil {
log.Fatal("t.Execute", err)
}
}
func handleCreate(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
defer trackTime(time.Now(), "handleCreateRule")
err := r.ParseForm()
if err != nil {
log.Fatal("handleCreateRule", err)
}
fmt.Println("incoming parameters")
fmt.Printf("%v\n", r.Form)
notification_type := r.Form.Get("notification_type")
class := r.Form.Get("class")
template := r.Form.Get("template")
rules := r.Form.Get("rules")
_, err = db.NamedExec(`INSERT INTO notifiers (notification_type, class, template, rules) VALUES (:notification_type, :class, :template, :rules)`,
map[string]interface{}{
"notification_type": notification_type,
"class": class,
"template": template,
"rules": types.JsonText(rules),
})
if err != nil {
log.Println("ERROR: insert named query", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
func handleCount(w http.ResponseWriter, r *http.Request) {
defer trackTime(time.Now(), "handleCount")
count := countRows()
jmap := map[string]int{
"status": 200,
"count": count,
}
output, err := json.MarshalIndent(jmap, "", " ")
if err != nil {
log.Fatal("MarshalIndent", err)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(output)
}