2023-09-15 13:14:43 -04:00
|
|
|
/*
|
|
|
|
Copyright © 2023 AGECEM & Victor Lacasse-Beaudoin
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-09-19 16:48:29 -04:00
|
|
|
"crypto/subtle"
|
2023-09-15 13:14:43 -04:00
|
|
|
"fmt"
|
2023-09-15 16:36:56 -04:00
|
|
|
"log"
|
2023-09-17 16:32:40 -04:00
|
|
|
"net/http"
|
2023-09-15 13:14:43 -04:00
|
|
|
|
2023-09-18 18:34:53 -04:00
|
|
|
"codeberg.org/vlbeaudoin/voki"
|
2023-09-17 16:32:40 -04:00
|
|
|
"git.agecem.com/agecem/bottin-ag/apiclient"
|
2023-09-15 16:36:56 -04:00
|
|
|
"git.agecem.com/agecem/bottin-ag/config"
|
2023-09-17 01:12:46 -04:00
|
|
|
"git.agecem.com/agecem/bottin-ag/webcontent"
|
|
|
|
"git.agecem.com/agecem/bottin-ag/webhandler"
|
2023-09-15 16:36:56 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-09-15 13:14:43 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// webCmd represents the web command
|
|
|
|
var webCmd = &cobra.Command{
|
|
|
|
Use: "web",
|
|
|
|
Short: "Start the webserver",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-09-15 17:10:57 -04:00
|
|
|
cfg, err := config.UnmarshalConfig()
|
2023-09-15 16:36:56 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
2023-09-17 01:12:46 -04:00
|
|
|
e.Renderer = webcontent.TemplateHTMLFS()
|
|
|
|
|
2023-09-15 16:36:56 -04:00
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
|
|
|
|
2023-09-19 16:48:29 -04:00
|
|
|
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
|
|
|
|
}))
|
|
|
|
|
2023-09-18 18:34:53 -04:00
|
|
|
client := http.DefaultClient
|
|
|
|
defer client.CloseIdleConnections()
|
|
|
|
|
|
|
|
apiClient := &apiclient.APIClient{
|
|
|
|
Voki: voki.New(client, cfg.Web.API.Host, cfg.Web.API.Key, cfg.Web.API.Port, cfg.Web.API.Protocol),
|
|
|
|
}
|
2023-09-17 16:32:40 -04:00
|
|
|
|
|
|
|
handler := webhandler.New(apiClient)
|
2023-09-17 01:12:46 -04:00
|
|
|
|
|
|
|
webhandler.DeclareRoutes(e, &handler)
|
|
|
|
|
2023-09-17 16:32:40 -04:00
|
|
|
publicGroup := e.Group("/public/*")
|
|
|
|
publicGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
|
|
Root: "/",
|
|
|
|
Filesystem: http.FS(webcontent.PublicFS()),
|
|
|
|
}))
|
|
|
|
|
2023-09-15 16:36:56 -04:00
|
|
|
e.Start(fmt.Sprintf(":%d", cfg.Web.Port))
|
2023-09-15 13:14:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(webCmd)
|
|
|
|
}
|