package cmd import ( "crypto/subtle" "embed" "fmt" "html/template" "io" "log" "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 ) const ( viperWebUser string = "web.user" flagWebUser string = "web-user" defaultWebUser string = "bottin" descriptionWebUser string = "Web client basic auth user" viperWebPassword string = "web.password" flagWebPassword string = "web-password" defaultWebPassword string = "bottin" descriptionWebPassword string = "Web client basic auth password" viperWebPort string = "web.port" flagWebPort string = "web-port" defaultWebPort int = 2312 descriptionWebPort string = "Web client port" viperWebAPIHost string = "api.host" flagWebAPIHost string = "api-host" defaultWebAPIHost string = "api" descriptionWebAPIHost string = "Target API server host" viperWebAPIKey string = "api.key" flagWebAPIKey string = "api-key" defaultWebAPIKey string = "bottin" descriptionWebAPIKey string = "Target API server key" viperWebAPIPort string = "api.port" flagWebAPIPort string = "api-port" defaultWebAPIPort int = 1312 descriptionWebAPIPort string = "Target API server port" viperWebAPIProtocol string = "api.protocol" flagWebAPIProtocol string = "api-protocol" defaultWebAPIProtocol string = "http" descriptionWebAPIProtocol string = "Target API server protocol (http/https)" ) 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(viperWebAPIHost) webApiKey = viper.GetString(viperWebAPIKey) webApiPort = viper.GetInt(viperWebAPIPort) webApiProtocol = viper.GetString(viperWebAPIProtocol) webPassword = viper.GetString(viperWebPassword) webPort = viper.GetInt(viperWebPort) webUser = viper.GetString(viperWebUser) // 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 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 /* handler := webhandlers.Handler{APIClient: apiClient} e.GET("/", handler.GetIndex) e.GET("/membre/", handler.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(flagWebAPIHost, defaultWebAPIHost, descriptionWebAPIHost) if err := viper.BindPFlag(viperWebAPIHost, webCmd.Flags().Lookup(flagWebAPIHost)); err != nil { log.Fatal(err) } // web.api.key webCmd.Flags().String(flagWebAPIKey, defaultWebAPIKey, descriptionWebAPIKey) if err := viper.BindPFlag(viperWebAPIKey, webCmd.Flags().Lookup(flagWebAPIKey)); err != nil { log.Fatal(err) } // web.api.protocol webCmd.Flags().String(flagWebAPIProtocol, defaultWebAPIProtocol, descriptionWebAPIProtocol) if err := viper.BindPFlag(viperWebAPIProtocol, webCmd.Flags().Lookup(flagWebAPIProtocol)); err != nil { log.Fatal(err) } // web.api.port webCmd.Flags().Int(flagWebAPIPort, defaultWebAPIPort, descriptionWebAPIPort) if err := viper.BindPFlag(viperWebAPIPort, webCmd.Flags().Lookup(flagWebAPIPort)); err != nil { log.Fatal(err) } // web.password webCmd.Flags().String(flagWebPassword, defaultWebPassword, descriptionWebPassword) if err := viper.BindPFlag(viperWebPassword, webCmd.Flags().Lookup(flagWebPassword)); err != nil { log.Fatal(err) } // web.port webCmd.Flags().Int(flagWebPort, defaultWebPort, descriptionWebPort) if err := viper.BindPFlag(viperWebPort, webCmd.Flags().Lookup(flagWebPort)); err != nil { log.Fatal(err) } // web.user webCmd.Flags().String(flagWebUser, defaultWebUser, descriptionWebUser) if err := viper.BindPFlag(viperWebUser, webCmd.Flags().Lookup(flagWebUser)); err != nil { log.Fatal(err) } }