From ad83bc081a596e029110e91a445d5a5ae310d238 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 18 Sep 2023 22:07:02 -0400 Subject: [PATCH] Migrate webclient to voki and add webhandlers.Handler --- cmd/web.go | 12 +++++++++--- web/webhandlers/handlers.go | 11 +++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index d09477c..a944d30 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -7,6 +7,7 @@ import ( "html/template" "io" "log" + "net/http" "git.agecem.com/agecem/bottin/v5/data" "git.agecem.com/agecem/bottin/v5/web" @@ -53,7 +54,10 @@ var webCmd = &cobra.Command{ // Ping API server - apiClient := data.NewApiClient(webApiKey, webApiHost, webApiProtocol, webApiPort) + client := http.DefaultClient + defer client.CloseIdleConnections() + + apiClient := data.NewApiClient(client, webApiKey, webApiHost, webApiProtocol, webApiPort) pingResult, err := apiClient.GetHealth() if err != nil { @@ -84,8 +88,10 @@ var webCmd = &cobra.Command{ // Routes - e.GET("/", webhandlers.GetIndex) - e.GET("/membre/", webhandlers.GetMembre) + handler := webhandlers.Handler{APIClient: apiClient} + + e.GET("/", handler.GetIndex) + e.GET("/membre/", handler.GetMembre) // Execution diff --git a/web/webhandlers/handlers.go b/web/webhandlers/handlers.go index dce8399..96fba50 100644 --- a/web/webhandlers/handlers.go +++ b/web/webhandlers/handlers.go @@ -8,16 +8,19 @@ import ( "github.com/labstack/echo/v4" ) -func GetIndex(c echo.Context) error { +type Handler struct { + APIClient *data.ApiClient +} + +func (h *Handler) GetIndex(c echo.Context) error { return c.Render(http.StatusOK, "index-html", nil) } -func GetMembre(c echo.Context) error { - apiClient := data.NewApiClientFromViper() +func (h *Handler) GetMembre(c echo.Context) error { membreID := c.QueryParam("membre_id") - membre, err := apiClient.GetMembre(membreID) + membre, err := h.APIClient.GetMembre(membreID) if err != nil { return c.Render(http.StatusBadRequest, "index-html", struct { Result string