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

Add k8s liveness & readiness #50

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading