From 244276905b94253aad868032d388847ef1ffeab4 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 20 Jun 2024 19:36:38 -0400 Subject: [PATCH] =?UTF-8?q?feature(cmd):=20impl=C3=A9menter=20webCmd=20de?= =?UTF-8?q?=20base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit manque encore le processus de scan mais sinon c'est presque fini --- cmd.go | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/cmd.go b/cmd.go index 6067795..dd41f0b 100644 --- a/cmd.go +++ b/cmd.go @@ -6,8 +6,10 @@ import ( "fmt" "html/template" "log" + "net/http" "os" + "codeberg.org/vlbeaudoin/voki/v3" "github.com/jackc/pgx/v5/pgxpool" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -122,56 +124,56 @@ var webCmd = &cobra.Command{ Short: "Démarrer le client web", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { + // Parse config var cfg Config if err := viper.Unmarshal(&cfg); err != nil { log.Fatal("init config:", err) } - // Ping API server - /* - client := http.DefaultClient - defer client.CloseIdleConnections() - - apiClient := data.NewApiClient(client, webApiKey, webApiHost, webApiProtocol, webApiPort) - - pingResult, err := apiClient.GetHealth() - if err != nil { - log.Fatal(err) - } - - log.Println(pingResult) - */ - e := echo.New() // Middlewares + // Trailing slash e.Pre(middleware.AddTrailingSlash()) + // Auth e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) { usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(cfg.Web.User)) == 1 passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(cfg.Web.Password)) == 1 return usersMatch && passwordsMatch, nil })) - // Template - - t := &Template{ + // Templating + e.Renderer = &Template{ templates: template.Must(template.ParseFS(templatesFS, "templates/*.html")), } - e.Renderer = t + // API Client + apiClient := APIClient{voki.New( + http.DefaultClient, + cfg.Web.API.Host, + cfg.Web.API.Key, + cfg.Web.API.Port, + cfg.Web.API.Protocol, + )} + defer apiClient.Voki.CloseIdleConnections() // Routes - /* - handler := webhandlers.Handler{APIClient: apiClient} + e.GET("/", func(c echo.Context) error { + pingResult, err := apiClient.GetHealth() + if err != nil { + log.Fatal(err) + } - e.GET("/", handler.GetIndex) - e.GET("/membre/", handler.GetMembre) - */ + return c.Render( + http.StatusOK, + "index-html", + voki.MessageResponse{Message: pingResult}, + ) + }) // Execution - e.Logger.Fatal(e.Start( fmt.Sprintf(":%d", cfg.Web.Port))) },