/
routes.go
36 lines (30 loc) · 954 Bytes
/
routes.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
package routes
import (
"github.com/gillesdemey/npm-registry/storage"
"github.com/unrolled/render"
"github.com/urfave/negroni"
"golang.org/x/net/context"
"net/http"
)
func Root(w http.ResponseWriter, req *http.Request) {
render := RendererFromContext(req.Context())
render.Text(w, http.StatusOK, "Running npm registry")
}
func Noop(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNoContent)
}
func StorageFromContext(c context.Context) storage.Engine {
return c.Value("storage").(storage.Engine)
}
func RendererFromContext(c context.Context) *render.Render {
return c.Value("renderer").(*render.Render)
}
// CreateMiddleware takes a variable number of negroni.HandlerFuncs and returns a single
// http.HandlerFunc to pass to gorilla.pat
func CreateMiddleware(handlers ...negroni.HandlerFunc) http.HandlerFunc {
n := negroni.New()
for _, handlerFunc := range handlers {
n.Use(handlerFunc)
}
return n.ServeHTTP
}