Lots of small cleanups
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -58,7 +59,7 @@ func init() {
|
||||
l.Fatal("Unable to find templates")
|
||||
}
|
||||
|
||||
Templates = loadTemplates(templateFS, *externalResources == "")
|
||||
Templates = loadTemplates(templateFS, *externalResources != "")
|
||||
StaticFiles, err = fs.Sub(Resources, "static")
|
||||
if err != nil {
|
||||
l.Fatal("Cannot find static files", zap.Error(err))
|
||||
@@ -98,9 +99,19 @@ func extractResources(dst string) {
|
||||
})
|
||||
}
|
||||
|
||||
type AvailableTemplates int
|
||||
|
||||
const (
|
||||
ATHtml AvailableTemplates = 1 << iota
|
||||
ATText
|
||||
ATComboCount
|
||||
)
|
||||
|
||||
type TemplateSource struct {
|
||||
htmlCache *jet.Set
|
||||
textCache *jet.Set
|
||||
htmlCache *jet.Set
|
||||
textCache *jet.Set
|
||||
isDevMode bool
|
||||
availableContentTypes map[string]AvailableTemplates
|
||||
}
|
||||
|
||||
func loadTemplates(src fs.FS, devMode bool) TemplateSource {
|
||||
@@ -112,17 +123,63 @@ func loadTemplates(src fs.FS, devMode bool) TemplateSource {
|
||||
}
|
||||
|
||||
loader := genFsLoader{fs: src}
|
||||
return TemplateSource{
|
||||
htmlCache: jet.NewSet(loader, devModeOpt),
|
||||
textCache: jet.NewSet(loader, devModeOpt, jet.WithSafeWriter(nil)),
|
||||
res := TemplateSource{
|
||||
isDevMode: devMode,
|
||||
htmlCache: jet.NewSet(loader, devModeOpt, jet.WithTemplateNameExtensions([]string{".html", ".html.jet"})),
|
||||
textCache: jet.NewSet(loader, devModeOpt, jet.WithTemplateNameExtensions([]string{".txt", ".txt.jet"}), jet.WithSafeWriter(nil)),
|
||||
availableContentTypes: make(map[string]AvailableTemplates),
|
||||
}
|
||||
|
||||
res.htmlCache.AddGlobal("TemplateMode", "html")
|
||||
res.textCache.AddGlobal("TemplateMode", "text")
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (ts TemplateSource) HtmlTemplate(name string) (*jet.Template, error) {
|
||||
// Add a global variable available to all templates
|
||||
func (ts *TemplateSource) AddGlobalVar(name string, value interface{}) {
|
||||
ts.textCache.AddGlobal(name, value)
|
||||
ts.htmlCache.AddGlobal(name, value)
|
||||
}
|
||||
|
||||
// Add a global function available to HTML templates
|
||||
func (ts *TemplateSource) AddHtmlFunction(name string, value func(arguments jet.Arguments) reflect.Value) {
|
||||
ts.htmlCache.AddGlobalFunc(name, value)
|
||||
}
|
||||
|
||||
// Adds a global function available to text templates
|
||||
func (ts *TemplateSource) AddTextFunction(name string, value func(arguments jet.Arguments) reflect.Value) {
|
||||
ts.htmlCache.AddGlobalFunc(name, value)
|
||||
}
|
||||
|
||||
// Adds a global function available to all templates. Equivalent to calling AddHtmlFunction and AddTextFunction
|
||||
func (ts *TemplateSource) AddFunction(name string, value func(arguments jet.Arguments) reflect.Value) {
|
||||
ts.AddHtmlFunction(name, value)
|
||||
ts.AddTextFunction(name, value)
|
||||
}
|
||||
|
||||
func (ts *TemplateSource) GetContentTypes(name string) AvailableTemplates {
|
||||
if result, ok := ts.availableContentTypes[name]; ok {
|
||||
return result
|
||||
}
|
||||
var result AvailableTemplates
|
||||
if _, err := ts.HtmlTemplate(name); err == nil {
|
||||
result = result | ATHtml
|
||||
}
|
||||
if _, err := ts.TextTemplate(name); err == nil {
|
||||
result = result | ATText
|
||||
}
|
||||
if !ts.isDevMode {
|
||||
ts.availableContentTypes[name] = result
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (ts *TemplateSource) HtmlTemplate(name string) (*jet.Template, error) {
|
||||
return ts.htmlCache.GetTemplate(name)
|
||||
}
|
||||
|
||||
func (ts TemplateSource) TextTemplate(name string) (*jet.Template, error) {
|
||||
func (ts *TemplateSource) TextTemplate(name string) (*jet.Template, error) {
|
||||
return ts.textCache.GetTemplate(name)
|
||||
}
|
||||
|
||||
|
||||
27
resources/static/login/index.html
Normal file
27
resources/static/login/index.html
Normal file
@@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>SSO Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="password" method="post">
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="user">User</label></td>
|
||||
<td><input id="user" name="user" type="text"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="password">Password</label></td>
|
||||
<td><input id="password" name="password" type="password"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>
|
||||
<input type="submit" value="Log in">
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user