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
This commit is contained in:
Franz Kafka 2026-03-21 16:10:42 +00:00
parent ebeaeeef21
commit 28b61ff251
12 changed files with 1013 additions and 8 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io/fs"
"log"
"log/slog"
"net/http"
@ -13,6 +14,7 @@ import (
"github.com/ashie/gosearch/internal/httpapi"
"github.com/ashie/gosearch/internal/middleware"
"github.com/ashie/gosearch/internal/search"
"github.com/ashie/gosearch/internal/views"
)
func main() {
@ -38,8 +40,7 @@ func main() {
defer searchCache.Close()
// Seed env vars from config so existing engine/factory/planner code
// picks them up without changes. The config layer is the single source
// of truth; env vars remain as overrides via applyEnvOverrides.
// picks them up without changes.
if len(cfg.Engines.LocalPorted) > 0 {
os.Setenv("LOCAL_PORTED_ENGINES", cfg.LocalPortedCSV())
}
@ -59,9 +60,18 @@ func main() {
h := httpapi.NewHandler(svc)
mux := http.NewServeMux()
mux.HandleFunc("/", h.Index)
mux.HandleFunc("/healthz", h.Healthz)
mux.HandleFunc("/search", h.Search)
// Serve embedded static files (CSS, JS, images).
staticFS, err := views.StaticFS()
if err != nil {
log.Fatalf("failed to load static files: %v", err)
}
var subFS fs.FS = staticFS
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(subFS))))
// Apply middleware: rate limiter → CORS → handler.
var handler http.Handler = mux
handler = middleware.CORS(middleware.CORSConfig{