make path to themes configurable via ldflags

This commit is contained in:
Alex McGrath 2021-09-10 09:12:38 +01:00 committed by Simon Ser
parent 1af693ce53
commit 1988b2a158
3 changed files with 17 additions and 13 deletions

View file

@ -23,6 +23,8 @@ import (
_ "git.sr.ht/~migadu/alps/plugins/viewtext" _ "git.sr.ht/~migadu/alps/plugins/viewtext"
) )
var themesPath = "./themes"
func main() { func main() {
var ( var (
addr string addr string
@ -46,6 +48,7 @@ func main() {
flag.Usage() flag.Usage()
return return
} }
options.ThemesPath = themesPath
if loginKey != "" { if loginKey != "" {
fernetKey, err := fernet.DecodeKey(loginKey) fernetKey, err := fernet.DecodeKey(loginKey)

View file

@ -12,8 +12,6 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
const themesDir = "themes"
// GlobalRenderData contains data available in all templates. // GlobalRenderData contains data available in all templates.
type GlobalRenderData struct { type GlobalRenderData struct {
Path []string Path []string
@ -113,6 +111,7 @@ func (brd *BaseRenderData) WithTitle(title string) *BaseRenderData {
type renderer struct { type renderer struct {
logger echo.Logger logger echo.Logger
themesPath string
defaultTheme string defaultTheme string
base *template.Template 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) 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() theme, err := base.Clone()
if err != nil { if err != nil {
return nil, err return nil, err
} }
theme, err = theme.ParseGlob(themesDir + "/" + name + "/*.html") theme, err = theme.ParseGlob(themesPath + "/" + name + "/*.html")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -173,7 +172,7 @@ func (r *renderer) Load(plugins []Plugin) error {
themes := make(map[string]*template.Template) themes := make(map[string]*template.Template)
files, err := ioutil.ReadDir(themesDir) files, err := ioutil.ReadDir(r.themesPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return err return err
} }
@ -185,7 +184,7 @@ func (r *renderer) Load(plugins []Plugin) error {
r.logger.Printf("Loading theme %q", fi.Name()) r.logger.Printf("Loading theme %q", fi.Name())
var err error 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) 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 return nil
} }
func newRenderer(logger echo.Logger, defaultTheme string) *renderer { func newRenderer(logger echo.Logger, themesPath string, defaultTheme string) *renderer {
return &renderer{ return &renderer{
logger: logger, logger: logger,
defaultTheme: defaultTheme, defaultTheme: defaultTheme,
themesPath: themesPath,
} }
} }

View file

@ -204,7 +204,7 @@ func (s *Server) load() error {
plugins = append(plugins, l...) 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 { if err := renderer.Load(plugins); err != nil {
return fmt.Errorf("failed to load templates: %v", err) return fmt.Errorf("failed to load templates: %v", err)
} }
@ -365,10 +365,11 @@ func handleUnauthenticated(next echo.HandlerFunc, ctx *Context) error {
} }
type Options struct { type Options struct {
Upstreams []string Upstreams []string
Theme string Theme string
Debug bool ThemesPath string
LoginKey *fernet.Key Debug bool
LoginKey *fernet.Key
} }
// New creates a new server. // 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 return s, nil
} }