From 4eae59bd66b4fa1642614fb784aae73cbb2e37e9 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Mon, 14 Jul 2025 14:30:59 +0200 Subject: [PATCH] using embeded file system for static files --- main.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d83bbb1..fdf2c5a 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "bytes" + "embed" "fmt" + "io/fs" "net/http" "os" "strconv" @@ -12,10 +14,8 @@ import ( "github.com/labstack/echo/v4" ) - var CurrentConfig = rendering.NewConfig() - func FetchDiyHrt() error { fmt.Println("Fetch DiyHrt Marketplaces...") @@ -34,6 +34,18 @@ func getIndex(c echo.Context) error { return c.HTML(http.StatusOK, tpl.String()) } +//go:embed frontend/* +var frontendFiles embed.FS + +func getFileSystem() http.FileSystem { + fsys, err := fs.Sub(frontendFiles, "frontend") + if err != nil { + panic(err) + } + + return http.FS(fsys) +} + func main() { fmt.Println("running transfem startpage") @@ -54,8 +66,11 @@ func main() { } e := echo.New() - e.Static("/assets", "frontend/assets") - e.Static("/scripts", "frontend/scripts") + // "frontend/assets" + + staticHandler := http.FileServer(getFileSystem()) + e.GET("/assets/*", echo.WrapHandler(http.StripPrefix("/", staticHandler))) + e.GET("/scripts/*", echo.WrapHandler(http.StripPrefix("/", staticHandler))) e.GET("/", getIndex) e.Logger.Fatal(e.Start(":" + strconv.Itoa(CurrentConfig.Server.Port)))