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 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,7 +127,7 @@ 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
@ -745,12 +750,12 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error {
func handleStaticCSSIndex(c echo.Context) error { func handleStaticCSSIndex(c echo.Context) error {
// TODO Ajouter gestion d'erreurs // 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) return c.Blob(http.StatusOK, "text/css", data)
} }
func handleStaticCSSGeneral(c echo.Context) error { func handleStaticCSSGeneral(c echo.Context) error {
// TODO Ajouter gestion d'erreurs // 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) return c.Blob(http.StatusOK, "text/css", data)
} }

View file

@ -2,9 +2,9 @@ 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
} }

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
}