2023-02-17 15:43:23 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-11-29 17:15:27 -05:00
|
|
|
"net/http"
|
2023-02-17 15:43:23 -05:00
|
|
|
|
2023-05-05 18:59:53 -04:00
|
|
|
"git.agecem.com/agecem/babillard/handlers"
|
2023-11-29 17:15:27 -05:00
|
|
|
"git.agecem.com/agecem/babillard/public"
|
2023-02-17 15:43:23 -05:00
|
|
|
"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) {
|
2023-05-05 18:59:53 -04:00
|
|
|
port := viper.GetInt("server.port")
|
2023-11-29 17:15:27 -05:00
|
|
|
//static_dir := viper.GetString("server.static_dir")
|
2023-05-05 18:59:53 -04:00
|
|
|
|
2023-11-29 17:15:27 -05:00
|
|
|
runServer(port)
|
2023-02-17 15:43:23 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2023-11-29 17:15:27 -05:00
|
|
|
func runServer(port int) {
|
2023-02-17 15:43:23 -05:00
|
|
|
log.Print("[I] Starting webserver")
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
2023-11-29 17:15:27 -05:00
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
2023-02-17 15:43:23 -05:00
|
|
|
|
2023-11-29 17:15:27 -05:00
|
|
|
e.Group("").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
|
|
Root: "/html/",
|
|
|
|
Filesystem: http.FS(public.HTMLFS()),
|
|
|
|
}))
|
|
|
|
e.Group("/public/css").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
|
|
Root: "/css/",
|
|
|
|
Filesystem: http.FS(public.CSSFS()),
|
|
|
|
}))
|
|
|
|
e.Group("/public/js").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
|
|
Root: "/js/",
|
|
|
|
Filesystem: http.FS(public.JSFS()),
|
|
|
|
}))
|
2023-02-17 15:43:23 -05:00
|
|
|
|
2023-11-29 17:15:27 -05:00
|
|
|
groupAPI := e.Group("/api")
|
2023-02-17 15:43:23 -05:00
|
|
|
|
2023-11-29 17:15:27 -05:00
|
|
|
groupAPI.GET("/", handlers.HandleAPIShow)
|
|
|
|
groupAPI.GET("/contenu/", handlers.HandleAPIContenuList)
|
|
|
|
groupAPI.GET("/contenu/:filename/", handlers.HandleAPIContenuFile)
|
2023-02-17 15:43:23 -05:00
|
|
|
|
2023-05-05 18:59:53 -04:00
|
|
|
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port)))
|
2023-02-17 15:43:23 -05:00
|
|
|
}
|