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"
"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)))
},