mirror of https://github.com/velour/catbase.git
fact: use httpin
This commit is contained in:
parent
28e9fc2480
commit
674e30125b
|
@ -3,10 +3,9 @@ package fact
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/ggicci/httpin"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed *.html
|
//go:embed *.html
|
||||||
|
@ -15,26 +14,20 @@ var embeddedFS embed.FS
|
||||||
// Register any web URLs desired
|
// Register any web URLs desired
|
||||||
func (p *FactoidPlugin) registerWeb() {
|
func (p *FactoidPlugin) registerWeb() {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Post("/search", p.handleSearch)
|
r.With(httpin.NewInput(SearchReq{})).
|
||||||
r.HandleFunc("/req", p.serveQuery)
|
Post("/search", p.handleSearch)
|
||||||
r.HandleFunc("/", p.serveQuery)
|
r.Get("/", p.serveQuery)
|
||||||
p.b.GetWeb().RegisterWebName(r, "/factoid", "Factoid")
|
p.b.GetWeb().RegisterWebName(r, "/factoid", "Factoid")
|
||||||
}
|
}
|
||||||
|
|
||||||
func linkify(text string) template.HTML {
|
type SearchReq struct {
|
||||||
parts := strings.Fields(text)
|
Query string `in:"query"`
|
||||||
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) handleSearch(w http.ResponseWriter, r *http.Request) {
|
func (p *FactoidPlugin) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.FormValue("query")
|
input := r.Context().Value(httpin.Input).(*SearchReq)
|
||||||
|
|
||||||
entries, err := getFacts(p.db, query, "")
|
entries, err := getFacts(p.db, input.Query, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
fmt.Fprint(w, err)
|
fmt.Fprint(w, err)
|
||||||
|
|
Loading…
Reference in New Issue