bottin-agenda/handlers/health.go

66 lines
1.5 KiB
Go
Raw Normal View History

2023-05-29 17:58:23 -04:00
package handlers
import (
"net/http"
2023-06-03 20:16:41 -04:00
"git.agecem.com/agecem/bottin-agenda/data"
"git.agecem.com/agecem/bottin-agenda/responses"
2023-06-03 19:48:37 -04:00
bottindata "git.agecem.com/agecem/bottin/v5/data"
2023-05-29 17:58:23 -04:00
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
2023-06-03 19:48:37 -04:00
func GetHealth(c echo.Context) error {
var response responses.GetHealthResponse
var statusCode int = http.StatusNotImplemented
2023-05-29 17:58:23 -04:00
bottinApiKey := viper.GetString("bottin.api.key")
bottinApiHost := viper.GetString("bottin.api.host")
bottinApiProtocol := viper.GetString("bottin.api.protocol")
bottinApiPort := viper.GetInt("bottin.api.port")
bottinClient := http.DefaultClient
defer bottinClient.CloseIdleConnections()
2023-06-03 19:48:37 -04:00
bottinConnection := bottindata.NewApiClient(
bottinClient,
2023-05-29 17:58:23 -04:00
bottinApiKey,
bottinApiHost,
bottinApiProtocol,
bottinApiPort,
)
var bottinStatus string
2023-06-03 19:48:37 -04:00
healthResponse, err := bottinConnection.GetHealth()
2023-05-29 17:58:23 -04:00
if err != nil {
bottinStatus = err.Error()
} else {
2023-06-03 19:48:37 -04:00
bottinStatus = healthResponse
2023-05-29 17:58:23 -04:00
}
2023-06-03 20:16:41 -04:00
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"
}
}
statusCode = http.StatusOK
response.Message = "Bottin-agenda API v3 is ready"
response.Data.Bottin = bottinStatus
response.Data.Database = databaseStatus
return c.JSON(statusCode, response)
2023-05-29 17:58:23 -04:00
}