/
router.go
59 lines (51 loc) · 1.04 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
package httpServer
import (
"expvar"
"github.com/gorilla/mux"
"github.com/lestrrat-go/apache-logformat"
"io"
"net/http"
)
type HttpRoute struct {
Name string
Method string
Pattern string
HandlerFunc HandlerHandleFunc
}
var httpRoutes = []HttpRoute{
{
"StatsCounts",
"GET",
"/api/v0/Stats/Counts",
HandleStatsCounts,
}, {
"ApiIndex",
"GET",
"/api{Path:.*}",
HandleApiNotFound,
}, {
"expvar",
"GET",
"/debug/vars",
func(env *Environment, w http.ResponseWriter, r *http.Request) Error {
expvar.Handler().ServeHTTP(w, r)
return nil
},
},
}
func newRouter(logger io.Writer, env *Environment) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
// setup normal http routes
for _, route := range httpRoutes {
var handler http.Handler
handler = Handler{Env: env, Handle: route.HandlerFunc}
if logger != nil {
handler = apachelog.CombinedLog.Wrap(handler, logger)
}
router.Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}