catbase/plugins/fact/web.go

43 lines
918 B
Go
Raw Permalink Normal View History

package fact
import (
"embed"
"fmt"
2024-03-13 16:24:07 +00:00
"github.com/ggicci/httpin"
"github.com/go-chi/chi/v5"
"net/http"
)
//go:embed *.html
var embeddedFS embed.FS
// Register any web URLs desired
func (p *FactoidPlugin) registerWeb() {
r := chi.NewRouter()
2024-03-13 16:24:07 +00:00
r.With(httpin.NewInput(SearchReq{})).
Post("/search", p.handleSearch)
r.Get("/", p.serveQuery)
2024-02-27 19:29:54 +00:00
p.b.GetWeb().RegisterWebName(r, "/factoid", "Factoid")
}
2024-03-13 16:24:07 +00:00
type SearchReq struct {
2024-10-10 01:21:25 +00:00
Query string `in:"form=query"`
}
2024-02-28 02:59:24 +00:00
func (p *FactoidPlugin) handleSearch(w http.ResponseWriter, r *http.Request) {
2024-03-13 16:24:07 +00:00
input := r.Context().Value(httpin.Input).(*SearchReq)
2024-03-13 16:24:07 +00:00
entries, err := getFacts(p.db, input.Query, "")
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, err)
return
}
2024-02-28 02:59:24 +00:00
p.searchResults(entries).Render(r.Context(), w)
}
func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) {
2024-02-28 02:59:24 +00:00
p.b.GetWeb().Index("Fact", p.factIndex()).Render(r.Context(), w)
}