Skip to content

Commit 7fd0940

Browse files
committed
feat: add a health check handler (#121)
1 parent 01871fd commit 7fd0940

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SERVER_BINARY = dblab-server
44
CLI_BINARY = dblab
55
GOARCH = amd64
66

7-
VERSION?=0.1
7+
VERSION?=0.3.1
88
BUILD_TIME?=$(shell date -u '+%Y%m%d-%H%M')
99
COMMIT?=no #$(shell git rev-parse HEAD)
1010
BRANCH?=no #$(shell git rev-parse --abbrev-ref HEAD)
@@ -14,10 +14,10 @@ BUILD_DIR=${GOPATH}/${SERVER_BINARY}
1414

1515
# Setup the -ldflags option for go build here, interpolate the variable values
1616
LDFLAGS = -ldflags "-s -w \
17-
-X main.version=${VERSION} \
17+
-X gitlab.com/postgres-ai/database-lab/version.version=${VERSION} \
1818
-X main.commit=${COMMIT} \
1919
-X main.branch=${BRANCH}\
20-
-X main.buildTime=${BUILD_TIME}"
20+
-X gitlab.com/postgres-ai/database-lab/version.buildTime=${BUILD_TIME}"
2121

2222
# Go tooling command aliases
2323
GOBUILD = GO111MODULE=on GOARCH=${GOARCH} go build ${LDFLAGS}

cmd/cli/main.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ import (
1616
"gitlab.com/postgres-ai/database-lab/cmd/cli/commands/snapshot"
1717
"gitlab.com/postgres-ai/database-lab/cmd/cli/templates"
1818
dblabLog "gitlab.com/postgres-ai/database-lab/pkg/log"
19-
)
20-
21-
const (
22-
version = "v0.2.3"
19+
"gitlab.com/postgres-ai/database-lab/version"
2320
)
2421

2522
func main() {
2623
app := &cli.App{
27-
Version: version,
24+
Version: version.GetVersion(),
2825
CommandNotFound: func(c *cli.Context, command string) {
2926
fmt.Fprintf(c.App.Writer, "[ERROR] Command %q not found.\n", command)
3027
},

pkg/srv/routes.go

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package srv
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net/http"
67

@@ -9,8 +10,14 @@ import (
910

1011
"gitlab.com/postgres-ai/database-lab/pkg/client/dblabapi/types"
1112
"gitlab.com/postgres-ai/database-lab/pkg/log"
13+
"gitlab.com/postgres-ai/database-lab/version"
1214
)
1315

16+
// HealthResponse represents a response for heath-check requests.
17+
type HealthResponse struct {
18+
Version string `json:"version"`
19+
}
20+
1421
func (s *Server) getInstanceStatus() http.HandlerFunc {
1522
return func(w http.ResponseWriter, r *http.Request) {
1623
status, err := s.Cloning.GetInstanceState()
@@ -136,3 +143,19 @@ func (s *Server) resetClone() http.HandlerFunc {
136143
log.Dbg(fmt.Sprintf("Clone ID=%s is being reset", cloneID))
137144
}
138145
}
146+
147+
// healthCheck provides a health check handler.
148+
func (s *Server) healthCheck(w http.ResponseWriter, _ *http.Request) {
149+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
150+
151+
healthResponse := HealthResponse{
152+
Version: version.GetVersion(),
153+
}
154+
155+
if err := json.NewEncoder(w).Encode(healthResponse); err != nil {
156+
http.Error(w, err.Error(), http.StatusInternalServerError)
157+
log.Err(err)
158+
159+
return
160+
}
161+
}

pkg/srv/server.go

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func (s *Server) Run() error {
9292
r.HandleFunc("/clone/{id}/reset",
9393
authMW.authorized(s.resetClone())).Methods(http.MethodPost)
9494

95+
// Health check.
96+
r.HandleFunc("/healthz", s.healthCheck).Methods(http.MethodGet)
97+
9598
// Show Swagger UI on index page.
9699
if err := attachAPI(r); err != nil {
97100
log.Err(fmt.Sprintf("Cannot load API description."))

version/version.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
2019 © Postgres.ai
3+
*/
4+
5+
// Package version provides the Database Lab version info.
6+
package version
7+
8+
import (
9+
"fmt"
10+
)
11+
12+
// ldflag variables.
13+
var (
14+
version string
15+
buildTime string
16+
)
17+
18+
// GetVersion return the app version info.
19+
func GetVersion() string {
20+
return fmt.Sprintf("%s-%s", version, buildTime)
21+
}

version/version_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
2019 © Postgres.ai
3+
*/
4+
5+
package version
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestGetVersion(t *testing.T) {
14+
version = "0.0.1"
15+
buildTime = "20200427-0551"
16+
17+
assert.Equal(t, "0.0.1-20200427-0551", GetVersion())
18+
}

0 commit comments

Comments
 (0)