-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (44 loc) · 1.17 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
package main
import (
"log"
"net/http"
"time"
"github.com/3cb/ssc"
"github.com/boltdb/bolt"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
func main() {
// open database and create buckets of key/value pairs
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
err = createBucket(db, "Messages")
if err != nil {
log.Fatal(err)
}
defer db.Close()
pool := ssc.NewPool([]string{}, time.Second*0)
err = pool.Start()
if err != nil {
log.Fatal("Unable to start pool to serve websocket connections.")
}
// start 5 minute polling goroutine
go poll(db, pool)
// create new router instance
r := mux.NewRouter()
// serve static files
r.Handle("/", http.FileServer(http.Dir("./static/")))
r.PathPrefix("/dist/").Handler(http.FileServer(http.Dir("./static/")))
// handle websocket requests
upgrader := &websocket.Upgrader{}
r.Handle("/ws", WS(db, pool, upgrader))
r.Handle("/api/day/{date}", SingleDate(db))
// {date} variable is first day of month
r.Handle("/api/month/{date}", Month())
// {date} variable is first day of year
r.Handle("/api/year/{date}", Year())
// start server
log.Fatal(http.ListenAndServe(":3030", r))
}