-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (67 loc) · 2.08 KB
/
main.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
package main
import (
"html/template"
"log"
"net/http"
"suy.io/bots/examples/web/bots"
)
var templates *template.Template
var examples = []struct{ Name, Link string }{
{"echo", "/echo"},
{"threads", "/threads"},
{"attachments", "/attachments"},
{"conversations", "/conversations"},
}
func main() {
var err error
templates, err = template.ParseGlob("./tmpl/*.tmpl")
if err != nil {
log.Fatal(err)
}
templates, err = templates.ParseGlob("./tmpl/partials/*.tmpl")
if err != nil {
log.Fatal(err)
}
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static"))))
e, err := bots.NewEchoBots(templates, bots.PageContext{"echo", "/static/js/lib/echo.js"})
if err != nil {
log.Fatal(err)
}
http.Handle("/echo", e)
http.HandleFunc("/echo_chat", e.ConnectionHandler())
c, err := bots.NewConversationBots(templates, bots.PageContext{"echo", "/static/js/lib/conversations.js"})
if err != nil {
log.Fatal(err)
}
http.Handle("/conversations", c)
http.HandleFunc("/conversations_chat", c.ConnectionHandler())
t, err := bots.NewThreadBots(templates, bots.PageContext{"echo", "/static/js/lib/threads.js"})
if err != nil {
log.Fatal(err)
}
http.Handle("/threads", t)
http.HandleFunc("/threads_chat", t.ConnectionHandler())
a, err := bots.NewAttachmentBots(templates, bots.PageContext{"echo", "/static/js/lib/attachments.js"})
if err != nil {
log.Fatal(err)
}
http.Handle("/attachments", a)
http.HandleFunc("/attachments_chat", a.ConnectionHandler())
r, err := bots.NewRedisBots("redis:6379", templates, bots.PageContext{"redis", "/static/js/lib/redis.js"})
if err != nil {
log.Println("Not adding redis bots", err)
} else {
http.Handle("/redis", r)
http.HandleFunc("/redis_chat", r.ConnectionHandler())
examples = append(examples, struct{ Name, Link string }{"redis", "/redis"})
}
http.HandleFunc("/", index)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func index(res http.ResponseWriter, req *http.Request) {
templates.ExecuteTemplate(res, "index.tmpl", struct {
Items []struct{ Name, Link string }
}{
Items: examples,
})
}