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..770d970 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,13 +127,21 @@ 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 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()) @@ -188,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) @@ -740,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, _ := embedFS.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") - return c.Blob(http.StatusOK, "text/css", data) -} diff --git a/public/html/general.gohtml b/public/html/general.gohtml deleted file mode 100644 index 1683ce3..0000000 --- a/public/html/general.gohtml +++ /dev/null @@ -1,3 +0,0 @@ -{{ define "general-html" }} - -{{ end }} diff --git a/public/public.go b/public/public.go index 60d9fe6..fe44412 100644 --- a/public/public.go +++ b/public/public.go @@ -1,10 +1,18 @@ +/* +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" -//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/templates/html/general.gohtml b/templates/html/general.gohtml new file mode 100644 index 0000000..1eafbe0 --- /dev/null +++ b/templates/html/general.gohtml @@ -0,0 +1,3 @@ +{{ define "general-html" }} + +{{ end }} 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 84% rename from public/html/index.gohtml rename to templates/html/index.gohtml index fdeef06..b1306e1 100644 --- a/public/html/index.gohtml +++ b/templates/html/index.gohtml @@ -5,7 +5,7 @@ AGECEM {{ template "general-html" }} - + {{ template "header-html" }} 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..89dc221 --- /dev/null +++ b/templates/templates.go @@ -0,0 +1,18 @@ +/* +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" + +//go:embed html/*.gohtml +var templatesFS embed.FS + +func GetTemplatesFS() embed.FS { + return templatesFS +}