/
server.go
60 lines (49 loc) · 1.23 KB
/
server.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
package main
import (
"github.com/gorilla/mux"
"github.com/howeyc/fsnotify"
"html/template"
"log"
"net/http"
)
//reload templates on modify
func listenForChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
//recompile templates
templates = template.Must(template.ParseGlob("templates/*.html"))
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
err = watcher.Watch("templates/")
if err != nil {
log.Fatal(err)
}
<-done
watcher.Close()
}
func main() {
go listenForChanges()
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
r := mux.NewRouter()
r.HandleFunc("/api/events", handleApiAllEvents).Methods("GET")
r.HandleFunc("/api/events", handleApiEventCreate).Methods("PUT")
r.HandleFunc("/api/events/{id}", handleApiEventDestroy).Methods("DELETE")
r.HandleFunc("/api/events/{id}", handleApiEvent).Methods("GET")
r.HandleFunc("/", handleHome)
r.HandleFunc("/events/{name}", handleEvent)
r.HandleFunc("/api", handleApi)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}