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 @@ - +
- - - - - - - - - - - - -Key | +Value | +
---|---|
{{.Key}} | {{.Value}} | +
No data | +