diff --git a/cmd/main.go b/cmd/main.go index 0c1c006..52566f0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -38,6 +38,21 @@ func startHTTPServer(address string, grpcServer *server.RegistryServer) error { return http.ListenAndServe(address, mux) } +func startDebugServer(address string) error { + // add /healthz endpoint + mux := runtime.NewServeMux() + + err := mux.HandlePath("GET", "/healthz", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { + w.WriteHeader(http.StatusOK) + }) + + if err != nil { + log.Fatalf("failed to register debug server: %v", err) + } + + return http.ListenAndServe(address, mux) +} + func main() { config.NewLoader().MustLoad() @@ -60,8 +75,15 @@ func main() { } }() - err = startHTTPServer(config.Cfg.Server.HTTP.Addr, registryServer) + go func() { + err = startHTTPServer(config.Cfg.Server.HTTP.Addr, registryServer) + if err != nil { + log.Fatalf("failed to start http server: %v", err) + } + }() + + err = startDebugServer(config.Cfg.Server.Debug.Addr) if err != nil { - log.Fatalf("failed to start http server: %v", err) + log.Fatalf("failed to start debug server: %v", err) } } diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index dec1643..20941d8 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -1,4 +1,4 @@ -version: '3.1' +version: '3.8' services: db: image: postgres:14.7 @@ -9,6 +9,11 @@ services: POSTGRES_USER: pbuf POSTGRES_PASSWORD: pbuf POSTGRES_DB: pbuf_registry + healthcheck: + test: [ "CMD-SHELL", "pg_isready", "-d", "pbuf_registry" ] + interval: 5s + timeout: 10s + retries: 5 pbuf-registry: build: context: . @@ -18,6 +23,12 @@ services: ports: - "8080:8080" - "8081:8081" + - "8082:8082" + healthcheck: + test: wget -O - http://localhost:8082/healthz || exit 1 + interval: 5s + timeout: 10s + retries: 5 environment: DATA_DATABASE_DSN: "postgres://pbuf:pbuf@db:5432/pbuf_registry?sslmode=disable" command: > diff --git a/docker-compose.yml b/docker-compose.yml index 434df84..b438d1d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3.1' +version: '3.8' services: db: image: postgres:14.7 @@ -11,14 +11,25 @@ services: POSTGRES_USER: pbuf POSTGRES_PASSWORD: pbuf POSTGRES_DB: pbuf_registry + healthcheck: + test: [ "CMD-SHELL", "pg_isready", "-d", "pbuf_registry" ] + interval: 5s + timeout: 10s + retries: 5 pbuf-registry: - image: ghcr.io/pbufio/registry:v0.1.1 + image: ghcr.io/pbufio/registry:v0.1.2 restart: always depends_on: - db ports: - "8080:8080" - "8081:8081" + - "8082:8082" + healthcheck: + test: wget -O - http://localhost:8082/healthz || exit 1 + interval: 5s + timeout: 10s + retries: 5 environment: DATA_DATABASE_DSN: "postgres://pbuf:pbuf@db:5432/pbuf_registry?sslmode=disable" command: > diff --git a/internal/config/config.go b/internal/config/config.go index f92c91e..02d55b0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,6 +19,9 @@ type Config struct { GRPC struct { Addr string `mapstructure:"addr"` } + Debug struct { + Addr string `mapstructure:"addr"` + } } Data struct { diff --git a/internal/config/config.yaml b/internal/config/config.yaml index 093a0c6..a98701c 100644 --- a/internal/config/config.yaml +++ b/internal/config/config.yaml @@ -3,6 +3,8 @@ server: addr: 0.0.0.0:8080 grpc: addr: 0.0.0.0:8081 + debug: + addr: 0.0.0.0:8082 data: