diff --git a/cmd/api.go b/cmd/api.go index e44632c..5c2ce60 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -49,7 +49,7 @@ var apiCmd = &cobra.Command{ // Routes - e.GET("/v4/", handlers.GetV4) + e.GET("/v4/health/", handlers.GetHealth) e.POST("/v4/membres/", handlers.PostMembres) diff --git a/data/apiclient.go b/data/apiclient.go index f97684b..9fc1d5a 100644 --- a/data/apiclient.go +++ b/data/apiclient.go @@ -9,6 +9,7 @@ import ( "net/http" "git.agecem.com/agecem/bottin/v4/models" + "github.com/spf13/viper" ) type ApiClient struct { @@ -18,6 +19,15 @@ type ApiClient struct { Protocol string } +func NewApiClientFromViper() *ApiClient { + apiClientKey := viper.GetString("web.api.key") + apiClientHost := viper.GetString("web.api.host") + apiClientProtocol := viper.GetString("web.api.protocol") + apiClientPort := viper.GetInt("web.api.port") + + return NewApiClient(apiClientKey, apiClientHost, apiClientProtocol, apiClientPort) +} + func NewApiClient(key, host, protocol string, port int) *ApiClient { return &ApiClient{ Key: key, diff --git a/data/data.go b/data/data.go index 4572dea..9601792 100644 --- a/data/data.go +++ b/data/data.go @@ -7,6 +7,7 @@ import ( "git.agecem.com/agecem/bottin/v4/models" _ "github.com/jackc/pgx/stdlib" "github.com/jmoiron/sqlx" + "github.com/spf13/viper" ) // DataClient is a postgres client based on sqlx @@ -24,6 +25,19 @@ 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"), + Database: viper.GetString("db.database"), + Port: viper.GetInt("db.port"), + }) + + return client, err +} + func NewDataClient(connection PostgresConnection) (*DataClient, error) { client := &DataClient{PostgresConnection: connection} diff --git a/handlers/health.go b/handlers/health.go new file mode 100644 index 0000000..49d45b0 --- /dev/null +++ b/handlers/health.go @@ -0,0 +1,35 @@ +package handlers + +import ( + "fmt" + "net/http" + + "git.agecem.com/agecem/bottin/v4/data" + "github.com/labstack/echo/v4" +) + +type GetHealthResponse struct { + Message string `json:"message"` +} + +func GetHealth(c echo.Context) error { + response := GetHealthResponse{ + Message: "Bottin API v4 is ready", + } + + dataClient, err := data.NewDataClientFromViper() + if err != nil { + response.Message = fmt.Sprintf("Error during data.NewDataClientFromViper(): %s", err) + + return c.JSON(http.StatusInternalServerError, response) + } + defer dataClient.DB.Close() + + if err = dataClient.DB.Ping(); err != nil { + response.Message = fmt.Sprintf("Error during dataClient.DB.Ping(): %s", err) + + return c.JSON(http.StatusInternalServerError, response) + } + + return c.JSON(http.StatusOK, response) +} diff --git a/handlers/v4.go b/handlers/v4.go deleted file mode 100644 index facbdde..0000000 --- a/handlers/v4.go +++ /dev/null @@ -1,13 +0,0 @@ -package handlers - -import ( - "net/http" - - "github.com/labstack/echo/v4" -) - -func GetV4(c echo.Context) error { - return c.JSON(http.StatusOK, map[string]string{ - "message": "Bottin API v4 is ready", - }) -} diff --git a/web/webhandlers/handlers.go b/web/webhandlers/handlers.go index 34724f9..732d3e7 100644 --- a/web/webhandlers/handlers.go +++ b/web/webhandlers/handlers.go @@ -6,7 +6,6 @@ import ( "git.agecem.com/agecem/bottin/v4/data" "github.com/labstack/echo/v4" - "github.com/spf13/viper" ) func GetIndex(c echo.Context) error { @@ -14,30 +13,10 @@ func GetIndex(c echo.Context) error { } func GetMembre(c echo.Context) error { - apiClientKey := viper.GetString("web.api.key") - apiClientHost := viper.GetString("web.api.host") - apiClientProtocol := viper.GetString("web.api.protocol") - apiClientPort := viper.GetInt("web.api.port") - - /* - log.Printf(` - apiClientKey: %s - apiClientHost: %s - apiClientProtocol: %s - apiClientPort: %d`, - apiClientKey, apiClientHost, apiClientProtocol, apiClientPort, - ) - */ - - apiClient := data.NewApiClient(apiClientKey, apiClientHost, apiClientProtocol, apiClientPort) + apiClient := data.NewApiClientFromViper() membreID := c.QueryParam("membre_id") - /* - // TODO - log.Printf("Requesting membreID: [%s]", membreID) - */ - membre, err := apiClient.GetMembre(membreID) if err != nil { return c.Render(http.StatusBadRequest, "index-html", struct {