From 9c18a2172ad5922f803ead237e7149ff15e58193 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Thu, 28 Dec 2023 07:35:46 -0600 Subject: [PATCH] Fixing static file serving --- cmd/gohome/main.go | 12 ++++++++---- config.example.ini | 18 +++++------------- embed.go | 2 +- middleware.go | 1 + utils.go | 13 +++++++++++++ 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/cmd/gohome/main.go b/cmd/gohome/main.go index 009f3ca..c947de5 100644 --- a/cmd/gohome/main.go +++ b/cmd/gohome/main.go @@ -75,8 +75,15 @@ func run() error { gohome.Middleware(root), ) + staticHandler := http.StripPrefix(root, http.FileServer(http.FS(gohome.StaticFS))) + e.GET(fmt.Sprintf("%s/static/*", root), echo.WrapHandler(staticHandler)) + svc := e.Group(root) gohome.NewService(svc) + + srv.AddStaticFunc( + gohome.AddGlobalTmpl, + ) srv.AddFuncs(template.FuncMap{ "staticURL": func(path string) string { url, _ := url.JoinPath(config.StaticURL, path) @@ -96,20 +103,17 @@ func run() error { if err != nil { return err } - staticHandler := http.FileServer(http.FS(gohome.StaticFS)) - e.GET("/static/*", echo.WrapHandler(staticHandler)) if len(os.Args) > 1 { switch os.Args[1] { case "migrate": migrations := cmd.GetMigrations() - return migrate.Command(db, migrations, migrate.DOLLAR) + return migrate.Command(db, migrations, migrate.QUESTION) default: return fmt.Errorf("Unknown command '%s'", os.Args[1]) } } srv.Run() - return nil } diff --git a/config.example.ini b/config.example.ini index 4fca7be..be94bb2 100644 --- a/config.example.ini +++ b/config.example.ini @@ -17,19 +17,11 @@ email-service=console # Default: fs storage-service=fs -# Static URL -# default: /static -static-url=/static - -# Media URL -# default: /media -media-url=/media - # Admin email -admin-email=geeks@netlandish.com +admin-email=admin@yourdomain.com # Default email to send from -default-from-email=peter@netlandish.com +default-from-email=gohome@yourdomain.com # Email addmin errors? # Default: true @@ -38,12 +30,12 @@ email-admin-errors=true # Language default-language=en -scheme=http -domain=127.0.0.1:8000 +scheme=https +domain=yourdomain.com [database] # Path to sqlite3 db file -connection-string = gohome.db +connection-string = /path/to/gohome.db [storage] # Section for file storage variables diff --git a/embed.go b/embed.go index 74ac5e8..e016ed5 100644 --- a/embed.go +++ b/embed.go @@ -5,7 +5,7 @@ import "embed" var ( //go:embed templates TemplateFS embed.FS - //go:embed static/* + //go:embed static StaticFS embed.FS //go:embed migrations MigrateFS embed.FS diff --git a/middleware.go b/middleware.go index 0a78076..9a5afc1 100644 --- a/middleware.go +++ b/middleware.go @@ -46,6 +46,7 @@ func RepoMiddleware(next echo.HandlerFunc) echo.HandlerFunc { if !strings.HasPrefix(path, "/") { path = fmt.Sprintf("/%s", path) } + adminPath := c.Echo().Reverse("home:admin_home") if strings.HasPrefix(path, adminPath) { // Using the --admin-- url routes. Don't bother doing any diff --git a/utils.go b/utils.go index 0c1ddf9..7835ff5 100644 --- a/utils.go +++ b/utils.go @@ -1,7 +1,11 @@ package gohome import ( + "strings" + "github.com/labstack/echo/v4" + "netlandish.com/x/gobwebs" + "netlandish.com/x/gobwebs/server" "netlandish.com/x/gobwebs/sessions" ) @@ -28,3 +32,12 @@ func AuthLogout(c echo.Context) error { sm.Clear(c.Request().Context()) return nil } + +// AddGlobalTmpl adds global template variables +func AddGlobalTmpl(c echo.Context) gobwebs.Map { + gctx := c.(*server.Context) + gmap := gobwebs.Map{} + serverVersion := strings.Split(strings.Split(gctx.Server.AppInfo(), " ")[1], "-") + gmap["serverVersion"] = serverVersion[len(serverVersion)-1] + return gmap +} -- 2.45.2