Migrer exposition statique à StaticWithConfig #69

Merged
vlbeaudoin merged 2 commits from refactor/static into main 2023-07-04 22:19:58 -04:00
14 changed files with 26 additions and 9 deletions
Showing only changes of commit 2eee1f2fd2 - Show all commits

View file

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

View file

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

View file

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

10
templates/templates.go Normal file
View file

@ -0,0 +1,10 @@
package templates
import "embed"
//go:embed html/*.gohtml
var templatesFS embed.FS
func GetTemplatesFS() embed.FS {
return templatesFS
}