implemented start server

This commit is contained in:
Hazel Noack 2025-07-16 12:31:47 +02:00
parent f574a00a8f
commit 94b3f4c0f2
3 changed files with 16 additions and 110 deletions

View File

@ -1,8 +1,18 @@
package cli
import "log"
import (
"log"
"os"
"gitea.elara.ws/Hazel/transfem-startpage/internal/server"
)
func Start() error {
log.Println("starting server")
return nil
profile := "default"
if len(os.Args) > 2 {
profile = os.Args[2]
}
log.Println("starting server with profile " + profile)
return server.Start(profile)
}

View File

@ -3,10 +3,8 @@ package rendering
import (
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"gitea.elara.ws/Hazel/transfem-startpage/internal/diyhrt"
"github.com/pelletier/go-toml"
@ -102,17 +100,6 @@ func NewConfig() Config {
}
}
func (c *Config) LoadDiyHrt(listings []diyhrt.Listing) {
existingStores := make(map[int]diyhrt.Store)
for _, listing := range listings {
existingStores[listing.Store.Id] = listing.Store
}
c.Template.Listings = c.DiyHrt.ListingFilter.Filter(listings)
c.Template.Stores = c.DiyHrt.StoreFilter.Filter(slices.Collect(maps.Values(existingStores)))
}
func (rc *Config) ScanForConfigFile(profile string) error {
profileFile := profile + ".toml"

97
main.go
View File

@ -1,107 +1,16 @@
package main
import (
"bytes"
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"strconv"
"gitea.elara.ws/Hazel/transfem-startpage/internal/cli"
"gitea.elara.ws/Hazel/transfem-startpage/internal/diyhrt"
"gitea.elara.ws/Hazel/transfem-startpage/internal/rendering"
"github.com/labstack/echo/v4"
"gitea.elara.ws/Hazel/transfem-startpage/internal/server"
)
var CurrentConfig = rendering.NewConfig()
func FetchDiyHrt() error {
fmt.Println("Fetch DiyHrt Marketplaces...")
l, err := diyhrt.GetListings(CurrentConfig.DiyHrt.ApiKey)
if err != nil {
return err
}
CurrentConfig.LoadDiyHrt(l)
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
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 backMain() {
profile := "default"
if len(os.Args) > 1 {
profile = os.Args[1]
}
fmt.Println("loading profile " + profile)
err := CurrentConfig.ScanForConfigFile(profile)
if err != nil {
fmt.Println(err)
}
err = CurrentConfig.Init()
if err != nil {
fmt.Println(err)
}
err = FetchDiyHrt()
if err != nil {
fmt.Println(err)
}
e := echo.New()
// statically serve the file
cacheDir, err := rendering.GetCacheDir()
if err == nil {
e.Static("/cache", cacheDir)
} else {
fmt.Println(err)
}
// 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)))
}
var FrontendFiles embed.FS
func main() {
server.FrontendFiles = FrontendFiles
cli.Cli()
}