- 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)
163 lines
4.1 KiB
Go
163 lines
4.1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
// Config is the top-level configuration for the gosearch service.
|
|
type Config struct {
|
|
Server ServerConfig `toml:"server"`
|
|
Upstream UpstreamConfig `toml:"upstream"`
|
|
Engines EnginesConfig `toml:"engines"`
|
|
Cache CacheConfig `toml:"cache"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port int `toml:"port"`
|
|
HTTPTimeout string `toml:"http_timeout"`
|
|
}
|
|
|
|
type UpstreamConfig struct {
|
|
URL string `toml:"url"`
|
|
}
|
|
|
|
type EnginesConfig struct {
|
|
LocalPorted []string `toml:"local_ported"`
|
|
Brave BraveConfig `toml:"brave"`
|
|
Qwant QwantConfig `toml:"qwant"`
|
|
}
|
|
|
|
// CacheConfig holds Valkey/Redis cache settings.
|
|
type CacheConfig struct {
|
|
Address string `toml:"address"` // Valkey server address (e.g. "localhost:6379")
|
|
Password string `toml:"password"` // Auth password (empty = none)
|
|
DB int `toml:"db"` // Database index (default 0)
|
|
DefaultTTL string `toml:"default_ttl"` // Cache TTL (e.g. "5m", default "5m")
|
|
}
|
|
|
|
type BraveConfig struct {
|
|
APIKey string `toml:"api_key"`
|
|
AccessToken string `toml:"access_token"`
|
|
}
|
|
|
|
type QwantConfig struct {
|
|
Category string `toml:"category"`
|
|
ResultsPerPage int `toml:"results_per_page"`
|
|
}
|
|
|
|
// Load reads configuration from the given TOML file path.
|
|
// If the file does not exist, it returns defaults (empty values where applicable).
|
|
// Environment variables are used as fallbacks for any zero-value fields.
|
|
func Load(path string) (*Config, error) {
|
|
cfg := defaultConfig()
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
if _, err := toml.DecodeFile(path, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parse config %s: %w", path, err)
|
|
}
|
|
}
|
|
|
|
applyEnvOverrides(cfg)
|
|
return cfg, nil
|
|
}
|
|
|
|
func defaultConfig() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: 8080,
|
|
HTTPTimeout: "10s",
|
|
},
|
|
Upstream: UpstreamConfig{},
|
|
Engines: EnginesConfig{
|
|
LocalPorted: []string{"wikipedia", "arxiv", "crossref", "braveapi", "qwant"},
|
|
Qwant: QwantConfig{
|
|
Category: "web-lite",
|
|
ResultsPerPage: 10,
|
|
},
|
|
},
|
|
Cache: CacheConfig{
|
|
DB: 0,
|
|
DefaultTTL: "5m",
|
|
},
|
|
}
|
|
}
|
|
|
|
// applyEnvOverrides fills any zero-value fields from environment variables.
|
|
// This preserves backward compatibility: existing deployments using env vars
|
|
// continue to work without a config file.
|
|
func applyEnvOverrides(cfg *Config) {
|
|
if v := os.Getenv("PORT"); v != "" {
|
|
fmt.Sscanf(v, "%d", &cfg.Server.Port)
|
|
}
|
|
if v := os.Getenv("HTTP_TIMEOUT"); v != "" {
|
|
cfg.Server.HTTPTimeout = v
|
|
}
|
|
if v := os.Getenv("UPSTREAM_SEARXNG_URL"); v != "" {
|
|
cfg.Upstream.URL = v
|
|
}
|
|
if v := os.Getenv("LOCAL_PORTED_ENGINES"); v != "" {
|
|
parts := splitCSV(v)
|
|
if len(parts) > 0 {
|
|
cfg.Engines.LocalPorted = parts
|
|
}
|
|
}
|
|
if v := os.Getenv("BRAVE_API_KEY"); v != "" {
|
|
cfg.Engines.Brave.APIKey = v
|
|
}
|
|
if v := os.Getenv("BRAVE_ACCESS_TOKEN"); v != "" {
|
|
cfg.Engines.Brave.AccessToken = v
|
|
}
|
|
if v := os.Getenv("VALKEY_ADDRESS"); v != "" {
|
|
cfg.Cache.Address = v
|
|
}
|
|
if v := os.Getenv("VALKEY_PASSWORD"); v != "" {
|
|
cfg.Cache.Password = v
|
|
}
|
|
if v := os.Getenv("VALKEY_DB"); v != "" {
|
|
fmt.Sscanf(v, "%d", &cfg.Cache.DB)
|
|
}
|
|
if v := os.Getenv("VALKEY_CACHE_TTL"); v != "" {
|
|
cfg.Cache.DefaultTTL = v
|
|
}
|
|
}
|
|
|
|
// HTTPTimeout parses the configured timeout string into a time.Duration.
|
|
func (c *Config) HTTPTimeout() time.Duration {
|
|
if d, err := time.ParseDuration(c.Server.HTTPTimeout); err == nil && d > 0 {
|
|
return d
|
|
}
|
|
return 10 * time.Second
|
|
}
|
|
|
|
// LocalPortedCSV returns the local ported engines as a comma-separated string.
|
|
func (c *Config) LocalPortedCSV() string {
|
|
return strings.Join(c.Engines.LocalPorted, ",")
|
|
}
|
|
|
|
// CacheTTL parses the configured cache TTL string into a time.Duration.
|
|
func (c *Config) CacheTTL() time.Duration {
|
|
if d, err := time.ParseDuration(c.Cache.DefaultTTL); err == nil && d > 0 {
|
|
return d
|
|
}
|
|
return 5 * time.Minute
|
|
}
|
|
|
|
func splitCSV(s string) []string {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Split(s, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|