-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
78 lines (62 loc) · 2.23 KB
/
router.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/coopernurse/gorp"
"github.com/gorilla/mux"
)
func FileServerRouteG(m *mux.Router, path, dir string) {
m.PathPrefix(path).Handler(
http.StripPrefix(path, http.FileServer(http.Dir(dir))))
}
func indexRoute(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "views/index.html")
}
//fetchLocations takes in a GORP DbMap and fetches all locations in the
//locations database table into a Location slice
func fetchLocations(dbMap *gorp.DbMap) []Location {
var locations []Location
_, err := dbMap.Select(&locations, "SELECT * FROM locations")
if err != nil {
log.Fatal(err)
}
return locations
}
//makeLocationsRoute takes in a GORP DbMap and makes a route that uses that
//DbMap to fetch all locations in the locations table and serve them as JSON.
func makeLocationsRoute(dbMap *gorp.DbMap) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
locations := fetchLocations(dbMap)
locationsJSON, err := json.Marshal(locations)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, "%s", locationsJSON)
}
}
//AddStaticRoutes takes in a Gorilla mux Router and an alternating set of URL
//paths and directory paths and for each pair of strings, the router is given a
//FileServer Handler where the first string is the URL path and the second
//string is the directory path to serve files from.
func AddStaticRoutes(m *mux.Router, pathsAndDirs ...string) {
for i := 0; i < len(pathsAndDirs)-1; i += 2 {
FileServerRouteG(m, pathsAndDirs[i], pathsAndDirs[i+1])
}
}
//initRouter takes in a GORP DbMap and initializes the router's routes while
//using the DbMap to handle database functionality.
func initRouter(dbMap *gorp.DbMap) *mux.Router {
r := mux.NewRouter()
//Add static routes for the public directory
AddStaticRoutes(r, "/partials/", "public/partials",
"/scripts/", "public/scripts", "/styles/", "public/styles",
"/images/", "public/images")
//Add the locations route API with makeLocationsRoute
r.HandleFunc("/locations", makeLocationsRoute(dbMap))
//Serve all other requests with index.html, and ultimately the front-end
//Angular.js app.
r.PathPrefix("/").HandlerFunc(indexRoute)
return r
}