Compare commits

...

2 Commits

Author SHA1 Message Date
Hazel Noack
228fed989e embedding index html 2025-07-14 14:35:54 +02:00
Hazel Noack
4eae59bd66 using embeded file system for static files 2025-07-14 14:30:59 +02:00
2 changed files with 34 additions and 24 deletions

View File

@ -1,19 +0,0 @@
package rendering
import (
"html/template"
"log"
"os"
)
func getFileContent() string {
content, err := os.ReadFile("frontend/index.html")
if err != nil {
log.Fatal(err)
}
return string(content)
}
var IndexTemplate = template.Must(template.New("index").Parse(getFileContent()))

39
main.go
View File

@ -2,7 +2,11 @@ package main
import (
"bytes"
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"strconv"
@ -12,10 +16,8 @@ import (
"github.com/labstack/echo/v4"
)
var CurrentConfig = rendering.NewConfig()
func FetchDiyHrt() error {
fmt.Println("Fetch DiyHrt Marketplaces...")
@ -27,13 +29,37 @@ func FetchDiyHrt() error {
return nil
}
//go:embed frontend/*
var frontendFiles embed.FS
func getFileContent() string {
content, err := frontendFiles.ReadFile("frontend/index.html")
if err != nil {
log.Fatal(err)
}
return string(content)
}
var IndexTemplate = template.Must(template.New("index").Parse(getFileContent()))
func getIndex(c echo.Context) error {
var tpl bytes.Buffer
rendering.IndexTemplate.Execute(&tpl, CurrentConfig.Template)
IndexTemplate.Execute(&tpl, CurrentConfig.Template)
return c.HTML(http.StatusOK, tpl.String())
}
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 +80,11 @@ func main() {
}
e := echo.New()
e.Static("/assets", "frontend/assets")
e.Static("/scripts", "frontend/scripts")
// https://echo.labstack.com/docs/cookbook/embed-resources
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)))