refactor!: Déplacer fichiers statiques vers embed
Bump golang -> `1.21.4` Bump alpine -> `3.18.4` Exposer fichiers html sur `/*` Exposer fichiers css sur `/public/css/*` Exposer fichiers js sur `/public/js/*` Retirer prefix cgo de build step Ajouter `public/` à build step Retirer `public/static/` de `static/` dans run step Retirer mentions à static_dir dans `cmd/server.go` Retirer flag `server.static_dir`, `--static_dir` de serverCmd Retirer paramètre `static_dir` de `runServer` Remplacer middleware `RemoveTrailingSlash` -> `AddTrailingSlash` Ajouter `echo.Group`s pour exposition statique avec config Refactor déclaration de routes api derrière groupe `/api` Fix références de fichiers js et css dans `index.html` et `slider.js` BREAKING: static_dir n'est plus utilisé, les fichiers sont maintenant embedded
This commit is contained in:
parent
b68859f90b
commit
996aa67984
7 changed files with 43 additions and 25 deletions
|
@ -3,8 +3,10 @@ package cmd
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/babillard/handlers"
|
||||
"git.agecem.com/agecem/babillard/public"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -17,9 +19,9 @@ var serverCmd = &cobra.Command{
|
|||
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")
|
||||
//static_dir := viper.GetString("server.static_dir")
|
||||
|
||||
runServer(port, static_dir)
|
||||
runServer(port)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -34,25 +36,33 @@ func declareFlags() {
|
|||
|
||||
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) {
|
||||
func runServer(port int) {
|
||||
log.Print("[I] Starting webserver")
|
||||
|
||||
e := echo.New()
|
||||
|
||||
g := e.Group("")
|
||||
e.Pre(middleware.AddTrailingSlash())
|
||||
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
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()),
|
||||
}))
|
||||
|
||||
g.Static("/", static_dir)
|
||||
groupAPI := e.Group("/api")
|
||||
|
||||
g.GET("/api", handlers.HandleAPIShow)
|
||||
g.GET("/api/contenu", handlers.HandleAPIContenuList)
|
||||
g.GET("/api/contenu/:filename", handlers.HandleAPIContenuFile)
|
||||
groupAPI.GET("/", handlers.HandleAPIShow)
|
||||
groupAPI.GET("/contenu/", handlers.HandleAPIContenuList)
|
||||
groupAPI.GET("/contenu/:filename/", handlers.HandleAPIContenuFile)
|
||||
|
||||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port)))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue