2015-01-11 08:52:39 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler augments http.HandlerFunc with a context.
|
|
|
|
type HandlerFunc func(*Context, http.ResponseWriter, *http.Request)
|
|
|
|
|
|
|
|
// RouteTable is a routing table from a path to a handlerfunc.
|
|
|
|
type RouteTable map[string]HandlerFunc
|
|
|
|
|
|
|
|
// NewRouter returns a router to be mounted at some mountpoint.
|
2015-01-17 08:03:40 +02:00
|
|
|
func NewRouter() http.Handler {
|
2015-01-11 08:52:39 +02:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
for name, mod := range modules {
|
|
|
|
for route, handler := range mod.Routes() {
|
2015-01-17 08:03:40 +02:00
|
|
|
fmt.Fprintf(cfg.LogWriter, "%-10s Register Route: %s\n", "["+name+"]", route)
|
|
|
|
mux.Handle(path.Join(cfg.MountPath, route), contextRoute{handler})
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
type contextRoute struct {
|
2015-01-17 08:03:40 +02:00
|
|
|
fn HandlerFunc
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c contextRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-01-15 12:56:13 +02:00
|
|
|
ctx, err := ContextFromRequest(r)
|
|
|
|
if err != nil {
|
2015-01-17 08:03:40 +02:00
|
|
|
fmt.Fprintf(cfg.LogWriter, "route: Malformed request, could not create context: %v", err)
|
2015-01-15 12:56:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-17 08:03:40 +02:00
|
|
|
ctx.CookieStorer = cfg.CookieStoreMaker(w, r)
|
|
|
|
ctx.SessionStorer = cfg.SessionStoreMaker(w, r)
|
2015-01-11 08:52:39 +02:00
|
|
|
|
|
|
|
c.fn(ctx, w, r)
|
|
|
|
}
|