-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
50 lines (43 loc) · 1.13 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
package confmgr
import (
log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/moensch/confmgr/backends"
"net/http"
"time"
)
func (c *ConfMgr) NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range c.RouteDefinitions() {
var handler http.Handler
handler = route.HandlerFunc
handler = c.ClientHandler(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
const ReqScope = 0
type HandlerFuncBackend func(w http.ResponseWriter, r *http.Request, b backend.ConfigBackend)
func handlerDecorate(f HandlerFuncBackend) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
scope := ScopeFromHeaders(r.Header)
context.Set(r, ReqScope, scope)
b := BackendFactory.NewBackend()
defer b.Close()
f(w, r, b)
log.WithFields(log.Fields{
"method": r.Method,
"uri": r.RequestURI,
"client": r.RemoteAddr,
"time": time.Since(start),
"scope": scope,
}).Info("Request")
context.Clear(r)
})
}