bottin/cmd/web.go
Victor Lacasse-Beaudoin 9a0bf87e7b Bump root version to v4
Remove all files from v3

Move all files from v4/ to project root
2023-05-29 18:19:31 -04:00

143 lines
3.5 KiB
Go

package cmd
import (
"crypto/subtle"
"embed"
"fmt"
"html/template"
"io"
"log"
"git.agecem.com/agecem/bottin/v4/data"
"git.agecem.com/agecem/bottin/v4/web"
"git.agecem.com/agecem/bottin/v4/web/webhandlers"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
webUser string
webPassword string
webPort int
webApiHost string
webApiKey string
webApiPort int
webApiProtocol string
)
var templatesFS embed.FS
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
// webCmd represents the web command
var webCmd = &cobra.Command{
Use: "web",
Short: "Démarrer le client web",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
webApiHost = viper.GetString("web.api.host")
webApiKey = viper.GetString("web.api.key")
webApiPort = viper.GetInt("web.api.port")
webApiProtocol = viper.GetString("web.api.protocol")
webPassword = viper.GetString("web.password")
webPort = viper.GetInt("web.port")
webUser = viper.GetString("web.user")
// Ping API server
apiClient := data.NewApiClient(webApiKey, webApiHost, webApiProtocol, webApiPort)
pingResult, err := apiClient.GetV4()
if err != nil {
log.Fatal(err)
}
log.Println(pingResult)
e := echo.New()
// Middlewares
e.Pre(middleware.AddTrailingSlash())
e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) {
usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(webUser)) == 1
passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(webPassword)) == 1
return usersMatch && passwordsMatch, nil
}))
// Template
t := &Template{
templates: template.Must(template.ParseFS(templatesFS, "templates/*.html")),
}
e.Renderer = t
// Routes
e.GET("/", webhandlers.GetIndex)
e.GET("/membre/", webhandlers.GetMembre)
// Execution
e.Logger.Fatal(e.Start(
fmt.Sprintf(":%d", webPort)))
},
}
func init() {
rootCmd.AddCommand(webCmd)
templatesFS = web.GetTemplates()
// web.api.host
webCmd.Flags().String(
"web-api-host", "api",
"Remote API server host (config:'web.api.host')")
viper.BindPFlag("web.api.host", webCmd.Flags().Lookup("web-api-host"))
// web.api.key
webCmd.Flags().String(
"web-api-key", "bottin",
"Remote API server key (config:'web.api.key')")
viper.BindPFlag("web.api.key", webCmd.Flags().Lookup("web-api-key"))
// web.api.protocol
webCmd.Flags().String(
"web-api-protocol", "http",
"Remote API server protocol (config:'web.api.protocol')")
viper.BindPFlag("web.api.protocol", webCmd.Flags().Lookup("web-api-protocol"))
// web.api.port
webCmd.Flags().Int(
"web-api-port", 1312,
"Remote API server port (config:'web.api.port')")
viper.BindPFlag("web.api.port", webCmd.Flags().Lookup("web-api-port"))
// web.password
webCmd.Flags().String(
"web-password", "bottin",
"Web client password (config:'web.password')")
viper.BindPFlag("web.password", webCmd.Flags().Lookup("web-password"))
// web.port
webCmd.Flags().Int(
"web-port", 2312,
"Web client port (config:'web.port')")
viper.BindPFlag("web.port", webCmd.Flags().Lookup("web-port"))
// web.user
webCmd.Flags().String(
"web-user", "bottin",
"Web client user (config:'web.user')")
viper.BindPFlag("web.user", webCmd.Flags().Lookup("web-user"))
}