package cmd import ( "fmt" "log" "git.agecem.com/agecem/babillard/handlers" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/spf13/cobra" "github.com/spf13/viper" ) // serverCmd represents the server command var serverCmd = &cobra.Command{ Use: "server", Short: "Démarrer le serveur web", Run: func(cmd *cobra.Command, args []string) { port := viper.GetInt("server.port") static_dir := viper.GetString("server.static_dir") runServer(port, static_dir) }, } func init() { rootCmd.AddCommand(serverCmd) declareFlags() } func declareFlags() { serverCmd.Flags().IntP("port", "p", 8080, "Port réseau à utiliser (config: \"server.port\")") viper.BindPFlag("server.port", serverCmd.Flags().Lookup("port")) serverCmd.Flags().String("contenu_dir", "contenu", "Répertoire du contenu à exposer (config: \"server.contenu_dir\")") viper.BindPFlag("server.contenu_dir", serverCmd.Flags().Lookup("contenu_dir")) serverCmd.Flags().String("static_dir", "static", "Répertoire des fichiers statiques à exposer (config: \"server.static_dir\")") viper.BindPFlag("server.static_dir", serverCmd.Flags().Lookup("static_dir")) } func runServer(port int, static_dir string) { log.Print("[I] Starting webserver") e := echo.New() g := e.Group("") e.Pre(middleware.RemoveTrailingSlash()) g.Static("/", static_dir) g.GET("/api", handlers.HandleAPIShow) g.GET("/api/contenu", handlers.HandleAPIContenuList) g.GET("/api/contenu/:filename", handlers.HandleAPIContenuFile) e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port))) }