From b68859f90b6077255b799c232158c33b3c824915 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 5 May 2023 19:50:43 -0400 Subject: [PATCH 1/2] =?UTF-8?q?Pr=C3=A9parer=20=C3=A0=20embed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- public/embed.go | 10 ++++++++++ {static => public/static}/index.html | 0 {static => public/static}/slider.js | 0 {static => public/static}/style.css | 0 {static => public/static}/text.js | 0 6 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 public/embed.go rename {static => public/static}/index.html (100%) rename {static => public/static}/slider.js (100%) rename {static => public/static}/style.css (100%) rename {static => public/static}/text.js (100%) diff --git a/Dockerfile b/Dockerfile index 5fa348b..ea6216c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ WORKDIR /app ADD contenu/ contenu/ -ADD static/ static/ +ADD public/static/ static/ COPY --from=build /go/src/app/babillard /usr/bin/babillard diff --git a/public/embed.go b/public/embed.go new file mode 100644 index 0000000..bfc1ca1 --- /dev/null +++ b/public/embed.go @@ -0,0 +1,10 @@ +package public + +import "embed" + +//go:embed static/* +var embedFS embed.FS + +func GetEmbedFS() embed.FS { + return embedFS +} diff --git a/static/index.html b/public/static/index.html similarity index 100% rename from static/index.html rename to public/static/index.html diff --git a/static/slider.js b/public/static/slider.js similarity index 100% rename from static/slider.js rename to public/static/slider.js diff --git a/static/style.css b/public/static/style.css similarity index 100% rename from static/style.css rename to public/static/style.css diff --git a/static/text.js b/public/static/text.js similarity index 100% rename from static/text.js rename to public/static/text.js From 996aa679840add8bddf0b88400efb05e85092d03 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 29 Nov 2023 17:15:27 -0500 Subject: [PATCH 2/2] =?UTF-8?q?refactor!:=20D=C3=A9placer=20fichiers=20sta?= =?UTF-8?q?tiques=20vers=20embed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Dockerfile | 10 ++++----- cmd/server.go | 34 +++++++++++++++++++----------- public/{static => css}/style.css | 0 public/embed.go | 18 +++++++++++----- public/{static => html}/index.html | 4 ++-- public/{static => js}/slider.js | 2 +- public/{static => js}/text.js | 0 7 files changed, 43 insertions(+), 25 deletions(-) rename public/{static => css}/style.css (100%) rename public/{static => html}/index.html (86%) rename public/{static => js}/slider.js (92%) rename public/{static => js}/text.js (100%) diff --git a/Dockerfile b/Dockerfile index ea6216c..0847470 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20.2 as build +FROM golang:1.21.4 as build LABEL author="Victor Lacasse-Beaudoin " LABEL license="MIT" @@ -15,11 +15,13 @@ ADD data/ data/ ADD handlers/ handlers/ -RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o babillard . +ADD public/ public/ + +RUN CGO_ENABLED=0 go build -a -o babillard . # Alpine -FROM alpine:3.17.2 +FROM alpine:3.18.4 RUN apk update && apk upgrade --no-cache @@ -27,8 +29,6 @@ WORKDIR /app ADD contenu/ contenu/ -ADD public/static/ static/ - COPY --from=build /go/src/app/babillard /usr/bin/babillard CMD ["babillard", "server"] diff --git a/cmd/server.go b/cmd/server.go index 44eba23..a28481b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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))) } diff --git a/public/static/style.css b/public/css/style.css similarity index 100% rename from public/static/style.css rename to public/css/style.css diff --git a/public/embed.go b/public/embed.go index bfc1ca1..db7092c 100644 --- a/public/embed.go +++ b/public/embed.go @@ -2,9 +2,17 @@ package public import "embed" -//go:embed static/* -var embedFS embed.FS +//go:embed html/* +var htmlFS embed.FS -func GetEmbedFS() embed.FS { - return embedFS -} +func HTMLFS() embed.FS { return htmlFS } + +//go:embed css/* +var cssFS embed.FS + +func CSSFS() embed.FS { return cssFS } + +//go:embed js/* +var jsFS embed.FS + +func JSFS() embed.FS { return jsFS } diff --git a/public/static/index.html b/public/html/index.html similarity index 86% rename from public/static/index.html rename to public/html/index.html index c8a77bd..c32e44b 100644 --- a/public/static/index.html +++ b/public/html/index.html @@ -5,13 +5,13 @@ - + - + diff --git a/public/static/slider.js b/public/js/slider.js similarity index 92% rename from public/static/slider.js rename to public/js/slider.js index 96a65c8..e30a9c6 100644 --- a/public/static/slider.js +++ b/public/js/slider.js @@ -6,7 +6,7 @@ function afficherIndex() { } function afficherImage() { - $('#image').attr('src', "api/contenu/"+images[indexImages]); + $('#image').attr('src', "/api/contenu/"+images[indexImages]); } function augmenterIndex() { diff --git a/public/static/text.js b/public/js/text.js similarity index 100% rename from public/static/text.js rename to public/js/text.js