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:
parent
cc81dd04e9
commit
57a22f99aa
@ -6,11 +6,11 @@ SPDX-License-Identifier: Apache-2.0
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/suzerain-io/placeholder-name/pkg/handlers"
|
||||||
|
"log"
|
||||||
"github.com/suzerain-io/placeholder-name/pkg/hello"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(hello.NewHelloSayer().SayHello())
|
log.Fatal(http.ListenAndServe(":8080", handlers.New()))
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
|||||||
module github.com/suzerain-io/placeholder-name
|
module github.com/suzerain-io/placeholder-name
|
||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
|
require github.com/stretchr/testify v1.6.1
|
||||||
|
11
go.sum
Normal file
11
go.sum
Normal file
@ -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=
|
17
pkg/handlers/handlers.go
Normal file
17
pkg/handlers/handlers.go
Normal 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
|
||||||
|
}
|
28
pkg/handlers/healthz_handler.go
Normal file
28
pkg/handlers/healthz_handler.go
Normal 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{}
|
||||||
|
}
|
31
pkg/handlers/healthz_handler_test.go
Normal file
31
pkg/handlers/healthz_handler_test.go
Normal 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))
|
||||||
|
}
|
@ -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{}
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user