Merge pull request 'Migrer exposition statique à StaticWithConfig' (#69) from refactor/static into main
Reviewed-on: #69
This commit is contained in:
commit
891cddd51e
15 changed files with 52 additions and 31 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
{{ define "general-html" }}
|
||||
<link rel="stylesheet" href="/static/general.css">
|
||||
{{ end }}
|
|
@ -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
|
||||
}
|
||||
|
|
3
templates/html/general.gohtml
Normal file
3
templates/html/general.gohtml
Normal file
|
@ -0,0 +1,3 @@
|
|||
{{ define "general-html" }}
|
||||
<link rel="stylesheet" href="/public/css/general.css">
|
||||
{{ end }}
|
|
@ -5,7 +5,7 @@
|
|||
<meta charset="utf-8">
|
||||
<title>AGECEM</title>
|
||||
{{ template "general-html" }}
|
||||
<link rel="stylesheet" href="static/index.css">
|
||||
<link rel="stylesheet" href="/public/css/index.css">
|
||||
</head>
|
||||
<body>
|
||||
{{ template "header-html" }}
|
18
templates/templates.go
Normal file
18
templates/templates.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue