Bump API à version 2 #2
4 changed files with 53 additions and 14 deletions
29
cmd/api.go
29
cmd/api.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"git.agecem.com/agecem/bottin-agenda/data"
|
||||||
"git.agecem.com/agecem/bottin-agenda/handlers"
|
"git.agecem.com/agecem/bottin-agenda/handlers"
|
||||||
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
@ -27,16 +28,6 @@ var apiCmd = &cobra.Command{
|
||||||
apiKey = viper.GetString("api.key")
|
apiKey = viper.GetString("api.key")
|
||||||
apiPort = viper.GetInt("api.port")
|
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")
|
bottinApiKey := viper.GetString("bottin.api.key")
|
||||||
bottinApiHost := viper.GetString("bottin.api.host")
|
bottinApiHost := viper.GetString("bottin.api.host")
|
||||||
bottinApiProtocol := viper.GetString("bottin.api.protocol")
|
bottinApiProtocol := viper.GetString("bottin.api.protocol")
|
||||||
|
@ -72,10 +63,24 @@ var apiCmd = &cobra.Command{
|
||||||
|
|
||||||
bottinHealthResponse, err := bottinConnection.GetHealth()
|
bottinHealthResponse, err := bottinConnection.GetHealth()
|
||||||
if err != nil {
|
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
|
// Execution
|
||||||
|
|
||||||
|
|
13
data/data.go
13
data/data.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"git.agecem.com/agecem/bottin-agenda/models"
|
"git.agecem.com/agecem/bottin-agenda/models"
|
||||||
_ "github.com/jackc/pgx/stdlib"
|
_ "github.com/jackc/pgx/stdlib"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DataClient is a postgres client based on sqlx
|
// DataClient is a postgres client based on sqlx
|
||||||
|
@ -23,6 +24,18 @@ type PostgresConnection struct {
|
||||||
SSL bool
|
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) {
|
func NewDataClient(connection PostgresConnection) (*DataClient, error) {
|
||||||
client := &DataClient{PostgresConnection: connection}
|
client := &DataClient{PostgresConnection: connection}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@ services:
|
||||||
POSTGRES_DATABASE: "${BOTTINAGENDA_POSTGRES_DATABASE}"
|
POSTGRES_DATABASE: "${BOTTINAGENDA_POSTGRES_DATABASE}"
|
||||||
POSTGRES_PASSWORD: "${BOTTINAGENDA_POSTGRES_PASSWORD}"
|
POSTGRES_PASSWORD: "${BOTTINAGENDA_POSTGRES_PASSWORD}"
|
||||||
POSTGRES_USER: "${BOTTINAGENDA_POSTGRES_USER}"
|
POSTGRES_USER: "${BOTTINAGENDA_POSTGRES_USER}"
|
||||||
|
ports:
|
||||||
|
- '5433:5432'
|
||||||
volumes:
|
volumes:
|
||||||
- 'db-data:/var/lib/postgresql/data'
|
- 'db-data:/var/lib/postgresql/data'
|
||||||
restart: 'unless-stopped'
|
restart: 'unless-stopped'
|
||||||
|
|
|
@ -3,6 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.agecem.com/agecem/bottin-agenda/data"
|
||||||
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -30,8 +31,26 @@ func GetHealth(c echo.Context) error {
|
||||||
bottinStatus = healthResponse
|
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{
|
return c.JSON(http.StatusOK, map[string]string{
|
||||||
"message": "Bottin-agenda API v2 is ready",
|
"message": "Bottin-agenda API v2 is ready",
|
||||||
"bottin": bottinStatus,
|
"bottin": bottinStatus,
|
||||||
|
"database": databaseStatus,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue