feat: add /autocompleter endpoint for search suggestions
Some checks failed
Mirror to GitHub / mirror (push) Waiting to run
Tests / test (push) Waiting to run
Build and Push Docker Image / build-and-push (push) Has been cancelled

Proxies to upstream SearXNG /autocompleter if configured, otherwise
falls back to Wikipedia OpenSearch API. Returns a JSON array of
suggestion strings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ashisgreat22 2026-03-22 01:06:25 +01:00
parent 90810cb934
commit 9b280ad606
3 changed files with 157 additions and 4 deletions

View file

@ -1,7 +1,10 @@
package httpapi
import (
"context"
"encoding/json"
"net/http"
"strings"
"github.com/metamorphosis-dev/kafka/internal/contracts"
"github.com/metamorphosis-dev/kafka/internal/search"
@ -9,11 +12,15 @@ import (
)
type Handler struct {
searchSvc *search.Service
searchSvc *search.Service
autocompleteSvc func(ctx context.Context, query string) ([]string, error)
}
func NewHandler(searchSvc *search.Service) *Handler {
return &Handler{searchSvc: searchSvc}
func NewHandler(searchSvc *search.Service, autocompleteSuggestions func(ctx context.Context, query string) ([]string, error)) *Handler {
return &Handler{
searchSvc: searchSvc,
autocompleteSvc: autocompleteSuggestions,
}
}
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
@ -96,3 +103,21 @@ func (h *Handler) Search(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Autocompleter returns search suggestions for the given query.
func (h *Handler) Autocompleter(w http.ResponseWriter, r *http.Request) {
query := strings.TrimSpace(r.FormValue("q"))
if query == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
suggestions, err := h.autocompleteSvc(r.Context(), query)
if err != nil {
// Return empty list on error rather than an error status.
suggestions = []string{}
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(suggestions)
}