Version 7 #53

Merged
vlbeaudoin merged 33 commits from rewrite/v7 into main 2024-09-03 11:17:26 -04:00
Showing only changes of commit 244276905b - Show all commits

52
cmd.go
View file

@ -6,8 +6,10 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
"net/http"
"os" "os"
"codeberg.org/vlbeaudoin/voki/v3"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
@ -122,56 +124,56 @@ var webCmd = &cobra.Command{
Short: "Démarrer le client web", Short: "Démarrer le client web",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Parse config
var cfg Config var cfg Config
if err := viper.Unmarshal(&cfg); err != nil { if err := viper.Unmarshal(&cfg); err != nil {
log.Fatal("init config:", err) 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() e := echo.New()
// Middlewares // Middlewares
// Trailing slash
e.Pre(middleware.AddTrailingSlash()) e.Pre(middleware.AddTrailingSlash())
// Auth
e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) { e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) {
usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(cfg.Web.User)) == 1 usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(cfg.Web.User)) == 1
passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(cfg.Web.Password)) == 1 passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(cfg.Web.Password)) == 1
return usersMatch && passwordsMatch, nil return usersMatch && passwordsMatch, nil
})) }))
// Template // Templating
e.Renderer = &Template{
t := &Template{
templates: template.Must(template.ParseFS(templatesFS, "templates/*.html")), 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 // Routes
/* e.GET("/", func(c echo.Context) error {
handler := webhandlers.Handler{APIClient: apiClient} pingResult, err := apiClient.GetHealth()
if err != nil {
log.Fatal(err)
}
e.GET("/", handler.GetIndex) return c.Render(
e.GET("/membre/", handler.GetMembre) http.StatusOK,
*/ "index-html",
voki.MessageResponse{Message: pingResult},
)
})
// Execution // Execution
e.Logger.Fatal(e.Start( e.Logger.Fatal(e.Start(
fmt.Sprintf(":%d", cfg.Web.Port))) fmt.Sprintf(":%d", cfg.Web.Port)))
}, },