Commit 7fd09408 authored by Artyom Kartasov's avatar Artyom Kartasov

feat: add a health check handler (#121)

parent 01871fdd
Pipeline #140099420 passed with stages
in 3 minutes and 57 seconds
......@@ -4,7 +4,7 @@ SERVER_BINARY = dblab-server
CLI_BINARY = dblab
GOARCH = amd64
VERSION?=0.1
VERSION?=0.3.1
BUILD_TIME?=$(shell date -u '+%Y%m%d-%H%M')
COMMIT?=no #$(shell git rev-parse HEAD)
BRANCH?=no #$(shell git rev-parse --abbrev-ref HEAD)
......@@ -14,10 +14,10 @@ BUILD_DIR=${GOPATH}/${SERVER_BINARY}
# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS = -ldflags "-s -w \
-X main.version=${VERSION} \
-X gitlab.com/postgres-ai/database-lab/version.version=${VERSION} \
-X main.commit=${COMMIT} \
-X main.branch=${BRANCH}\
-X main.buildTime=${BUILD_TIME}"
-X gitlab.com/postgres-ai/database-lab/version.buildTime=${BUILD_TIME}"
# Go tooling command aliases
GOBUILD = GO111MODULE=on GOARCH=${GOARCH} go build ${LDFLAGS}
......
......@@ -16,15 +16,12 @@ import (
"gitlab.com/postgres-ai/database-lab/cmd/cli/commands/snapshot"
"gitlab.com/postgres-ai/database-lab/cmd/cli/templates"
dblabLog "gitlab.com/postgres-ai/database-lab/pkg/log"
)
const (
version = "v0.2.3"
"gitlab.com/postgres-ai/database-lab/version"
)
func main() {
app := &cli.App{
Version: version,
Version: version.GetVersion(),
CommandNotFound: func(c *cli.Context, command string) {
fmt.Fprintf(c.App.Writer, "[ERROR] Command %q not found.\n", command)
},
......
package srv
import (
"encoding/json"
"fmt"
"net/http"
......@@ -9,8 +10,14 @@ import (
"gitlab.com/postgres-ai/database-lab/pkg/client/dblabapi/types"
"gitlab.com/postgres-ai/database-lab/pkg/log"
"gitlab.com/postgres-ai/database-lab/version"
)
// HealthResponse represents a response for heath-check requests.
type HealthResponse struct {
Version string `json:"version"`
}
func (s *Server) getInstanceStatus() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := s.Cloning.GetInstanceState()
......@@ -136,3 +143,19 @@ func (s *Server) resetClone() http.HandlerFunc {
log.Dbg(fmt.Sprintf("Clone ID=%s is being reset", cloneID))
}
}
// healthCheck provides a health check handler.
func (s *Server) healthCheck(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
healthResponse := HealthResponse{
Version: version.GetVersion(),
}
if err := json.NewEncoder(w).Encode(healthResponse); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Err(err)
return
}
}
......@@ -92,6 +92,9 @@ func (s *Server) Run() error {
r.HandleFunc("/clone/{id}/reset",
authMW.authorized(s.resetClone())).Methods(http.MethodPost)
// Health check.
r.HandleFunc("/healthz", s.healthCheck).Methods(http.MethodGet)
// Show Swagger UI on index page.
if err := attachAPI(r); err != nil {
log.Err(fmt.Sprintf("Cannot load API description."))
......
/*
2019 © Postgres.ai
*/
// Package version provides the Database Lab version info.
package version
import (
"fmt"
)
// ldflag variables.
var (
version string
buildTime string
)
// GetVersion return the app version info.
func GetVersion() string {
return fmt.Sprintf("%s-%s", version, buildTime)
}
/*
2019 © Postgres.ai
*/
package version
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetVersion(t *testing.T) {
version = "0.0.1"
buildTime = "20200427-0551"
assert.Equal(t, "0.0.1-20200427-0551", GetVersion())
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment