Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Updated Version Handler to support BuildTime (#7)
Browse files Browse the repository at this point in the history
* Updated Version Handler to support BuildTime

 - Also added some documentation
 - Version information is outputted as JSON

* lint fix
  • Loading branch information
Ketan Umare authored Apr 23, 2019
1 parent 77b9038 commit 12e73dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 14 additions & 1 deletion profutils/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/lyft/flytestdlib/config"

Expand All @@ -28,12 +29,20 @@ const (
contentTypeJSON = "application/json; charset=utf-8"
)

type BuildVersion struct {
Build string `json:"build"`
Version string `json:"version"`
Timestamp time.Time `json:"timestamp,string"`
}

// Writes a string to the Http output stream
func WriteStringResponse(resp http.ResponseWriter, code int, body string) error {
resp.WriteHeader(code)
_, err := resp.Write([]byte(body))
return err
}

// Writes a JSON to the http output stream
func WriteJSONResponse(resp http.ResponseWriter, code int, body interface{}) error {
resp.Header().Set(contentTypeHeader, contentTypeJSON)
resp.WriteHeader(code)
Expand All @@ -44,20 +53,24 @@ func WriteJSONResponse(resp http.ResponseWriter, code int, body interface{}) err
return WriteStringResponse(resp, http.StatusOK, string(j))
}

// Simple healthcheck module that returns OK and provides a simple L7 healthcheck
// TODO we may want to provide a simple function that returns a bool, where users could provide custom healthchecks
func healtcheckHandler(w http.ResponseWriter, req *http.Request) {
err := WriteStringResponse(w, http.StatusOK, http.StatusText(http.StatusOK))
if err != nil {
panic(err)
}
}

// Handler that returns a JSON response indicating the Build Version information (refer to #version module)
func versionHandler(w http.ResponseWriter, req *http.Request) {
err := WriteStringResponse(w, http.StatusOK, fmt.Sprintf("Build [%s], Version [%s]", version.Build, version.Version))
err := WriteJSONResponse(w, http.StatusOK, BuildVersion{Build: version.Build, Version: version.Version, Timestamp: version.BuildTime})
if err != nil {
panic(err)
}
}

// Provides a handler that dumps the config information as a string
func configHandler(w http.ResponseWriter, req *http.Request) {
m, err := config.AllConfigsAsMap(config.GetRootSection())
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion profutils/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"net/http"
"testing"
"time"

"github.com/lyft/flytestdlib/version"

"github.com/lyft/flytestdlib/internal/utils"

Expand Down Expand Up @@ -85,9 +88,16 @@ func TestVersionHandler(t *testing.T) {
URL: &testURL,
}

version.BuildTime = time.Now()

http.DefaultServeMux.ServeHTTP(writer, request)
assert.Equal(t, http.StatusOK, writer.Status)
assert.Equal(t, `Build [unknown], Version [unknown]`, string(writer.Body))
assert.NotNil(t, writer.Body)
bv := BuildVersion{}
assert.NoError(t, json.Unmarshal(writer.Body, &bv))
assert.Equal(t, bv.Timestamp.Unix(), version.BuildTime.Unix())
assert.Equal(t, bv.Build, version.Build)
assert.Equal(t, bv.Version, version.Version)
}

func TestHealthcheckHandler(t *testing.T) {
Expand Down

0 comments on commit 12e73dc

Please sign in to comment.