mitbringsl/backend/internal/httpapi/suggest.go
Tronax 895725b5e5
Backend Phase C: Sync-Kern + Caddy behind-proxy
Sync-Kern:
- internal/sync/hlc.go: Hybrid Logical Clock (wall_ms<<16|counter)
  Tick/Now/After, global mutex, strikt monoton + kausal korrekt
- internal/sync/hlc_test.go: Unit-Tests (monoton, kausal, keine Duplikate)

Store-Schicht:
- internal/store/opstore.go: AppendOps idempotent via UNIQUE(client_id,
  client_seq) ON CONFLICT DO NOTHING; LWW-Projektion (list_create/
  rename/delete, item_add/update/remove) in derselben Transaktion;
  PullOps mit Cursor (seq > since, 500er Pages)
- internal/store/liststore.go: CreateList / GetLists / GetList
- internal/store/itemstore.go: GetItems (nicht-gelöschte Items)
- internal/store/suggeststore.go: Search (pg_trgm + LIKE-fallback, 10)

HTTP-Handler:
- internal/httpapi/lists.go: GET/POST /api/lists, GET /api/lists/{id}
- internal/httpapi/ops.go: POST /api/lists/{id}/ops (Push),
  GET /api/lists/{id}/ops (Pull ?since=)
- internal/httpapi/suggest.go: GET /api/suggestions?q=
- internal/httpapi/api.go: alle Routen verdrahtet (RequireAuth)

Deployment:
- deploy/Caddyfile.behind-proxy: auto_https off, trusted_proxies
- deploy/Caddyfile: X-Forwarded-Proto hinzugefügt, Kommentar aktualisiert
- deploy/docker-compose.yml: CADDY_HTTP_PORT + CADDY_HTTPS_PORT
- deploy/.env.example: Caddy-Port-Variablen dokumentiert

go build ./... && go vet ./... && go test ./... 
HLC-Tests: monoton, kausal, keine Duplikate 
AGENTS.md: Phase C vollständig 
2026-08-05 19:56:05 +02:00

47 lines
1.2 KiB
Go

package httpapi
import (
"log/slog"
"net/http"
"strings"
"github.com/mitbringsl/backend/internal/store"
)
// SuggestHandler handles the /api/suggestions endpoint.
type SuggestHandler struct {
suggest *store.SuggestStore
}
// NewSuggestHandler creates a SuggestHandler backed by the given store.
func NewSuggestHandler(suggest *store.SuggestStore) *SuggestHandler {
return &SuggestHandler{suggest: suggest}
}
type suggestResponse struct {
Suggestions []string `json:"suggestions"`
}
// Search returns autocomplete suggestions for item names matching the query.
//
// GET /api/suggestions?q=<query>
func (h *SuggestHandler) Search(w http.ResponseWriter, r *http.Request) {
q := strings.TrimSpace(r.URL.Query().Get("q"))
if q == "" {
renderError(w, http.StatusBadRequest, "Bad request", "Query parameter 'q' is required.")
return
}
// Lowercase so the trgm index can match correctly.
q = strings.ToLower(q)
names, err := h.suggest.Search(r.Context(), q)
if err != nil {
slog.Error("suggest search failed", "error", err, "q", q)
renderError(w, http.StatusInternalServerError, "Internal error", "Could not load suggestions.")
return
}
if names == nil {
names = []string{}
}
renderJSON(w, http.StatusOK, suggestResponse{Suggestions: names})
}