Hazel Noack 4f44974c58 draft
2025-08-01 10:45:26 +02:00

83 lines
1.8 KiB
Go

package server
import (
"log"
"net/http"
"path/filepath"
"strconv"
"time"
"gitea.elara.ws/Hazel/transfem-startpage/internal/rendering"
)
var Config = rendering.NewConfig()
func StartFetching() {
for {
log.Println("Fetch DiyHrt data...")
Config.FetchDiyHrt()
time.Sleep(time.Duration(Config.DiyHrt.FetchIntervals) * time.Second)
if Config.DiyHrt.FetchIntervals == 0 {
break
}
}
}
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 {
return err
}
go StartFetching()
err = Config.FetchDiyHrt()
if err != nil {
log.Println(err)
}
http.HandleFunc("/static", staticHandler)
http.ListenAndServe(":"+strconv.Itoa(Config.Server.Port), nil)
/*
e := echo.New()
// 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
}