Skip to content

Commit

Permalink
Merge pull request #103 from silasdavis/expose-routing
Browse files Browse the repository at this point in the history
Expose public and private routes for extending and overriding
  • Loading branch information
cainlevy committed May 21, 2019
2 parents 7d9de2b + e815df1 commit 7f82ad8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/route/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
prometheus.MustRegister(httpTimings)
}

func instrumentRoute(name string, next http.Handler) http.Handler {
func InstrumentRoute(name string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
metrics := httpsnoop.CaptureMetrics(next, w, r)
httpRequests.WithLabelValues(name, strconv.Itoa(metrics.Code)).Inc()
Expand Down
22 changes: 13 additions & 9 deletions lib/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ type SecurityHandler func(http.Handler) http.Handler

// Post creates a new POST route. A security handler must be registered next.
func Post(tpl string) *Route {
return &Route{verb: "POST", tpl: tpl}
return &Route{Verb: "POST", Tpl: tpl}
}

// Get creates a new GET route. A security handler must be registered next.
func Get(tpl string) *Route {
return &Route{verb: "GET", tpl: tpl}
return &Route{Verb: "GET", Tpl: tpl}
}

// Delete creates a new DELETE route. A security handler must be registered next.
func Delete(tpl string) *Route {
return &Route{verb: "DELETE", tpl: tpl}
return &Route{Verb: "DELETE", Tpl: tpl}
}

// Patch creates a new PATCH route. A security handler must be registered next.
func Patch(tpl string) *Route {
return &Route{verb: "PATCH", tpl: tpl}
return &Route{Verb: "PATCH", Tpl: tpl}
}

// Route is an incomplete Route comprising only verb and path (as a gorilla/mux template). It must
// next be `SecuredWith`.
type Route struct {
verb string
tpl string
Verb string
Tpl string
}

// SecuredWith registers a security handler for a route. A handler must be registered next.
Expand All @@ -64,13 +64,17 @@ type HandledRoute struct {
handler http.Handler
}

func (hr *HandledRoute) ServeHTTP(response http.ResponseWriter, request *http.Request) {
hr.security(hr.handler).ServeHTTP(response, request)
}

// Attach is the adapter for adding HandledRoutes to a gorilla/mux Router.
func Attach(router *mux.Router, pathPrefix string, routes ...*HandledRoute) {
for _, r := range routes {
router.
PathPrefix(pathPrefix).
Methods(r.verb).
Path(r.tpl).
Handler(instrumentRoute(r.verb+" "+r.tpl, r.security(r.handler)))
Methods(r.Verb).
Path(r.Tpl).
Handler(InstrumentRoute(r.Verb+" "+r.Tpl, r))
}
}
2 changes: 1 addition & 1 deletion server/private_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func privateRoutes(app *app.App) []*route.HandledRoute {
func PrivateRoutes(app *app.App) []*route.HandledRoute {
var routes []*route.HandledRoute
authentication := route.BasicAuthSecurity(app.Config.AuthUsername, app.Config.AuthPassword, "Private AuthN Realm")

Expand Down
2 changes: 1 addition & 1 deletion server/public_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/keratin/authn-server/server/handlers"
)

func publicRoutes(app *app.App) []*route.HandledRoute {
func PublicRoutes(app *app.App) []*route.HandledRoute {
var routes []*route.HandledRoute
originSecurity := route.OriginSecurity(app.Config.ApplicationDomains)

Expand Down
6 changes: 3 additions & 3 deletions server/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (

func Router(app *app.App) http.Handler {
r := mux.NewRouter()
route.Attach(r, app.Config.MountedPath, privateRoutes(app)...)
route.Attach(r, app.Config.MountedPath, publicRoutes(app)...)
route.Attach(r, app.Config.MountedPath, PrivateRoutes(app)...)
route.Attach(r, app.Config.MountedPath, PublicRoutes(app)...)

return wrapRouter(r, app)
}

func PublicRouter(app *app.App) http.Handler {
r := mux.NewRouter()
route.Attach(r, app.Config.MountedPath, publicRoutes(app)...)
route.Attach(r, app.Config.MountedPath, PublicRoutes(app)...)

return wrapRouter(r, app)
}
Expand Down

0 comments on commit 7f82ad8

Please sign in to comment.