diff --git a/cmd/iapap/iapap.go b/cmd/iapap/iapap.go index 60862dc..822cd18 100644 --- a/cmd/iapap/iapap.go +++ b/cmd/iapap/iapap.go @@ -9,6 +9,8 @@ import ( "os" "strings" + "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" "github.com/kpurdon/iapap/pkg/iapap" ) @@ -34,11 +36,14 @@ func main() { log.Panicf("target %q is an invalid url: %s", target, err) } + r := chi.NewRouter() + r.Use(middleware.Logger) + // provide healthcheck endpoints, use _* to avoid common whitelisted endpoints - http.HandleFunc("/_liveness", func(w http.ResponseWriter, r *http.Request) { + r.Get("/_liveness", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) - http.HandleFunc("/_readiness", func(w http.ResponseWriter, r *http.Request) { + r.Get("/_readiness", func(w http.ResponseWriter, r *http.Request) { // TODO: check if target is available w.WriteHeader(http.StatusOK) }) @@ -50,13 +55,13 @@ func main() { if !strings.HasPrefix(e, "/") { log.Panicf("whitelisted endpoint %q does not begin with a /", e) } - http.Handle(e, proxy) + r.Handle(e, proxy) } // for all other endpoints, provide an authenticated proxy - http.Handle("/*", iapap.NewVerifier(audience).Apply(proxy)) + r.Handle("/*", iapap.NewVerifier(audience).Apply(proxy)) addr := net.JoinHostPort("", port) log.Printf("iapap listening on %s", addr) - log.Panic(http.ListenAndServe(addr, nil)) + log.Panic(http.ListenAndServe(addr, r)) } diff --git a/go.mod b/go.mod index 26f0e72..a3606b6 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.12 require ( github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 + github.com/go-chi/chi v1.5.0 github.com/stretchr/testify v1.6.1 ) diff --git a/go.sum b/go.sum index 8f0014c..c7f46bd 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU= github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4= +github.com/go-chi/chi v1.5.0 h1:2ZcJZozJ+rj6BA0c19ffBUGXEKAT/aOLOtQjD46vBRA= +github.com/go-chi/chi v1.5.0/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=