Feat: add healthz
This commit is contained in:
14
Makefile
14
Makefile
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
SWAG ?= go run github.com/swaggo/swag/cmd/swag@v1.16.4
|
SWAG ?= go run github.com/swaggo/swag/cmd/swag@v1.16.4
|
||||||
DOCKER ?= docker
|
DOCKER ?= docker
|
||||||
|
COMPOSE ?= $(DOCKER) compose -p go2025-backend
|
||||||
|
|
||||||
GO_ENV += CGO_ENABLED=0
|
GO_ENV += CGO_ENABLED=0
|
||||||
SOURCE := $(shell find . -type f -name '*.go')
|
SOURCE := $(shell find . -type f -name '*.go')
|
||||||
@@ -15,7 +16,7 @@ swagger:
|
|||||||
$(SWAG) init -o docs -g cmds/serve.go -pdl 1
|
$(SWAG) init -o docs -g cmds/serve.go -pdl 1
|
||||||
|
|
||||||
docker: $(TARGET)
|
docker: $(TARGET)
|
||||||
$(DOCKER) compose up -d --force-recreate --build backend
|
$(COMPOSE) up -d --force-recreate --build backend
|
||||||
|
|
||||||
$(TARGET): $(SOURCE)
|
$(TARGET): $(SOURCE)
|
||||||
$(GO_ENV) go build -o $@
|
$(GO_ENV) go build -o $@
|
||||||
@@ -27,16 +28,19 @@ test:
|
|||||||
go test -v ./tests -count=1 -failfast
|
go test -v ./tests -count=1 -failfast
|
||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
$(DOCKER) compose exec postgres psql \
|
$(COMPOSE) exec postgres psql \
|
||||||
postgres://go2025:go2025@postgres:5432/go2025?sslmode=disable
|
postgres://go2025:go2025@postgres:5432/go2025?sslmode=disable
|
||||||
|
|
||||||
test-ci: docker-quiet test docker-clean
|
test-ci: docker-quiet test docker-clean
|
||||||
|
|
||||||
docker-quiet: $(TARGET)
|
docker-quiet: $(TARGET)
|
||||||
$(DOCKER) compose -p go2025-backend up -d \
|
$(COMPOSE) up -d \
|
||||||
--force-recreate --build backend \
|
--force-recreate --build backend \
|
||||||
--quiet-build --quiet-pull
|
--quiet-build --quiet-pull
|
||||||
sleep 5
|
-for i in seq 10; do \
|
||||||
|
$(COMPOSE) exec postgres docker-ensure-initdb.sh && break; \
|
||||||
|
sleep 1; \
|
||||||
|
done
|
||||||
|
|
||||||
docker-clean:
|
docker-clean:
|
||||||
$(DOCKER) compose -p go2025-backend down -v --remove-orphans
|
$(COMPOSE) down -v --remove-orphans
|
||||||
|
|||||||
@@ -101,6 +101,8 @@ var serveCmd = &cobra.Command{
|
|||||||
Use(middlewares.AccessLog).
|
Use(middlewares.AccessLog).
|
||||||
Use(middlewares.CORSHandler)
|
Use(middlewares.CORSHandler)
|
||||||
|
|
||||||
|
backend.GET("/healthz", utils.GetHealthz)
|
||||||
|
|
||||||
apiGroup := backend.NewGroup("/api").
|
apiGroup := backend.NewGroup("/api").
|
||||||
Use(midHandlers.CheckRefreshToken).
|
Use(midHandlers.CheckRefreshToken).
|
||||||
Use(midHandlers.CheckAccessToken)
|
Use(midHandlers.CheckAccessToken)
|
||||||
|
|||||||
27
tests/00_healthz_test.go
Normal file
27
tests/00_healthz_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var client *resty.Client
|
||||||
|
|
||||||
|
func Test_00_Healthz(t *testing.T) {
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
resp, err := client.R().
|
||||||
|
Get("http://localhost:8080/healthz")
|
||||||
|
if err == nil && resp.StatusCode() == http.StatusOK {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
t.Fatal("server did not ready")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
client = resty.New()
|
||||||
|
}
|
||||||
@@ -4,12 +4,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *resty.Client
|
|
||||||
|
|
||||||
type genLoginUrlPayload struct {
|
type genLoginUrlPayload struct {
|
||||||
LoginUrl string `json:"loginUrl"`
|
LoginUrl string `json:"loginUrl"`
|
||||||
}
|
}
|
||||||
@@ -19,8 +15,6 @@ type loginPayload struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_01_Login(t *testing.T) {
|
func Test_01_Login(t *testing.T) {
|
||||||
client = resty.New()
|
|
||||||
|
|
||||||
t.Run("check preshared key failed", func(t *testing.T) {
|
t.Run("check preshared key failed", func(t *testing.T) {
|
||||||
resp, err := client.R().
|
resp, err := client.R().
|
||||||
SetBody(`{"userId": "testuser1"}`).
|
SetBody(`{"userId": "testuser1"}`).
|
||||||
|
|||||||
13
utils/getHealthz.go
Normal file
13
utils/getHealthz.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/uptrace/bunrouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetHealthz(
|
||||||
|
w http.ResponseWriter, req bunrouter.Request,
|
||||||
|
) error {
|
||||||
|
return Success(w)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user