init
This commit is contained in:
63
internal/app/render.go
Normal file
63
internal/app/render.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Renderer struct {
|
||||
basePath string
|
||||
tpls *template.Template
|
||||
}
|
||||
|
||||
func NewRenderer(basePath string) *Renderer {
|
||||
funcs := template.FuncMap{
|
||||
"abs": func(p string) string {
|
||||
if basePath == "" {
|
||||
return p
|
||||
}
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
p = "/" + p
|
||||
}
|
||||
return basePath + p
|
||||
},
|
||||
"join": path.Join,
|
||||
}
|
||||
t := template.New("base").Funcs(funcs)
|
||||
t = template.Must(t.ParseFS(tfs(),
|
||||
"layout.html",
|
||||
"login.html",
|
||||
"users.html",
|
||||
"user.html",
|
||||
"access.html",
|
||||
"tokens.html",
|
||||
"admins.html",
|
||||
"audit.html",
|
||||
"error.html",
|
||||
))
|
||||
return &Renderer{basePath: basePath, tpls: t}
|
||||
}
|
||||
|
||||
type PageData struct {
|
||||
Title string
|
||||
Admin string
|
||||
Role string
|
||||
CSRF string
|
||||
Flash string
|
||||
Error string
|
||||
|
||||
Users any
|
||||
User any
|
||||
Tokens any
|
||||
Admins any
|
||||
Audit any
|
||||
Access any
|
||||
Next string
|
||||
}
|
||||
|
||||
func (r *Renderer) Render(w http.ResponseWriter, name string, data PageData) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_ = r.tpls.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
Reference in New Issue
Block a user