Revamp templates, allow loading external templates, support themes

This commit is contained in:
WeebDataHoarder
2025-04-03 04:39:33 +02:00
parent 150927e7ba
commit f9634cfc71
9 changed files with 41 additions and 16 deletions

View File

@@ -50,15 +50,23 @@ func init() {
if err != nil {
panic(err)
}
tpl := template.New(e.Name())
_, err = tpl.Parse(string(data))
err = initTemplate(e.Name(), string(data))
if err != nil {
panic(err)
}
templates[e.Name()] = tpl
}
}
func initTemplate(name, data string) error {
tpl := template.New(name)
_, err := tpl.Parse(data)
if err != nil {
return err
}
templates[name] = tpl
return nil
}
func makeReverseProxy(target string) (http.Handler, error) {
u, err := url.Parse(target)
if err != nil {

View File

@@ -30,6 +30,8 @@ import (
"net"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
@@ -96,8 +98,9 @@ type ChallengeState struct {
}
type StateSettings struct {
PackagePath string
ChallengeTemplate string
PackagePath string
ChallengeTemplate string
ChallengeTemplateTheme string
}
func NewState(p policy.Policy, settings StateSettings) (state *State, err error) {
@@ -131,6 +134,16 @@ func NewState(p policy.Policy, settings StateSettings) (state *State, err error)
}
if templates["challenge-"+state.Settings.ChallengeTemplate+".gohtml"] == nil {
if data, err := os.ReadFile(state.Settings.ChallengeTemplate); err == nil && len(data) > 0 {
name := path.Base(state.Settings.ChallengeTemplate)
err := initTemplate(name, string(data))
if err != nil {
return nil, fmt.Errorf("error loading template %s: %w", settings.ChallengeTemplate, err)
}
state.Settings.ChallengeTemplate = name
}
return nil, fmt.Errorf("no template defined for %s", settings.ChallengeTemplate)
}