2022-05-31 15:19:37 +00:00
|
|
|
package fact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
2024-03-13 16:24:07 +00:00
|
|
|
"github.com/ggicci/httpin"
|
2022-05-31 15:19:37 +00:00
|
|
|
"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")
|
2022-05-31 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 16:24:07 +00:00
|
|
|
type SearchReq struct {
|
|
|
|
Query string `in:"query"`
|
2022-05-31 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-05-31 15:19:37 +00:00
|
|
|
|
2024-03-13 16:24:07 +00:00
|
|
|
entries, err := getFacts(p.db, input.Query, "")
|
2022-05-31 15:19:37 +00:00
|
|
|
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)
|
2022-05-31 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-05-31 15:19:37 +00:00
|
|
|
}
|