forked from wtg/shuttletracker
/
main.go
55 lines (47 loc) · 1.75 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
package main
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"shuttle_tracking_2/tracking"
)
var (
// Config holds the global app settings.
Config = tracking.InitConfig()
// App holds the application itself.
App = tracking.InitApp(Config)
)
// IndexHandler serves the index page.
func IndexHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
// AdminHandler serves the admin page.
func AdminHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "admin.html")
}
func main() {
// close Mongo session when server terminates
defer App.Session.Close()
// Start auto updater
go App.UpdateShuttles(Config.DataFeed, Config.UpdateInterval)
// Routing
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler).Methods("GET")
r.HandleFunc("/admin", AdminHandler).Methods("GET")
r.HandleFunc("/admin/{*}", AdminHandler).Methods("GET")
r.HandleFunc("/vehicles", App.VehiclesHandler).Methods("GET")
r.HandleFunc("/vehicles/create", App.VehiclesCreateHandler).Methods("POST")
r.HandleFunc("/updates", App.UpdatesHandler).Methods("GET")
r.HandleFunc("/routes", App.RoutesHandler).Methods("GET")
r.HandleFunc("/routes/create", App.RoutesCreateHandler).Methods("POST")
r.HandleFunc("/stops", App.StopsHandler).Methods("GET")
r.HandleFunc("/stops/create", App.StopsCreateHandler).Methods("POST")
// Static files
r.PathPrefix("/bower_components/").Handler(http.StripPrefix("/bower_components/", http.FileServer(http.Dir("bower_components/"))))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
// Serve requests
http.Handle("/", r)
if err := http.ListenAndServe(":8080", r); err != nil {
log.Fatalf("Unable to ListenAndServe: %v", err)
}
}