From 2eee1f2fd228b104a8b13a91445ff9aa8c95c78a Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 4 Jul 2023 21:57:13 -0400 Subject: [PATCH 1/2] =?UTF-8?q?S=C3=A9parer=20templates=20de=20public/=20-?= =?UTF-8?q?>=20templates/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Séparer embedFS en publicFS et templatesFS --- Dockerfile | 2 ++ cmd/server.go | 15 ++++++++++----- public/public.go | 8 ++++---- {public => templates}/html/a-propos.gohtml | 0 {public => templates}/html/actualite.gohtml | 0 {public => templates}/html/admin-upload.gohtml | 0 {public => templates}/html/admin.gohtml | 0 {public => templates}/html/documentation.gohtml | 0 {public => templates}/html/formulaires.gohtml | 0 {public => templates}/html/general.gohtml | 0 {public => templates}/html/header.gohtml | 0 {public => templates}/html/index.gohtml | 0 {public => templates}/html/vie-etudiante.gohtml | 0 templates/templates.go | 10 ++++++++++ 14 files changed, 26 insertions(+), 9 deletions(-) rename {public => templates}/html/a-propos.gohtml (100%) rename {public => templates}/html/actualite.gohtml (100%) rename {public => templates}/html/admin-upload.gohtml (100%) rename {public => templates}/html/admin.gohtml (100%) rename {public => templates}/html/documentation.gohtml (100%) rename {public => templates}/html/formulaires.gohtml (100%) rename {public => templates}/html/general.gohtml (100%) rename {public => templates}/html/header.gohtml (100%) rename {public => templates}/html/index.gohtml (100%) rename {public => templates}/html/vie-etudiante.gohtml (100%) create mode 100644 templates/templates.go diff --git a/Dockerfile b/Dockerfile index d91ca09..a062281 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,8 @@ ADD config/ config/ ADD media/ media/ +ADD templates/ templates/ + RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org . # Alpine diff --git a/cmd/server.go b/cmd/server.go index 3abcb0a..fab4eaf 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -25,6 +25,7 @@ import ( "git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/media" "git.agecem.com/agecem/agecem-org/public" + "git.agecem.com/agecem/agecem-org/templates" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) @@ -35,7 +36,10 @@ type Template struct { var cfg config.Config -var embedFS embed.FS +var ( + publicFS embed.FS + templatesFS embed.FS +) // serverCmd represents the server command var serverCmd = &cobra.Command{ @@ -64,7 +68,8 @@ var serverCmd = &cobra.Command{ func init() { rootCmd.AddCommand(serverCmd) - embedFS = public.GetEmbedFS() + publicFS = public.GetPublicFS() + templatesFS = templates.GetTemplatesFS() // server.port - --server-port serverCmd.Flags().Int("server-port", 8080, "Port to run the webserver on (config: server.port)") @@ -122,7 +127,7 @@ func RunServer() { e := echo.New() t := &Template{ - templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")), + templates: template.Must(template.ParseFS(templatesFS, "html/*.gohtml")), } e.Renderer = t @@ -745,12 +750,12 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { func handleStaticCSSIndex(c echo.Context) error { // TODO Ajouter gestion d'erreurs - data, _ := embedFS.ReadFile("css/index.css") + data, _ := publicFS.ReadFile("css/index.css") return c.Blob(http.StatusOK, "text/css", data) } func handleStaticCSSGeneral(c echo.Context) error { // TODO Ajouter gestion d'erreurs - data, _ := embedFS.ReadFile("css/general.css") + data, _ := publicFS.ReadFile("css/general.css") return c.Blob(http.StatusOK, "text/css", data) } diff --git a/public/public.go b/public/public.go index 60d9fe6..a2ab158 100644 --- a/public/public.go +++ b/public/public.go @@ -2,9 +2,9 @@ package public import "embed" -//go:embed html/* css/* js/* -var embedFS embed.FS +//go:embed css/*.css js/*.js +var publicFS embed.FS -func GetEmbedFS() embed.FS { - return embedFS +func GetPublicFS() embed.FS { + return publicFS } diff --git a/public/html/a-propos.gohtml b/templates/html/a-propos.gohtml similarity index 100% rename from public/html/a-propos.gohtml rename to templates/html/a-propos.gohtml diff --git a/public/html/actualite.gohtml b/templates/html/actualite.gohtml similarity index 100% rename from public/html/actualite.gohtml rename to templates/html/actualite.gohtml diff --git a/public/html/admin-upload.gohtml b/templates/html/admin-upload.gohtml similarity index 100% rename from public/html/admin-upload.gohtml rename to templates/html/admin-upload.gohtml diff --git a/public/html/admin.gohtml b/templates/html/admin.gohtml similarity index 100% rename from public/html/admin.gohtml rename to templates/html/admin.gohtml diff --git a/public/html/documentation.gohtml b/templates/html/documentation.gohtml similarity index 100% rename from public/html/documentation.gohtml rename to templates/html/documentation.gohtml diff --git a/public/html/formulaires.gohtml b/templates/html/formulaires.gohtml similarity index 100% rename from public/html/formulaires.gohtml rename to templates/html/formulaires.gohtml diff --git a/public/html/general.gohtml b/templates/html/general.gohtml similarity index 100% rename from public/html/general.gohtml rename to templates/html/general.gohtml diff --git a/public/html/header.gohtml b/templates/html/header.gohtml similarity index 100% rename from public/html/header.gohtml rename to templates/html/header.gohtml diff --git a/public/html/index.gohtml b/templates/html/index.gohtml similarity index 100% rename from public/html/index.gohtml rename to templates/html/index.gohtml diff --git a/public/html/vie-etudiante.gohtml b/templates/html/vie-etudiante.gohtml similarity index 100% rename from public/html/vie-etudiante.gohtml rename to templates/html/vie-etudiante.gohtml diff --git a/templates/templates.go b/templates/templates.go new file mode 100644 index 0000000..d848e80 --- /dev/null +++ b/templates/templates.go @@ -0,0 +1,10 @@ +package templates + +import "embed" + +//go:embed html/*.gohtml +var templatesFS embed.FS + +func GetTemplatesFS() embed.FS { + return templatesFS +} From 6bc791506a1ac9580c36b4395950304786541940 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 4 Jul 2023 22:17:04 -0400 Subject: [PATCH 2/2] =?UTF-8?q?Migrer=20exposition=20statique=20=C3=A0=20S?= =?UTF-8?q?taticWithConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajouter groupStatic pour StaticWithConfig Retirer handlers manuels redondants par StaticWithConfig --- cmd/server.go | 28 ++++++++-------------------- public/public.go | 8 ++++++++ templates/html/general.gohtml | 2 +- templates/html/index.gohtml | 2 +- templates/templates.go | 8 ++++++++ 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index fab4eaf..770d970 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -134,6 +134,14 @@ func RunServer() { e.Pre(middleware.RemoveTrailingSlash()) + groupStatic := e.Group("/public/*") + groupStatic.Use(middleware.StaticWithConfig(middleware.StaticConfig{ + Root: "/", + Filesystem: http.FS(publicFS), + //TODO + //Browse: true, + })) + groupV1 := e.Group("/v1") groupV1.Use(middleware.AddTrailingSlash()) @@ -193,12 +201,6 @@ func RunServer() { groupV1.DELETE("/bucket/:bucket/:document", handleV1DocumentDelete) - // Static Routes - - e.GET("/static/general.css", handleStaticCSSGeneral) - - e.GET("/static/index.css", handleStaticCSSIndex) - // HTML Routes e.GET("/", handleIndex) @@ -745,17 +747,3 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: message}) } - -// CSS Handlers - -func handleStaticCSSIndex(c echo.Context) error { - // TODO Ajouter gestion d'erreurs - data, _ := publicFS.ReadFile("css/index.css") - return c.Blob(http.StatusOK, "text/css", data) -} - -func handleStaticCSSGeneral(c echo.Context) error { - // TODO Ajouter gestion d'erreurs - data, _ := publicFS.ReadFile("css/general.css") - return c.Blob(http.StatusOK, "text/css", data) -} diff --git a/public/public.go b/public/public.go index a2ab158..fe44412 100644 --- a/public/public.go +++ b/public/public.go @@ -1,3 +1,11 @@ +/* +Package public contient les fichiers css et js exposés publiquement par l'application. + +Le contenu sera embedded dans le fichier binaire, dans le but de bundle les +dépendances avec l'application, simplifiant son déploiement. + +Une copie du contenu peut être obtenue par un appel de GetPublicFS(). +*/ package public import "embed" diff --git a/templates/html/general.gohtml b/templates/html/general.gohtml index 1683ce3..1eafbe0 100644 --- a/templates/html/general.gohtml +++ b/templates/html/general.gohtml @@ -1,3 +1,3 @@ {{ define "general-html" }} - + {{ end }} diff --git a/templates/html/index.gohtml b/templates/html/index.gohtml index fdeef06..b1306e1 100644 --- a/templates/html/index.gohtml +++ b/templates/html/index.gohtml @@ -5,7 +5,7 @@ AGECEM {{ template "general-html" }} - + {{ template "header-html" }} diff --git a/templates/templates.go b/templates/templates.go index d848e80..89dc221 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -1,3 +1,11 @@ +/* +Package templates contient les fichiers gohtml à templater par l'application. + +Le contenu sera embedded dans le fichier binaire, dans le but de bundle les +dépendances avec l'application, simplifiant son déploiement. + +Une copie du contenu peut être obtenue par un appel de GetTemplatesFS(). +*/ package templates import "embed"