embedding index html

This commit is contained in:
Hazel Noack 2025-07-14 14:35:54 +02:00
parent 4eae59bd66
commit 228fed989e
2 changed files with 19 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()))

24
main.go
View File

@ -4,7 +4,9 @@ import (
"bytes"
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"strconv"
@ -27,16 +29,28 @@ 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())
}
//go:embed frontend/*
var frontendFiles embed.FS
func getFileSystem() http.FileSystem {
fsys, err := fs.Sub(frontendFiles, "frontend")
if err != nil {
@ -66,8 +80,8 @@ func main() {
}
e := echo.New()
// "frontend/assets"
// 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)))