Add ability to query the article index

This commit is contained in:
2025-02-12 17:55:44 +01:00
parent 0cd964343b
commit d6c58cf532
3 changed files with 21 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package calls
import (
"encoding/json"
"fmt"
"log"
"net/http"
@ -109,3 +110,21 @@ func ServeClicks(db *b.DB) http.HandlerFunc {
}
}
}
func QueryArticles(i *b.Index) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
result, err := i.Query(r.PathValue("query"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintf("error encoding results to JSON: %v", err), http.StatusInternalServerError)
return
}
}
}