-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrouter.go
60 lines (49 loc) · 1.12 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
package main
import (
"errors"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
func newRouter(hostname string) *mux.Router {
backendUpdates = make(chan triggerAction)
go backendPump()
router := mux.NewRouter()
publicRouter := router.
StrictSlash(true).
Host(hostname).
PathPrefix("/api").
Subrouter()
for ver, jRoutes := range jsonRoutes {
versionRouter := publicRouter.PathPrefix("/" + ver).Subrouter()
for _, r := range jRoutes {
versionRouter.
Methods(r.Method).
Path(r.Pattern).
Name(r.Name).
Handler(httpLogger(r.Handler, r.Name))
}
}
privateRouter := router.
StrictSlash(true).
Host("localhost").
PathPrefix("/trigger").
Subrouter()
for _, r := range triggerRoutes {
privateRouter.
Methods(r.Method).
Path(r.Pattern).
Name(r.Name).
Handler(httpLogger(r.Handler, r.Name))
}
router.NotFoundHandler = http.HandlerFunc(notFound)
return router
}
func notFound(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Cache-Control", "no-store")
err := apiErr(w, errors.New("not Found"))
if err != nil {
log.Println(err)
}
}