From 344bcb9f642359db9f51d263acd8f9e4206a95d1 Mon Sep 17 00:00:00 2001
From: Chris Sexton <3216719+chrissexton@users.noreply.github.com>
Date: Tue, 27 Feb 2024 21:59:24 -0500
Subject: [PATCH] fact: templ and htmx
---
plugins/fact/fact.templ | 51 +++++++++++++
plugins/fact/fact_templ.go | 147 +++++++++++++++++++++++++++++++++++++
plugins/fact/web.go | 35 ++-------
3 files changed, 205 insertions(+), 28 deletions(-)
create mode 100644 plugins/fact/fact.templ
create mode 100644 plugins/fact/fact_templ.go
diff --git a/plugins/fact/fact.templ b/plugins/fact/fact.templ
new file mode 100644
index 0000000..b821bb7
--- /dev/null
+++ b/plugins/fact/fact.templ
@@ -0,0 +1,51 @@
+package fact
+
+import "fmt"
+
+templ (p *FactoidPlugin) factIndex() {
+
")
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ var templ_7745c5c3_Var4 string
+ templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(fact.Fact)
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 44, Col: 23}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
+ 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_Var5 string
+ templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(fact.Tidbit)
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 45, Col: 25}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
+ 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_Var6 string
+ templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fact.Owner)
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 46, Col: 24}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
+ 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_Var7 string
+ templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint(fact.Count))
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/fact/fact.templ`, Line: 47, Col: 36}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
+ 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
+ }
+ if !templ_7745c5c3_IsBuffer {
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
+ }
+ return templ_7745c5c3_Err
+ })
+}
diff --git a/plugins/fact/web.go b/plugins/fact/web.go
index 2a9fbc7..ba7069f 100644
--- a/plugins/fact/web.go
+++ b/plugins/fact/web.go
@@ -2,7 +2,6 @@ package fact
import (
"embed"
- "encoding/json"
"fmt"
"github.com/go-chi/chi/v5"
"html/template"
@@ -16,7 +15,7 @@ var embeddedFS embed.FS
// Register any web URLs desired
func (p *FactoidPlugin) registerWeb() {
r := chi.NewRouter()
- r.HandleFunc("/api", p.serveAPI)
+ r.Post("/search", p.handleSearch)
r.HandleFunc("/req", p.serveQuery)
r.HandleFunc("/", p.serveQuery)
p.b.GetWeb().RegisterWebName(r, "/factoid", "Factoid")
@@ -32,39 +31,19 @@ func linkify(text string) template.HTML {
return template.HTML(strings.Join(parts, " "))
}
-func (p *FactoidPlugin) serveAPI(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- fmt.Fprintf(w, "Incorrect HTTP method")
- return
- }
- info := struct {
- Query string `json:"query"`
- }{}
- decoder := json.NewDecoder(r.Body)
- err := decoder.Decode(&info)
+func (p *FactoidPlugin) handleSearch(w http.ResponseWriter, r *http.Request) {
+ query := r.FormValue("query")
+
+ entries, err := getFacts(p.db, query, "")
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, err)
return
}
- entries, err := getFacts(p.db, info.Query, "")
- if err != nil {
- w.WriteHeader(500)
- fmt.Fprint(w, err)
- return
- }
-
- data, err := json.Marshal(entries)
- if err != nil {
- w.WriteHeader(500)
- fmt.Fprint(w, err)
- return
- }
- w.Write(data)
+ p.searchResults(entries).Render(r.Context(), w)
}
func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) {
- index, _ := embeddedFS.ReadFile("index.html")
- w.Write(index)
+ p.b.GetWeb().Index("Fact", p.factIndex()).Render(r.Context(), w)
}