draft
This commit is contained in:
@@ -2,12 +2,12 @@ package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.elara.ws/Hazel/transfem-startpage/internal/cache"
|
||||
"gitea.elara.ws/Hazel/transfem-startpage/internal/rendering"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
var Config = rendering.NewConfig()
|
||||
@@ -24,6 +24,16 @@ func StartFetching() {
|
||||
}
|
||||
}
|
||||
|
||||
func GetFilepath(u string) string {
|
||||
return filepath.Join("frontend", u)
|
||||
}
|
||||
|
||||
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
||||
filepath := GetFilepath(r.URL.Path)
|
||||
log.Println("serving file:", filepath)
|
||||
http.ServeFileFS(w, r, FrontendFiles, filepath)
|
||||
}
|
||||
|
||||
func Start(profile string) error {
|
||||
err := Config.ScanForConfigFile(profile)
|
||||
if err != nil {
|
||||
@@ -37,33 +47,36 @@ func Start(profile string) error {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
http.HandleFunc("/static", staticHandler)
|
||||
http.ListenAndServe(":"+strconv.Itoa(Config.Server.Port), nil)
|
||||
|
||||
// statically serve the file
|
||||
cache := cache.NewCache(profile)
|
||||
if !cache.Disabled {
|
||||
cache.StartStaticServer(e)
|
||||
|
||||
log.Println("downloading website icons...")
|
||||
for i, w := range Config.Template.Websites {
|
||||
u, err := cache.CacheUrl(w.ImageUrl)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
Config.Template.Websites[i].ImageUrl = u
|
||||
Config.Template.Websites[i].IsFetched = true
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
*/
|
||||
StartTemplating(e)
|
||||
e := echo.New()
|
||||
|
||||
e.Logger.Fatal(e.Start(":" + strconv.Itoa(Config.Server.Port)))
|
||||
// statically serve the file
|
||||
cache := cache.NewCache(profile)
|
||||
if !cache.Disabled {
|
||||
cache.StartStaticServer(e)
|
||||
|
||||
log.Println("downloading website icons...")
|
||||
for i, w := range Config.Template.Websites {
|
||||
u, err := cache.CacheUrl(w.ImageUrl)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
Config.Template.Websites[i].ImageUrl = u
|
||||
Config.Template.Websites[i].IsFetched = true
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
StartTemplating(e)
|
||||
|
||||
e.Logger.Fatal(e.Start(":" + strconv.Itoa(Config.Server.Port)))
|
||||
*/
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -51,24 +52,29 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
var t *template.Template
|
||||
|
||||
func ServeTemplate(c echo.Context) error {
|
||||
fmt.Println(c.Request().URL)
|
||||
return c.Render(http.StatusOK, "style.css", Config.Template)
|
||||
filename := filepath.Base(c.Request().URL.Path)
|
||||
if filename == "/" {
|
||||
filename = "index.html"
|
||||
}
|
||||
fmt.Println(filename)
|
||||
|
||||
var tpl bytes.Buffer
|
||||
t.ExecuteTemplate(&tpl, filename, Config.Template)
|
||||
return c.HTML(http.StatusOK, tpl.String())
|
||||
}
|
||||
|
||||
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
|
||||
t = template.Must(template.ParseFS(
|
||||
FrontendFiles,
|
||||
"frontend/templates/*",
|
||||
))
|
||||
fmt.Println(t.ParseName)
|
||||
|
||||
e.GET("/*", ServeTemplate)
|
||||
e.GET("/**/*", ServeTemplate)
|
||||
staticHandler := http.FileServer(getFileSystem())
|
||||
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/", staticHandler)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user