forked from bittersweet/notifilter-receive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
62 lines (51 loc) · 1.24 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
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"time"
)
func trackTime(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s\n", name, elapsed)
}
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")
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 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)
}