Add a simple /healthz endpoint

- Also remove the old hello world code

Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
Ryan Richard
2020-07-06 16:07:21 -07:00
committed by Andrew Keesler
parent cc81dd04e9
commit 57a22f99aa
8 changed files with 93 additions and 39 deletions

17
pkg/handlers/handlers.go Normal file
View File

@@ -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
}

View File

@@ -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{}
}

View File

@@ -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))
}

View File

@@ -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{}
}

View File

@@ -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)
}
}