Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
Add k8s liveness & readiness (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahelmy authored Jan 28, 2024
1 parent 9ffbc34 commit 696b5a5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ XDev helps with daily development tasks like formatting JSON, base64 encoding /
- [Usage](#usage)
- [Architecture](#architecture)
- [Docker](#docker)
- [Kuberenetes](#kuberenetes)
- [Web UI](#web-ui)
- [Features](#features)
- [Contributing](#contributing)
Expand Down Expand Up @@ -148,7 +149,23 @@ If you want to use `Xdev` as server running in docker container you can use http

- `VERBOSE` To enable verbose mode.


## Kuberenetes

Healthcheck endpoint for liveness & readiness
`GET /api/health`

```
readinessProbe:
httpGet:
path: /api/health
initialDelaySeconds: 10
periodSeconds: 10
livenessProbe:
httpGet:
path: /api/health
initialDelaySeconds: 10
periodSeconds: 10
```

## Web UI

Expand Down
10 changes: 9 additions & 1 deletion api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
)

const (
APIPrefix = "/api"
APIPrefix = "/api"
healthCheckPath = "/health"
)

type Response struct {
Expand All @@ -15,6 +16,7 @@ type Response struct {
}

func AddAPILayer(app *fiber.App) {
healthCheck(app)
uuidAPI(app)
ulidAPI(app)
passwordAPI(app)
Expand All @@ -27,3 +29,9 @@ func AddAPILayer(app *fiber.App) {
timeAPI(app)
propertiesAPI(app)
}

func healthCheck(app *fiber.App) {
app.Get(APIPrefix+healthCheckPath, func(c fiber.Ctx) error {
return c.JSON(Response{Success: true, Message: "OK"})
})
}
28 changes: 28 additions & 0 deletions api/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/assert"
)

func TestHealthCheck(t *testing.T) {
app := fiber.New()
healthCheck(app)
// Create a test request
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
resp, err := app.Test(req)
assert.NoError(t, err)

// Check the response status code
assert.Equal(t, http.StatusOK, resp.StatusCode)

// Check the response body
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.JSONEq(t, `{"success": true, "message": "OK", "data": null}`, string(body))
}

0 comments on commit 696b5a5

Please sign in to comment.