Revamp templates, allow loading external templates, support themes
This commit is contained in:
14
lib/http.go
14
lib/http.go
@@ -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 {
|
||||
|
17
lib/state.go
17
lib/state.go
@@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user