75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"text/template"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
var FrontendFiles embed.FS
|
|
|
|
func getFileContent() string {
|
|
content, err := FrontendFiles.ReadFile("frontend/index.html")
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return string(content)
|
|
}
|
|
|
|
func getIndex(c echo.Context) error {
|
|
IndexTemplate := template.Must(template.New("index").Parse(getFileContent()))
|
|
|
|
var tpl bytes.Buffer
|
|
IndexTemplate.Execute(&tpl, Config.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)
|
|
}
|
|
|
|
type Template struct {
|
|
templates *template.Template
|
|
}
|
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
}
|
|
|
|
func ServeTemplate(c echo.Context) error {
|
|
fmt.Println(c.Request().URL)
|
|
return c.Render(http.StatusOK, "style.css", Config.Template)
|
|
}
|
|
|
|
func StartTemplating(e *echo.Echo) {
|
|
// register templates as renderer
|
|
t := &Template{
|
|
templates: template.Must(template.ParseFS(
|
|
FrontendFiles,
|
|
"frontend/index.html",
|
|
"frontend/**/*.css",
|
|
"frontend/**/*.js",
|
|
)),
|
|
}
|
|
fmt.Println(t.templates.ParseName)
|
|
e.Renderer = t
|
|
|
|
e.GET("/*", ServeTemplate)
|
|
e.GET("/**/*", ServeTemplate)
|
|
}
|