Merge pull request 'Migrer exposition statique à StaticWithConfig' (#69) from refactor/static into main

Reviewed-on: #69
This commit is contained in:
Victor Lacasse-Beaudoin 2023-07-04 21:19:57 -05:00
commit 891cddd51e
15 changed files with 52 additions and 31 deletions

View file

@ -16,6 +16,8 @@ ADD config/ config/
ADD media/ media/ ADD media/ media/
ADD templates/ templates/
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org . RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org .
# Alpine # Alpine

View file

@ -25,6 +25,7 @@ import (
"git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/config"
"git.agecem.com/agecem/agecem-org/media" "git.agecem.com/agecem/agecem-org/media"
"git.agecem.com/agecem/agecem-org/public" "git.agecem.com/agecem/agecem-org/public"
"git.agecem.com/agecem/agecem-org/templates"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
) )
@ -35,7 +36,10 @@ type Template struct {
var cfg config.Config var cfg config.Config
var embedFS embed.FS var (
publicFS embed.FS
templatesFS embed.FS
)
// serverCmd represents the server command // serverCmd represents the server command
var serverCmd = &cobra.Command{ var serverCmd = &cobra.Command{
@ -64,7 +68,8 @@ var serverCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(serverCmd) rootCmd.AddCommand(serverCmd)
embedFS = public.GetEmbedFS() publicFS = public.GetPublicFS()
templatesFS = templates.GetTemplatesFS()
// server.port - --server-port // server.port - --server-port
serverCmd.Flags().Int("server-port", 8080, "Port to run the webserver on (config: 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() e := echo.New()
t := &Template{ t := &Template{
templates: template.Must(template.ParseFS(embedFS, "html/*.gohtml")), templates: template.Must(template.ParseFS(templatesFS, "html/*.gohtml")),
} }
e.Renderer = t e.Renderer = t
e.Pre(middleware.RemoveTrailingSlash()) 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 := e.Group("/v1")
groupV1.Use(middleware.AddTrailingSlash()) groupV1.Use(middleware.AddTrailingSlash())
@ -188,12 +201,6 @@ func RunServer() {
groupV1.DELETE("/bucket/:bucket/:document", handleV1DocumentDelete) groupV1.DELETE("/bucket/:bucket/:document", handleV1DocumentDelete)
// Static Routes
e.GET("/static/general.css", handleStaticCSSGeneral)
e.GET("/static/index.css", handleStaticCSSIndex)
// HTML Routes // HTML Routes
e.GET("/", handleIndex) 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}) 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)
}

View file

@ -1,3 +0,0 @@
{{ define "general-html" }}
<link rel="stylesheet" href="/static/general.css">
{{ end }}

View file

@ -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 package public
import "embed" import "embed"
//go:embed html/* css/* js/* //go:embed css/*.css js/*.js
var embedFS embed.FS var publicFS embed.FS
func GetEmbedFS() embed.FS { func GetPublicFS() embed.FS {
return embedFS return publicFS
} }

View file

@ -0,0 +1,3 @@
{{ define "general-html" }}
<link rel="stylesheet" href="/public/css/general.css">
{{ end }}

View file

@ -5,7 +5,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>AGECEM</title> <title>AGECEM</title>
{{ template "general-html" }} {{ template "general-html" }}
<link rel="stylesheet" href="static/index.css"> <link rel="stylesheet" href="/public/css/index.css">
</head> </head>
<body> <body>
{{ template "header-html" }} {{ template "header-html" }}

18
templates/templates.go Normal file
View file

@ -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
}