60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
/*
|
|
Copyright © 2023 AGECEM & Victor Lacasse-Beaudoin
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"codeberg.org/vlbeaudoin/voki"
|
|
"git.agecem.com/agecem/bottin-ag/apiclient"
|
|
"git.agecem.com/agecem/bottin-ag/config"
|
|
"git.agecem.com/agecem/bottin-ag/webcontent"
|
|
"git.agecem.com/agecem/bottin-ag/webhandler"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"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) {
|
|
cfg, err := config.UnmarshalConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
e := echo.New()
|
|
|
|
e.Renderer = webcontent.TemplateHTMLFS()
|
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
|
|
|
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),
|
|
}
|
|
|
|
handler := webhandler.New(apiClient)
|
|
|
|
webhandler.DeclareRoutes(e, &handler)
|
|
|
|
publicGroup := e.Group("/public/*")
|
|
publicGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
Root: "/",
|
|
Filesystem: http.FS(webcontent.PublicFS()),
|
|
}))
|
|
|
|
e.Start(fmt.Sprintf(":%d", cfg.Web.Port))
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(webCmd)
|
|
}
|