From ab3dac35ba36cca3b11d1c5a60fac5a71758ea06 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 27 May 2019 23:14:05 -0400 Subject: [PATCH] web: redo factoid page; add title to counter page --- plugins/counter/html.go | 1 + plugins/fact/factoid.go | 37 +++++++ plugins/fact/webTemplates.go | 181 +++++++++++++++++------------------ 3 files changed, 126 insertions(+), 93 deletions(-) diff --git a/plugins/counter/html.go b/plugins/counter/html.go index dca3964..3dd9b3a 100644 --- a/plugins/counter/html.go +++ b/plugins/counter/html.go @@ -14,6 +14,7 @@ var html = ` + Counters diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index eec9156..6546c7e 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -4,6 +4,7 @@ package fact import ( "database/sql" + "encoding/json" "fmt" "html/template" "math/rand" @@ -759,6 +760,7 @@ func (p *FactoidPlugin) factTimer(c bot.Connector, channel string) { // Register any web URLs desired func (p *FactoidPlugin) registerWeb() { + http.HandleFunc("/factoid/api", p.serveAPI) http.HandleFunc("/factoid/req", p.serveQuery) http.HandleFunc("/factoid", p.serveQuery) p.Bot.RegisterWeb("/factoid", "Factoid") @@ -773,8 +775,43 @@ 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) + 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) +} func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, factoidIndex) +} + +func (p *FactoidPlugin) serveQueryOld(w http.ResponseWriter, r *http.Request) { context := make(map[string]interface{}) funcMap := template.FuncMap{ // The name "title" is what the function will be called in the template text. diff --git a/plugins/fact/webTemplates.go b/plugins/fact/webTemplates.go index a2b2d25..c890664 100644 --- a/plugins/fact/webTemplates.go +++ b/plugins/fact/webTemplates.go @@ -9,106 +9,101 @@ package fact var factoidIndex = ` - + - Factoids - + + + - - - - - - - - + + + + + + + + + Factoids -
-
-
- Search for a factoid - - -
-
-
-
- - - {{if .Error}} - {{.Error}} - {{end}} - - {{if .Count}} - Found {{.Count}} entries. - {{end}} -
- - {{if .Entries}} -
- - - - - - - - - - - - {{range .Entries}} - - - - - - - {{end}} - -
TriggerFull TextAuthor# Hits
{{linkify .Fact}}{{linkify .Tidbit}}{{linkify .Owner}}{{.Count}}
-
- {{end}} - - + - `