diff --git a/Makefile b/Makefile index 933ea49..b8379f9 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ SWAG ?= go run github.com/swaggo/swag/cmd/swag@v1.16.4 DOCKER ?= docker +COMPOSE ?= $(DOCKER) compose -p go2025-backend GO_ENV += CGO_ENABLED=0 SOURCE := $(shell find . -type f -name '*.go') @@ -15,7 +16,7 @@ swagger: $(SWAG) init -o docs -g cmds/serve.go -pdl 1 docker: $(TARGET) - $(DOCKER) compose up -d --force-recreate --build backend + $(COMPOSE) up -d --force-recreate --build backend $(TARGET): $(SOURCE) $(GO_ENV) go build -o $@ @@ -27,16 +28,19 @@ test: go test -v ./tests -count=1 -failfast postgres: - $(DOCKER) compose exec postgres psql \ + $(COMPOSE) exec postgres psql \ postgres://go2025:go2025@postgres:5432/go2025?sslmode=disable test-ci: docker-quiet test docker-clean docker-quiet: $(TARGET) - $(DOCKER) compose -p go2025-backend up -d \ + $(COMPOSE) up -d \ --force-recreate --build backend \ --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) compose -p go2025-backend down -v --remove-orphans + $(COMPOSE) down -v --remove-orphans diff --git a/cmds/serve.go b/cmds/serve.go index f901b53..46eb525 100644 --- a/cmds/serve.go +++ b/cmds/serve.go @@ -101,6 +101,8 @@ var serveCmd = &cobra.Command{ Use(middlewares.AccessLog). Use(middlewares.CORSHandler) + backend.GET("/healthz", utils.GetHealthz) + apiGroup := backend.NewGroup("/api"). Use(midHandlers.CheckRefreshToken). Use(midHandlers.CheckAccessToken) diff --git a/tests/00_healthz_test.go b/tests/00_healthz_test.go new file mode 100644 index 0000000..99f2adb --- /dev/null +++ b/tests/00_healthz_test.go @@ -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() +} diff --git a/tests/01_login_test.go b/tests/01_login_test.go index 190099f..5d4763f 100644 --- a/tests/01_login_test.go +++ b/tests/01_login_test.go @@ -4,12 +4,8 @@ import ( "net/http" "net/url" "testing" - - "github.com/go-resty/resty/v2" ) -var client *resty.Client - type genLoginUrlPayload struct { LoginUrl string `json:"loginUrl"` } @@ -19,8 +15,6 @@ type loginPayload struct { } func Test_01_Login(t *testing.T) { - client = resty.New() - t.Run("check preshared key failed", func(t *testing.T) { resp, err := client.R(). SetBody(`{"userId": "testuser1"}`). diff --git a/utils/getHealthz.go b/utils/getHealthz.go new file mode 100644 index 0000000..4aac506 --- /dev/null +++ b/utils/getHealthz.go @@ -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) +}