From 346743176b34dcffa75cdbf4c7eb95b4943d82b6 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 21 May 2019 13:29:58 -0700 Subject: [PATCH] separate healthz from other routes --- cmd/drone-server/inject_server.go | 12 +++++++++++- cmd/drone-server/wire_gen.go | 7 ++++--- handler/{web/healthz.go => health/health.go} | 19 ++++++++++++++++--- .../healthz_test.go => health/health_test.go} | 4 ++-- handler/web/web.go | 1 - 5 files changed, 33 insertions(+), 10 deletions(-) rename handler/{web/healthz.go => health/health.go} (70%) rename handler/{web/healthz_test.go => health/health_test.go} (90%) diff --git a/cmd/drone-server/inject_server.go b/cmd/drone-server/inject_server.go index eaedbda3d..9df619b04 100644 --- a/cmd/drone-server/inject_server.go +++ b/cmd/drone-server/inject_server.go @@ -20,6 +20,7 @@ import ( "github.com/drone/drone/cmd/drone-server/config" "github.com/drone/drone/core" "github.com/drone/drone/handler/api" + "github.com/drone/drone/handler/health" "github.com/drone/drone/handler/web" "github.com/drone/drone/metric" "github.com/drone/drone/operator/manager" @@ -44,6 +45,7 @@ var serverSet = wire.NewSet( manager.New, api.New, web.New, + provideHealthz, provideMetric, provideRouter, provideRPC, @@ -54,8 +56,9 @@ var serverSet = wire.NewSet( // provideRouter is a Wire provider function that returns a // router that is serves the provided handlers. -func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpcHandlerV2, metrics *metric.Server) *chi.Mux { +func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpcHandlerV2, healthz healthzHandler, metrics *metric.Server) *chi.Mux { r := chi.NewRouter() + r.Mount("/healthz", healthz) r.Mount("/metrics", metrics) r.Mount("/api", api.Handler()) r.Mount("/rpc/v2", rpcv2) @@ -64,6 +67,13 @@ func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpc return r } +// provideMetric is a Wire provider function that returns the +// healthcheck server. +func provideHealthz() healthzHandler { + v := health.New() + return healthzHandler(v) +} + // provideMetric is a Wire provider function that returns the // metrics server exposing metrics in prometheus format. func provideMetric(session core.Session, config config.Config) *metric.Server { diff --git a/cmd/drone-server/wire_gen.go b/cmd/drone-server/wire_gen.go index 8d92c8ffc..3e24ca725 100644 --- a/cmd/drone-server/wire_gen.go +++ b/cmd/drone-server/wire_gen.go @@ -91,10 +91,11 @@ func InitializeApplication(config2 config.Config) (application, error) { middleware := provideLogin(config2) options := provideServerOptions(config2) webServer := web.New(admissionService, buildStore, client, hookParser, coreLicense, licenseService, middleware, repositoryStore, session, syncer, triggerer, userStore, userService, webhookSender, options, system) - handler := provideRPC(buildManager, config2) - rpc2Server := provideRPC2(buildManager, config2) + mainRpcHandlerV1 := provideRPC(buildManager, config2) + mainRpcHandlerV2 := provideRPC2(buildManager, config2) + mainHealthzHandler := provideHealthz() metricServer := provideMetric(session, config2) - mux := provideRouter(server, webServer, handler, rpc2Server, metricServer) + mux := provideRouter(server, webServer, mainRpcHandlerV1, mainRpcHandlerV2, mainHealthzHandler, metricServer) serverServer := provideServer(mux, config2) mainApplication := newApplication(cronScheduler, datadog, runner, serverServer, userStore) return mainApplication, nil diff --git a/handler/web/healthz.go b/handler/health/health.go similarity index 70% rename from handler/web/healthz.go rename to handler/health/health.go index 61c3d919c..920728c64 100644 --- a/handler/web/healthz.go +++ b/handler/health/health.go @@ -12,19 +12,32 @@ // See the License for the specific language governing permissions and // limitations under the License. -package web +package health import ( "io" "net/http" + + "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" ) -// HandleHealthz creates an http.HandlerFunc that performs system +// New returns a new health check router. +func New() http.Handler { + r := chi.NewRouter() + r.Use(middleware.Recoverer) + r.Use(middleware.NoCache) + r.Handle("/", Handler()) + return r +} + +// Handler creates an http.HandlerFunc that performs system // healthchecks and returns 500 if the system is in an unhealthy state. -func HandleHealthz() http.HandlerFunc { +func Handler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain") io.WriteString(w, "OK") } } + diff --git a/handler/web/healthz_test.go b/handler/health/health_test.go similarity index 90% rename from handler/web/healthz_test.go rename to handler/health/health_test.go index 87281541c..1b8ec4411 100644 --- a/handler/web/healthz_test.go +++ b/handler/health/health_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by the Drone Non-Commercial License // that can be found in the LICENSE file. -package web +package health import ( "net/http/httptest" @@ -13,7 +13,7 @@ func TestHandleHealthz(t *testing.T) { w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/healthz", nil) - HandleHealthz().ServeHTTP(w, r) + Handler().ServeHTTP(w, r) if got, want := w.Code, 200; want != got { t.Errorf("Want response code %d, got %d", want, got) diff --git a/handler/web/web.go b/handler/web/web.go index 000475731..b507799d4 100644 --- a/handler/web/web.go +++ b/handler/web/web.go @@ -102,7 +102,6 @@ func (s Server) Handler() http.Handler { }) r.Get("/version", HandleVersion) - r.Get("/healthz", HandleHealthz()) r.Get("/varz", HandleVarz(s.Client, s.License)) r.Handle("/login",