mirror of https://github.com/velour/catbase.git
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
|
package fact
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// Register any web URLs desired
|
||
|
func (p *FactoidPlugin) registerWeb() {
|
||
|
r := chi.NewRouter()
|
||
|
r.HandleFunc("/api", p.serveAPI)
|
||
|
r.HandleFunc("/req", p.serveQuery)
|
||
|
r.HandleFunc("/", p.serveQuery)
|
||
|
p.Bot.RegisterWebName(r, "/factoid", "Factoid")
|
||
|
}
|
||
|
|
||
|
func linkify(text string) template.HTML {
|
||
|
parts := strings.Fields(text)
|
||
|
for i, word := range parts {
|
||
|
if strings.HasPrefix(word, "http") {
|
||
|
parts[i] = fmt.Sprintf("<a href=\"%s\">%s</a>", word, word)
|
||
|
}
|
||
|
}
|
||
|
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.store, info.Query, "")
|
||
|
if err != nil {
|
||
|
w.WriteHeader(500)
|
||
|
fmt.Fprint(w, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
log.Debug().Msgf("api request got %d entries", len(entries))
|
||
|
|
||
|
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) {
|
||
|
index, _ := embeddedFS.ReadFile("index.html")
|
||
|
w.Write(index)
|
||
|
}
|