From f7981715db2ea1b1a6e9f084403ac1639b7cefff Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 3 Jun 2023 20:16:41 -0400 Subject: [PATCH] Fix healthcheck --- cmd/api.go | 29 +++++++++++++++++------------ data/data.go | 13 +++++++++++++ docker-compose.yaml | 2 ++ handlers/health.go | 23 +++++++++++++++++++++-- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/cmd/api.go b/cmd/api.go index 397d2cc..e7cefd4 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -5,6 +5,7 @@ import ( "fmt" "log" + "git.agecem.com/agecem/bottin-agenda/data" "git.agecem.com/agecem/bottin-agenda/handlers" bottindata "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" @@ -27,16 +28,6 @@ var apiCmd = &cobra.Command{ apiKey = viper.GetString("api.key") apiPort = viper.GetInt("api.port") - /* - dbConnection := data.PostgresConnection{ - User: viper.GetString("db.user"), - Password: viper.GetString("db.password"), - Host: viper.GetString("db.host"), - Database: viper.GetString("db.database"), - Port: viper.GetInt("db.port"), - } - */ - bottinApiKey := viper.GetString("bottin.api.key") bottinApiHost := viper.GetString("bottin.api.host") bottinApiProtocol := viper.GetString("bottin.api.protocol") @@ -72,10 +63,24 @@ var apiCmd = &cobra.Command{ bottinHealthResponse, err := bottinConnection.GetHealth() if err != nil { - log.Fatalf("bottinConnection.GetHealth(): %s", err) + log.Fatalf("[bottin] bottinConnection.GetHealth(): %s", err) } - log.Println(bottinHealthResponse) + log.Println("[bottin] ok: ", bottinHealthResponse) + + // Check database is ready + + dataClient, err := data.NewDataClientFromViper() + if err != nil { + log.Fatalf("[bottin-agenda db] data.NewDataclientFromViper(): %s", err) + } + defer dataClient.DB.Close() + + if err := dataClient.DB.Ping(); err != nil { + log.Fatalf("[bottin-agenda db] dataClient.DB.Ping(): %s", err) + } else { + log.Println("[bottin-agenda db] ok") + } // Execution diff --git a/data/data.go b/data/data.go index f71ff65..26dacbd 100644 --- a/data/data.go +++ b/data/data.go @@ -6,6 +6,7 @@ import ( "git.agecem.com/agecem/bottin-agenda/models" _ "github.com/jackc/pgx/stdlib" "github.com/jmoiron/sqlx" + "github.com/spf13/viper" ) // DataClient is a postgres client based on sqlx @@ -23,6 +24,18 @@ type PostgresConnection struct { SSL bool } +func NewDataClientFromViper() (*DataClient, error) { + client, err := NewDataClient(PostgresConnection{ + User: viper.GetString("db.user"), + Password: viper.GetString("db.password"), + Host: viper.GetString("db.host"), + Port: viper.GetInt("db.port"), + Database: viper.GetString("db.database"), + }) + + return client, err +} + func NewDataClient(connection PostgresConnection) (*DataClient, error) { client := &DataClient{PostgresConnection: connection} diff --git a/docker-compose.yaml b/docker-compose.yaml index d0ae628..635d0c8 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,6 +6,8 @@ services: POSTGRES_DATABASE: "${BOTTINAGENDA_POSTGRES_DATABASE}" POSTGRES_PASSWORD: "${BOTTINAGENDA_POSTGRES_PASSWORD}" POSTGRES_USER: "${BOTTINAGENDA_POSTGRES_USER}" + ports: + - '5433:5432' volumes: - 'db-data:/var/lib/postgresql/data' restart: 'unless-stopped' diff --git a/handlers/health.go b/handlers/health.go index 9364932..47d8177 100644 --- a/handlers/health.go +++ b/handlers/health.go @@ -3,6 +3,7 @@ package handlers import ( "net/http" + "git.agecem.com/agecem/bottin-agenda/data" bottindata "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" "github.com/spf13/viper" @@ -30,8 +31,26 @@ func GetHealth(c echo.Context) error { bottinStatus = healthResponse } + var databaseStatus string + + // Check database is ready + + dataClient, err := data.NewDataClientFromViper() + if err != nil { + databaseStatus = err.Error() + } else { + defer dataClient.DB.Close() + + if err := dataClient.DB.Ping(); err != nil { + databaseStatus = err.Error() + } else { + databaseStatus = "Bottin-agenda database is ready" + } + } + return c.JSON(http.StatusOK, map[string]string{ - "message": "Bottin-agenda API v2 is ready", - "bottin": bottinStatus, + "message": "Bottin-agenda API v2 is ready", + "bottin": bottinStatus, + "database": databaseStatus, }) }