From 456ad4333da1ea9dd2249979dd9e57009f264d33 Mon Sep 17 00:00:00 2001 From: sirkrypt0 <22522058+sirkrypt0@users.noreply.github.com> Date: Mon, 3 May 2021 10:59:08 +0200 Subject: [PATCH] Make health route unauthenticated --- api/api.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/api/api.go b/api/api.go index 76b8417..a2a0a45 100644 --- a/api/api.go +++ b/api/api.go @@ -24,13 +24,10 @@ const ( // when extracting path parameters. func NewRouter() http.Handler { router := mux.NewRouter() + router.Use(logging.HTTPLoggingMiddleware) // this can later be restricted to a specific host with // `router.Host(...)` and to HTTPS with `router.Schemes("https")` - router = newRouterV1(router) - router.Use(logging.HTTPLoggingMiddleware) - if auth.InitializeAuthentication() { - router.Use(auth.HTTPAuthenticationMiddleware) - } + newRouterV1(router) return router } @@ -39,7 +36,15 @@ func NewRouter() http.Handler { func newRouterV1(router *mux.Router) *mux.Router { v1 := router.PathPrefix(RouteBase).Subrouter() v1.HandleFunc(RouteHealth, Health).Methods(http.MethodGet) + + if auth.InitializeAuthentication() { + // Create new authenticated subrouter. + // All routes added to v1 after this require authentication. + v1 = v1.PathPrefix("").Subrouter() + v1.Use(auth.HTTPAuthenticationMiddleware) + } registerRunnerRoutes(v1.PathPrefix(RouteRunners).Subrouter()) + return v1 }