Skip to content

Commit

Permalink
healthcheck is added (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
aatarasoff authored Nov 9, 2023
1 parent a50a78d commit 59b1c2f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
26 changes: 24 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
}
}
13 changes: 12 additions & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.1'
version: '3.8'
services:
db:
image: postgres:14.7
Expand All @@ -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: .
Expand All @@ -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: >
Expand Down
15 changes: 13 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.1'
version: '3.8'
services:
db:
image: postgres:14.7
Expand All @@ -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: >
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Config struct {
GRPC struct {
Addr string `mapstructure:"addr"`
}
Debug struct {
Addr string `mapstructure:"addr"`
}
}

Data struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 59b1c2f

Please sign in to comment.