~migadu/alps

1988b2a15860c21da7d2f57aa2e8fae2108b81c8 — Alex McGrath 14 days ago 1af693c master
make path to themes configurable via ldflags
3 files changed, 17 insertions(+), 13 deletions(-)

M cmd/alps/main.go
M renderer.go
M server.go
M cmd/alps/main.go => cmd/alps/main.go +3 -0
@@ 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)

M renderer.go => renderer.go +7 -7
@@ 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,
	}
}

M server.go => server.go +7 -6
@@ 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
}