From 1988b2a15860c21da7d2f57aa2e8fae2108b81c8 Mon Sep 17 00:00:00 2001 From: Alex McGrath Date: Fri, 10 Sep 2021 09:12:38 +0100 Subject: [PATCH] make path to themes configurable via ldflags --- cmd/alps/main.go | 3 +++ renderer.go | 14 +++++++------- server.go | 13 +++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/alps/main.go b/cmd/alps/main.go index 3cfd560..5a00957 100644 --- a/cmd/alps/main.go +++ b/cmd/alps/main.go @@ -23,6 +23,8 @@ import ( _ "git.sr.ht/~migadu/alps/plugins/viewtext" ) +var themesPath = "./themes" + func main() { var ( addr string @@ -46,6 +48,7 @@ func main() { flag.Usage() return } + options.ThemesPath = themesPath if loginKey != "" { fernetKey, err := fernet.DecodeKey(loginKey) diff --git a/renderer.go b/renderer.go index d8ea325..140abae 100644 --- a/renderer.go +++ b/renderer.go @@ -12,8 +12,6 @@ import ( "github.com/labstack/echo/v4" ) -const themesDir = "themes" - // GlobalRenderData contains data available in all templates. type GlobalRenderData struct { Path []string @@ -113,6 +111,7 @@ func (brd *BaseRenderData) WithTitle(title string) *BaseRenderData { type renderer struct { logger echo.Logger + themesPath string defaultTheme string base *template.Template @@ -148,13 +147,13 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo. return t.ExecuteTemplate(w, name, data) } -func loadTheme(name string, base *template.Template) (*template.Template, error) { +func loadTheme(themesPath string, name string, base *template.Template) (*template.Template, error) { theme, err := base.Clone() if err != nil { return nil, err } - theme, err = theme.ParseGlob(themesDir + "/" + name + "/*.html") + theme, err = theme.ParseGlob(themesPath + "/" + name + "/*.html") if err != nil { return nil, err } @@ -173,7 +172,7 @@ func (r *renderer) Load(plugins []Plugin) error { themes := make(map[string]*template.Template) - files, err := ioutil.ReadDir(themesDir) + files, err := ioutil.ReadDir(r.themesPath) if err != nil && !os.IsNotExist(err) { return err } @@ -185,7 +184,7 @@ func (r *renderer) Load(plugins []Plugin) error { r.logger.Printf("Loading theme %q", fi.Name()) var err error - if themes[fi.Name()], err = loadTheme(fi.Name(), base); err != nil { + if themes[fi.Name()], err = loadTheme(r.themesPath, fi.Name(), base); err != nil { return fmt.Errorf("failed to load theme %q: %v", fi.Name(), err) } } @@ -201,9 +200,10 @@ func (r *renderer) Load(plugins []Plugin) error { return nil } -func newRenderer(logger echo.Logger, defaultTheme string) *renderer { +func newRenderer(logger echo.Logger, themesPath string, defaultTheme string) *renderer { return &renderer{ logger: logger, defaultTheme: defaultTheme, + themesPath: themesPath, } } diff --git a/server.go b/server.go index c4386d9..17018c1 100644 --- a/server.go +++ b/server.go @@ -204,7 +204,7 @@ func (s *Server) load() error { plugins = append(plugins, l...) } - renderer := newRenderer(s.e.Logger, s.Options.Theme) + renderer := newRenderer(s.e.Logger, s.Options.ThemesPath, s.Options.Theme) if err := renderer.Load(plugins); err != nil { return fmt.Errorf("failed to load templates: %v", err) } @@ -365,10 +365,11 @@ func handleUnauthenticated(next echo.HandlerFunc, ctx *Context) error { } type Options struct { - Upstreams []string - Theme string - Debug bool - LoginKey *fernet.Key + Upstreams []string + Theme string + ThemesPath string + Debug bool + LoginKey *fernet.Key } // New creates a new server. @@ -454,7 +455,7 @@ func New(e *echo.Echo, options *Options) (*Server, error) { } }) - e.Static("/themes", "themes") + e.Static("/themes", options.ThemesPath) return s, nil }