diff --git a/frontend/assets/bg.svg b/frontend/assets/bg.svg deleted file mode 100644 index 3cea37b..0000000 --- a/frontend/assets/bg.svg +++ /dev/null @@ -1,293 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - Meow - Meow - Meow - Meow - Meow - Meow - gock <333 - gock <333 - gock <333 - gock <333 - gock <333 - - Gay :3 - Gay :3 - Gay :3 - Gay :3 - Gay :3 - Gay :3 - - diff --git a/frontend/assets/girl_juice.png b/frontend/assets/girl_juice.png deleted file mode 100644 index e40d62f..0000000 Binary files a/frontend/assets/girl_juice.png and /dev/null differ diff --git a/frontend/index.html b/frontend/templates/index.html similarity index 100% rename from frontend/index.html rename to frontend/templates/index.html diff --git a/frontend/scripts/search.js b/frontend/templates/search.js similarity index 100% rename from frontend/scripts/search.js rename to frontend/templates/search.js diff --git a/frontend/assets/style.css b/frontend/templates/style.css similarity index 100% rename from frontend/assets/style.css rename to frontend/templates/style.css diff --git a/go.mod b/go.mod index fc0c587..f63818c 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,8 @@ require github.com/labstack/echo/v4 v4.13.4 require ( github.com/TwiN/go-color v1.4.1 // indirect + github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect + github.com/labstack/echo v3.3.10+incompatible // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 4748c27..11d5e71 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,12 @@ github.com/TwiN/go-color v1.4.1 h1:mqG0P/KBgHKVqmtL5ye7K0/Gr4l6hTksPgTgMk3mUzc= github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU= github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0= +github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= +github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA= github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= diff --git a/internal/server/server.go b/internal/server/server.go index 04e26dd..2286c60 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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 } diff --git a/internal/server/templates.go b/internal/server/templates.go index 12a675a..1b0c528 100644 --- a/internal/server/templates.go +++ b/internal/server/templates.go @@ -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))) }