kafka/cmd/searxng-go/main.go
Franz Kafka 94322ceff4 feat: Valkey cache for search results
- Add internal/cache package using go-redis/v9 (Valkey-compatible)
- Cache keys are deterministic SHA-256 hashes of search parameters
- Cache wraps the Search() method: check cache → miss → execute → store
- Gracefully disabled if Valkey is unreachable or unconfigured
- Configurable TTL (default 5m), address, password, and DB index
- Environment variable overrides: VALKEY_ADDRESS, VALKEY_PASSWORD,
  VALKEY_DB, VALKEY_CACHE_TTL
- Structured JSON logging via slog throughout cache layer
- Refactored service.go: extract executeSearch() from Search() for clarity
- Update config.example.toml with [cache] section
- Add cache package tests (key generation, nop behavior)
2026-03-21 15:43:47 +00:00

67 lines
1.8 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"github.com/ashie/gosearch/internal/cache"
"github.com/ashie/gosearch/internal/config"
"github.com/ashie/gosearch/internal/httpapi"
"github.com/ashie/gosearch/internal/search"
)
func main() {
configPath := flag.String("config", "config.toml", "path to config.toml")
flag.Parse()
// Initialize structured logging.
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
slog.SetDefault(logger)
cfg, err := config.Load(*configPath)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
// Initialize Valkey cache.
searchCache := cache.New(cache.Config{
Address: cfg.Cache.Address,
Password: cfg.Cache.Password,
DB: cfg.Cache.DB,
DefaultTTL: cfg.CacheTTL(),
}, logger)
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.
if len(cfg.Engines.LocalPorted) > 0 {
os.Setenv("LOCAL_PORTED_ENGINES", cfg.LocalPortedCSV())
}
if cfg.Engines.Brave.APIKey != "" {
os.Setenv("BRAVE_API_KEY", cfg.Engines.Brave.APIKey)
}
if cfg.Engines.Brave.AccessToken != "" {
os.Setenv("BRAVE_ACCESS_TOKEN", cfg.Engines.Brave.AccessToken)
}
svc := search.NewService(search.ServiceConfig{
UpstreamURL: cfg.Upstream.URL,
HTTPTimeout: cfg.HTTPTimeout(),
Cache: searchCache,
})
h := httpapi.NewHandler(svc)
mux := http.NewServeMux()
mux.HandleFunc("/healthz", h.Healthz)
mux.HandleFunc("/search", h.Search)
addr := fmt.Sprintf(":%d", cfg.Server.Port)
logger.Info("searxng-go starting", "addr", addr, "cache", searchCache.Enabled())
log.Fatal(http.ListenAndServe(addr, mux))
}