From 098d255190fc5c441269288afe3efe145f80d9df Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 2 Jun 2023 02:35:15 -0400 Subject: [PATCH 1/6] =?UTF-8?q?D=C3=A9placer=20/v4=20->=20/v4/health?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Renommer handler GetV4 pour GetHealth Ajouter GetHealthResponse pour serialize et deserialize json response --- cmd/api.go | 2 +- handlers/health.go | 17 +++++++++++++++++ handlers/v4.go | 13 ------------- 3 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 handlers/health.go delete mode 100644 handlers/v4.go diff --git a/cmd/api.go b/cmd/api.go index e44632c..a948b78 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/handlers/health.go b/handlers/health.go new file mode 100644 index 0000000..d917738 --- /dev/null +++ b/handlers/health.go @@ -0,0 +1,17 @@ +package handlers + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +type GetHealthResponse struct { + Message string `json:"message"` +} + +func GetHealth(c echo.Context) error { + response := GetHealthResponse{"Bottin API v4 is ready"} + + 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", - }) -} From 4673612e06ef8e6a546181c978972f939bf40a60 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 2 Jun 2023 02:43:37 -0400 Subject: [PATCH 2/6] Ajouter data.NewApiClientFromViper() --- data/apiclient.go | 10 ++++++++++ 1 file changed, 10 insertions(+) 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, From 80a0260021463499204e42e9b2fac5eb00273f1b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 2 Jun 2023 02:44:11 -0400 Subject: [PATCH 3/6] Ajouter data.NewDataClientFromViper() --- data/data.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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} From 6d010c5009b3022e968759bb86a1591e63148a23 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 2 Jun 2023 02:46:08 -0400 Subject: [PATCH 4/6] Cleanup webhandlers.GetMembre Remplacer data.NewApiClient() -> data.NewApiClientFromViper() Cleanup comments --- web/webhandlers/handlers.go | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) 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 { From b4af26d3ddffcc86e07e66e35eece2af5bbc5345 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 2 Jun 2023 03:17:40 -0400 Subject: [PATCH 5/6] Fix /v4/health --- cmd/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/api.go b/cmd/api.go index a948b78..5c2ce60 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -49,7 +49,7 @@ var apiCmd = &cobra.Command{ // Routes - e.GET("/v4/health", handlers.GetHealth) + e.GET("/v4/health/", handlers.GetHealth) e.POST("/v4/membres/", handlers.PostMembres) From 7823541f0eb77a0ab8cd1711355f9b953d35a1c2 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 2 Jun 2023 03:17:43 -0400 Subject: [PATCH 6/6] =?UTF-8?q?Ajouter=20ping=20de=20database=20=C3=A0=20h?= =?UTF-8?q?ealthcheck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handlers/health.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/handlers/health.go b/handlers/health.go index d917738..49d45b0 100644 --- a/handlers/health.go +++ b/handlers/health.go @@ -1,8 +1,10 @@ package handlers import ( + "fmt" "net/http" + "git.agecem.com/agecem/bottin/v4/data" "github.com/labstack/echo/v4" ) @@ -11,7 +13,23 @@ type GetHealthResponse struct { } func GetHealth(c echo.Context) error { - response := GetHealthResponse{"Bottin API v4 is ready"} + 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) }