Fix healthcheck

This commit is contained in:
Victor Lacasse-Beaudoin 2023-06-03 20:16:41 -04:00
parent 36c04e656d
commit f7981715db
4 changed files with 53 additions and 14 deletions

View file

@ -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

View file

@ -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}

View file

@ -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'

View file

@ -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,
"database": databaseStatus,
})
}