2023-05-25 02:21:09 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-25 19:22:46 -04:00
|
|
|
"crypto/subtle"
|
|
|
|
"embed"
|
2023-05-25 02:21:09 -04:00
|
|
|
"fmt"
|
2023-05-25 19:22:46 -04:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2023-05-26 01:43:58 -04:00
|
|
|
"log"
|
2023-09-18 22:07:02 -04:00
|
|
|
"net/http"
|
2023-05-25 02:21:09 -04:00
|
|
|
|
2023-10-17 16:35:51 -04:00
|
|
|
"codeberg.org/vlbeaudoin/serpents"
|
2024-01-05 14:38:48 -05:00
|
|
|
"git.agecem.com/agecem/bottin/v6/data"
|
|
|
|
"git.agecem.com/agecem/bottin/v6/web"
|
|
|
|
"git.agecem.com/agecem/bottin/v6/web/webhandlers"
|
2023-05-25 19:22:46 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-05-25 02:21:09 -04:00
|
|
|
"github.com/spf13/cobra"
|
2023-05-25 19:22:46 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-05-25 02:21:09 -04:00
|
|
|
)
|
|
|
|
|
2023-05-25 19:22:46 -04:00
|
|
|
var (
|
2023-05-26 01:43:58 -04:00
|
|
|
webUser string
|
|
|
|
webPassword string
|
|
|
|
webPort int
|
|
|
|
webApiHost string
|
|
|
|
webApiKey string
|
|
|
|
webApiPort int
|
|
|
|
webApiProtocol string
|
2023-05-25 19:22:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-05-25 02:21:09 -04:00
|
|
|
// webCmd represents the web command
|
|
|
|
var webCmd = &cobra.Command{
|
|
|
|
Use: "web",
|
|
|
|
Short: "Démarrer le client web",
|
2023-05-25 19:22:46 -04:00
|
|
|
Args: cobra.ExactArgs(0),
|
2023-05-25 02:21:09 -04:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-05-25 19:22:46 -04:00
|
|
|
webApiHost = viper.GetString("web.api.host")
|
|
|
|
webApiKey = viper.GetString("web.api.key")
|
|
|
|
webApiPort = viper.GetInt("web.api.port")
|
2023-05-26 01:43:58 -04:00
|
|
|
webApiProtocol = viper.GetString("web.api.protocol")
|
2023-05-25 19:22:46 -04:00
|
|
|
webPassword = viper.GetString("web.password")
|
|
|
|
webPort = viper.GetInt("web.port")
|
|
|
|
webUser = viper.GetString("web.user")
|
|
|
|
|
2023-05-26 01:43:58 -04:00
|
|
|
// Ping API server
|
|
|
|
|
2023-09-18 22:07:02 -04:00
|
|
|
client := http.DefaultClient
|
|
|
|
defer client.CloseIdleConnections()
|
|
|
|
|
|
|
|
apiClient := data.NewApiClient(client, webApiKey, webApiHost, webApiProtocol, webApiPort)
|
2023-05-26 01:43:58 -04:00
|
|
|
|
2023-06-02 16:33:13 -04:00
|
|
|
pingResult, err := apiClient.GetHealth()
|
2023-05-26 01:43:58 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(pingResult)
|
|
|
|
|
2023-05-25 19:22:46 -04:00
|
|
|
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
|
|
|
|
|
2023-09-18 22:07:02 -04:00
|
|
|
handler := webhandlers.Handler{APIClient: apiClient}
|
|
|
|
|
|
|
|
e.GET("/", handler.GetIndex)
|
|
|
|
e.GET("/membre/", handler.GetMembre)
|
2023-05-25 19:22:46 -04:00
|
|
|
|
|
|
|
// Execution
|
|
|
|
|
|
|
|
e.Logger.Fatal(e.Start(
|
|
|
|
fmt.Sprintf(":%d", webPort)))
|
2023-05-25 02:21:09 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(webCmd)
|
2023-05-25 19:22:46 -04:00
|
|
|
templatesFS = web.GetTemplates()
|
|
|
|
|
|
|
|
// web.api.host
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(webCmd.Flags(),
|
|
|
|
"web.api.host", "web-api-host", "api",
|
|
|
|
"Remote API server host")
|
2023-05-25 19:22:46 -04:00
|
|
|
|
|
|
|
// web.api.key
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(webCmd.Flags(),
|
|
|
|
"web.api.key", "web-api-key", "bottin",
|
|
|
|
"Remote API server key")
|
2023-05-25 19:22:46 -04:00
|
|
|
|
2023-05-25 22:07:53 -04:00
|
|
|
// web.api.protocol
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(webCmd.Flags(),
|
|
|
|
"web.api.protocol", "web-api-protocol", "http",
|
|
|
|
"Remote API server protocol")
|
2023-05-25 22:07:53 -04:00
|
|
|
|
2023-05-25 19:22:46 -04:00
|
|
|
// web.api.port
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.Int(webCmd.Flags(),
|
|
|
|
"web.api.port", "web-api-port", 1312,
|
|
|
|
"Remote API server port")
|
2023-05-25 19:22:46 -04:00
|
|
|
|
|
|
|
// web.password
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(webCmd.Flags(),
|
|
|
|
"web.password", "web-password", "bottin",
|
|
|
|
"Web client password")
|
2023-05-25 19:22:46 -04:00
|
|
|
|
|
|
|
// web.port
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.Int(webCmd.Flags(),
|
|
|
|
"web.port", "web-port", 2312,
|
|
|
|
"Web client port")
|
2023-05-25 19:22:46 -04:00
|
|
|
|
|
|
|
// web.user
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(webCmd.Flags(),
|
|
|
|
"web.user", "web-user", "bottin",
|
|
|
|
"Web client user")
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|