forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
51 lines (39 loc) · 1.1 KB
/
mux.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
package api
import (
"net/http"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/registry"
)
func NewServeMux(reg registry.Registry) http.Handler {
sm := http.NewServeMux()
cAPI := &client.RegistryClient{reg}
prefix := "/v1-alpha"
wireUpDiscoveryResource(sm, prefix)
wireUpMachinesResource(sm, prefix, cAPI)
wireUpStateResource(sm, prefix, cAPI)
wireUpUnitsResource(sm, prefix, cAPI)
sm.HandleFunc(prefix, methodNotAllowedHandler)
sm.HandleFunc("/", baseHandler)
lm := &loggingMiddleware{sm}
return lm
}
type loggingMiddleware struct {
next http.Handler
}
func (lm *loggingMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
log.V(1).Infof("HTTP %s %v", req.Method, req.URL)
lm.next.ServeHTTP(rw, req)
}
func methodNotAllowedHandler(rw http.ResponseWriter, req *http.Request) {
sendError(rw, http.StatusMethodNotAllowed, nil)
}
func baseHandler(rw http.ResponseWriter, req *http.Request) {
var code int
if req.URL.Path == "/" {
code = http.StatusMethodNotAllowed
} else {
code = http.StatusNotFound
}
sendError(rw, code, nil)
}