From c32738f4440b662481bb461277043b1f66833e9e Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Thu, 17 Aug 2023 15:43:27 -0400 Subject: [PATCH] admin: convert variables page to htmx --- bot/bot.go | 2 + bot/nav.html | 22 +++++++++ bot/web.go | 17 +++++++ plugins/admin/vars.html | 99 ++++++++++++----------------------------- plugins/admin/web.go | 28 +++++++++++- plugins/fact/factoid.go | 1 - 6 files changed, 96 insertions(+), 73 deletions(-) create mode 100644 bot/nav.html diff --git a/bot/bot.go b/bot/bot.go index 98a5824..d8a3b51 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -147,6 +147,8 @@ func (b *bot) setupHTTP() { b.router.HandleFunc("/", b.serveRoot) b.router.HandleFunc("/nav", b.serveNav) + b.router.HandleFunc("/navHTML", b.serveNavHTML) + b.router.HandleFunc("/navHTML/{currentPage}", b.serveNavHTML) } func (b *bot) ListenAndServe() { diff --git a/bot/nav.html b/bot/nav.html new file mode 100644 index 0000000..c3d9068 --- /dev/null +++ b/bot/nav.html @@ -0,0 +1,22 @@ + \ No newline at end of file diff --git a/bot/web.go b/bot/web.go index e32aa23..4d901a6 100644 --- a/bot/web.go +++ b/bot/web.go @@ -3,8 +3,12 @@ package bot import ( "embed" "encoding/json" + "fmt" + "github.com/go-chi/chi/v5" + "github.com/rs/zerolog/log" "net/http" "strings" + "text/template" ) //go:embed *.html @@ -15,6 +19,19 @@ func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { w.Write(index) } +func (b *bot) serveNavHTML(w http.ResponseWriter, r *http.Request) { + currentPage := chi.URLParam(r, "currentPage") + tpl := template.Must(template.ParseFS(embeddedFS, "nav.html")) + if err := tpl.Execute(w, struct { + CurrentPage string + Items []EndPoint + }{currentPage, b.GetWebNavigation()}); err != nil { + log.Error().Err(err).Msg("template error") + w.WriteHeader(500) + fmt.Fprint(w, "Error parsing nav template") + } +} + func (b *bot) serveNav(w http.ResponseWriter, r *http.Request) { enc := json.NewEncoder(w) err := enc.Encode(b.GetWebNavigation()) diff --git a/plugins/admin/vars.html b/plugins/admin/vars.html index eded1ce..a401642 100644 --- a/plugins/admin/vars.html +++ b/plugins/admin/vars.html @@ -1,77 +1,36 @@ - + - - - - - - - - - - - - - Vars + + + vars + + +
+
-
- - Variables - - {{ item.name }} - - - - - {{ err }} - - - - -
- - + + + + + + + + + {{range .Items}} + + + + {{else}} + + + + {{end}} + +
KeyValue
{{.Key}}{{.Value}}
No data
+
+ \ No newline at end of file diff --git a/plugins/admin/web.go b/plugins/admin/web.go index b20f143..706edd9 100644 --- a/plugins/admin/web.go +++ b/plugins/admin/web.go @@ -6,6 +6,7 @@ import ( "embed" "encoding/json" "fmt" + "html/template" "io/ioutil" "net/http" "strings" @@ -166,9 +167,32 @@ func writeErr(w http.ResponseWriter, err error) { fmt.Fprint(w, string(j)) } +type configEntry struct { + Key string `json:"key"` + Value string `json:"value"` +} + func (p *AdminPlugin) handleVars(w http.ResponseWriter, r *http.Request) { - index, _ := embeddedFS.ReadFile("vars.html") - w.Write(index) + tpl := template.Must(template.ParseFS(embeddedFS, "vars.html")) + var configEntries []configEntry + q := `select key, value from config` + err := p.db.Select(&configEntries, q) + if err != nil { + log.Error(). + Err(err). + Msg("Error getting config entries.") + w.WriteHeader(500) + fmt.Fprint(w, err) + 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") + } } func (p *AdminPlugin) handleVarsAPI(w http.ResponseWriter, r *http.Request) { diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index e399624..ca1bddf 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -144,7 +144,6 @@ func (p *FactoidPlugin) findTrigger(fact string) (bool, *Factoid) { f, err := GetSingleFact(p.db, fact) if err != nil { - log.Error().Err(err).Msg("GetSingleFact") return findAlias(p.db, fact) } return true, f