From 57a22f99aade19e3e90a3780d16cb44bbc2b8a9c Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Mon, 6 Jul 2020 16:07:21 -0700 Subject: [PATCH] Add a simple /healthz endpoint - Also remove the old hello world code Signed-off-by: Andrew Keesler --- cmd/placeholder-name/main.go | 8 +++---- go.mod | 2 ++ go.sum | 11 ++++++++++ pkg/handlers/handlers.go | 17 +++++++++++++++ pkg/handlers/healthz_handler.go | 28 +++++++++++++++++++++++++ pkg/handlers/healthz_handler_test.go | 31 ++++++++++++++++++++++++++++ pkg/hello/hello.go | 18 ---------------- pkg/hello/hello_test.go | 17 --------------- 8 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 go.sum create mode 100644 pkg/handlers/handlers.go create mode 100644 pkg/handlers/healthz_handler.go create mode 100644 pkg/handlers/healthz_handler_test.go delete mode 100644 pkg/hello/hello.go delete mode 100644 pkg/hello/hello_test.go diff --git a/cmd/placeholder-name/main.go b/cmd/placeholder-name/main.go index ec7bd5ab..39d548d7 100644 --- a/cmd/placeholder-name/main.go +++ b/cmd/placeholder-name/main.go @@ -6,11 +6,11 @@ SPDX-License-Identifier: Apache-2.0 package main import ( - "fmt" - - "github.com/suzerain-io/placeholder-name/pkg/hello" + "github.com/suzerain-io/placeholder-name/pkg/handlers" + "log" + "net/http" ) func main() { - fmt.Println(hello.NewHelloSayer().SayHello()) + log.Fatal(http.ListenAndServe(":8080", handlers.New())) } diff --git a/go.mod b/go.mod index 390f10c4..6a79f279 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/suzerain-io/placeholder-name go 1.14 + +require github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..afe7890c --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +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/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= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go new file mode 100644 index 00000000..2e722edf --- /dev/null +++ b/pkg/handlers/handlers.go @@ -0,0 +1,17 @@ +/* +Copyright 2020 VMware, Inc. +SPDX-License-Identifier: Apache-2.0 +*/ + +package handlers + +import "net/http" + +const JsonMimeType = "application/json; charset=utf-8" +const HeaderNameContentType = "Content-Type" + +func New() http.Handler { + mux := http.NewServeMux() + mux.Handle("/healthz", newHealthzHandler()) + return mux +} diff --git a/pkg/handlers/healthz_handler.go b/pkg/handlers/healthz_handler.go new file mode 100644 index 00000000..791070f9 --- /dev/null +++ b/pkg/handlers/healthz_handler.go @@ -0,0 +1,28 @@ +/* +Copyright 2020 VMware, Inc. +SPDX-License-Identifier: Apache-2.0 +*/ + +package handlers + +import ( + "encoding/json" + "net/http" +) + +type healthzResponse struct { + Status string `json:"status"` +} + +type healthzHandler struct{} + +func (h healthzHandler) ServeHTTP(responseWriter http.ResponseWriter, _ *http.Request) { + response := healthzResponse{"OK"} + js, _ := json.Marshal(response) + responseWriter.Header().Set(HeaderNameContentType, JsonMimeType) + _, _ = responseWriter.Write(js) +} + +func newHealthzHandler() http.Handler { + return healthzHandler{} +} diff --git a/pkg/handlers/healthz_handler_test.go b/pkg/handlers/healthz_handler_test.go new file mode 100644 index 00000000..b04d0990 --- /dev/null +++ b/pkg/handlers/healthz_handler_test.go @@ -0,0 +1,31 @@ +/* +Copyright 2020 VMware, Inc. +SPDX-License-Identifier: Apache-2.0 +*/ + +package handlers_test + +import ( + "github.com/stretchr/testify/require" + "github.com/suzerain-io/placeholder-name/pkg/handlers" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" +) + +func TestHealthzReturnsOkWithJsonBody(t *testing.T) { + expect := require.New(t) + server := httptest.NewServer(handlers.New()) + defer server.Close() + client := http.Client{} + + response, err := client.Get(server.URL + "/healthz") + + expect.NoError(err) + expect.Equal(http.StatusOK, response.StatusCode) + expect.Equal("application/json; charset=utf-8", response.Header.Get("content-type")) + body, err := ioutil.ReadAll(response.Body) + expect.NoError(err) + expect.JSONEq(`{"status": "OK"}`, string(body)) +} diff --git a/pkg/hello/hello.go b/pkg/hello/hello.go deleted file mode 100644 index 0aecde11..00000000 --- a/pkg/hello/hello.go +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright 2020 VMware, Inc. -SPDX-License-Identifier: Apache-2.0 -*/ - -package hello - -type HelloSayer interface { - SayHello() string -} - -type helloSayerImpl struct{} - -func (helloSayerImpl) SayHello() string { return "hello" } - -func NewHelloSayer() HelloSayer { - return helloSayerImpl{} -} diff --git a/pkg/hello/hello_test.go b/pkg/hello/hello_test.go deleted file mode 100644 index f63a1a96..00000000 --- a/pkg/hello/hello_test.go +++ /dev/null @@ -1,17 +0,0 @@ -/* -Copyright 2020 VMware, Inc. -SPDX-License-Identifier: Apache-2.0 -*/ - -package hello - -import ( - "testing" -) - -func TestHelloSayerImpl_SayHello(t *testing.T) { - actualGreeting := NewHelloSayer().SayHello() - if actualGreeting != "hello" { - t.Errorf("expected to say hello but said %v", actualGreeting) - } -}