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() { +
+
+
+
+ +
+
+ +
+
+
+
+
+
+} + +templ (p *FactoidPlugin) searchResults(facts []*Factoid) { + + + + + + + for _, f := range facts { + @p.searchResult(f) + } +
+ FactTidbitOwnerCount
+} + +templ (p *FactoidPlugin) searchResult(fact *Factoid) { + + { fact.Fact } + { fact.Tidbit } + { fact.Owner } + { fmt.Sprint(fact.Count) } + +} \ No newline at end of file diff --git a/plugins/fact/fact_templ.go b/plugins/fact/fact_templ.go new file mode 100644 index 0000000..6126552 --- /dev/null +++ b/plugins/fact/fact_templ.go @@ -0,0 +1,147 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.543 +package fact + +//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" + +import "fmt" + +func (p *FactoidPlugin) factIndex() 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("
") + 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 + }) +} + +func (p *FactoidPlugin) searchResults(facts []*Factoid) 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_Var2 := templ.GetChildren(ctx) + if templ_7745c5c3_Var2 == nil { + templ_7745c5c3_Var2 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, f := range facts { + templ_7745c5c3_Err = p.searchResult(f).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
FactTidbitOwnerCount
") + 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 + }) +} + +func (p *FactoidPlugin) searchResult(fact *Factoid) 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_Var3 := templ.GetChildren(ctx) + if templ_7745c5c3_Var3 == nil { + templ_7745c5c3_Var3 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + 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) }