From b8e6e0595dfbe4daea5248f5aea2c7b66301b4be Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Tue, 27 Feb 2024 10:19:15 -0500 Subject: [PATCH] admin: vars use templ --- bot/web.go | 4 -- plugins/admin/vars.templ | 42 ++++++++++++++++++++ plugins/admin/vars_templ.go | 77 +++++++++++++++++++++++++++++++++++++ plugins/admin/web.go | 10 +---- 4 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 plugins/admin/vars_templ.go diff --git a/bot/web.go b/bot/web.go index 6b4f518..7898331 100644 --- a/bot/web.go +++ b/bot/web.go @@ -1,16 +1,12 @@ package bot import ( - "embed" "encoding/json" "github.com/go-chi/chi/v5" "net/http" "strings" ) -//go:embed *.html -var embeddedFS embed.FS - func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { b.index().Render(r.Context(), w) } diff --git a/plugins/admin/vars.templ b/plugins/admin/vars.templ index e69de29..1792804 100644 --- a/plugins/admin/vars.templ +++ b/plugins/admin/vars.templ @@ -0,0 +1,42 @@ +package admin + +templ vars(items []configEntry) { + + + + + + vars + + + + +
+ + +
+ + + + + + + + + for _, item := range items { + + + + } + if len(items) == 0 { + + + + } + +
KeyValue
{ item.Key }{ item.Value }
No data
+
+ + + +} \ No newline at end of file diff --git a/plugins/admin/vars_templ.go b/plugins/admin/vars_templ.go new file mode 100644 index 0000000..19dd1cf --- /dev/null +++ b/plugins/admin/vars_templ.go @@ -0,0 +1,77 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.543 +package admin + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import "context" +import "io" +import "bytes" + +func vars(items []configEntry) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("vars
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, item := range items { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if len(items) == 0 { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
KeyValue
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var2 string + templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(item.Key) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/vars.templ`, Line: 27, Col: 38} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(item.Value) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/vars.templ`, Line: 27, Col: 61} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
No data
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} diff --git a/plugins/admin/web.go b/plugins/admin/web.go index 706edd9..c6eafa2 100644 --- a/plugins/admin/web.go +++ b/plugins/admin/web.go @@ -6,7 +6,6 @@ import ( "embed" "encoding/json" "fmt" - "html/template" "io/ioutil" "net/http" "strings" @@ -173,7 +172,6 @@ type configEntry struct { } func (p *AdminPlugin) handleVars(w http.ResponseWriter, r *http.Request) { - tpl := template.Must(template.ParseFS(embeddedFS, "vars.html")) var configEntries []configEntry q := `select key, value from config` err := p.db.Select(&configEntries, q) @@ -186,13 +184,7 @@ func (p *AdminPlugin) handleVars(w http.ResponseWriter, r *http.Request) { return } - if err := tpl.Execute(w, struct { - Items []configEntry - }{configEntries}); err != nil { - log.Error().Err(err).Msg("template error") - w.WriteHeader(500) - fmt.Fprint(w, "Error parsing template") - } + vars(configEntries).Render(r.Context(), w) } func (p *AdminPlugin) handleVarsAPI(w http.ResponseWriter, r *http.Request) {