kafka/internal/httpapi/handlers.go
Franz Kafka 28b61ff251 feat: HTMX + Go Templates HTML frontend
- Add internal/views/ package with embedded templates and static files
- Go html/template with SearXNG-compatible CSS class names
- Dark mode via prefers-color-scheme, responsive layout, print styles
- HTMX integration:
  - Debounced instant search (500ms) on the search input
  - Form submission targets #results via hx-post
  - Pagination buttons are HTMX-powered (swap results div only)
  - HX-Request header detection for fragment vs full page rendering
- Template structure:
  - base.html: full page layout with HTMX script, favicon, CSS
  - index.html: homepage with centered search box
  - results.html: full results page (wraps base + results_inner)
  - results_inner.html: results fragment (HTMX partial + sidebar + pagination)
  - result_item.html: reusable result article partial
- Smart format detection: browser requests (Accept: text/html) default to HTML,
  API clients default to JSON
- Static files served at /static/ from embedded FS (CSS, favicon SVG)
- Index route at GET /
- Empty query on HTML format redirects to homepage
- Custom CSS (gosearch.css): clean, minimal, privacy-respecting aesthetic
  with light/dark mode, responsive breakpoints, print stylesheet
- Add views package tests
2026-03-21 16:10:42 +00:00

85 lines
2.2 KiB
Go

package httpapi
import (
"net/http"
"github.com/ashie/gosearch/internal/contracts"
"github.com/ashie/gosearch/internal/search"
"github.com/ashie/gosearch/internal/views"
)
type Handler struct {
searchSvc *search.Service
}
func NewHandler(searchSvc *search.Service) *Handler {
return &Handler{searchSvc: searchSvc}
}
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}
// Index renders the homepage with the search box.
func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
if err := views.RenderIndex(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (h *Handler) Search(w http.ResponseWriter, r *http.Request) {
// For HTML format with no query, redirect to homepage.
if r.FormValue("q") == "" && (r.FormValue("format") == "" || r.FormValue("format") == "html") {
http.Redirect(w, r, "/", http.StatusFound)
return
}
req, err := search.ParseSearchRequest(r)
if err != nil {
// For HTML, render error on the results page.
if req.Format == contracts.FormatHTML || r.FormValue("format") == "html" {
pd := views.PageData{Query: r.FormValue("q")}
if views.IsHTMXRequest(r) {
views.RenderSearchFragment(w, pd)
} else {
views.RenderSearch(w, pd)
}
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp, err := h.searchSvc.Search(r.Context(), req)
if err != nil {
if req.Format == contracts.FormatHTML {
pd := views.PageData{Query: req.Query}
if views.IsHTMXRequest(r) {
views.RenderSearchFragment(w, pd)
} else {
views.RenderSearch(w, pd)
}
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if req.Format == contracts.FormatHTML {
pd := views.FromResponse(resp, req.Query, req.Pageno)
if err := views.RenderSearchAuto(w, r, pd); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
if err := search.WriteSearchResponse(w, req.Format, resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}