feat: migrate from env vars to config.toml

- Add internal/config package with TOML parsing (BurntSushi/toml)
- Create config.example.toml documenting all settings
- Update main.go to load config via -config flag (default: config.toml)
- Environment variables remain as fallback overrides for backward compat
- Config file values are used as defaults; env vars override when set
- Add comprehensive tests for file loading, defaults, and env overrides
- Add config.toml to .gitignore (secrets stay local)
This commit is contained in:
Franz Kafka 2026-03-21 14:56:00 +00:00
parent dc44837219
commit 8649864971
6 changed files with 329 additions and 16 deletions

View file

@ -1,33 +1,42 @@
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/ashie/gosearch/internal/config"
"github.com/ashie/gosearch/internal/httpapi"
"github.com/ashie/gosearch/internal/search"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
configPath := flag.String("config", "config.toml", "path to config.toml")
flag.Parse()
cfg, err := config.Load(*configPath)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
upstreamURL := os.Getenv("UPSTREAM_SEARXNG_URL")
timeout := 10 * time.Second
if v := os.Getenv("HTTP_TIMEOUT"); v != "" {
if d, err := time.ParseDuration(v); err == nil {
timeout = d
}
// 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: upstreamURL,
HTTPTimeout: timeout,
UpstreamURL: cfg.Upstream.URL,
HTTPTimeout: cfg.HTTPTimeout(),
})
h := httpapi.NewHandler(svc)
@ -36,8 +45,7 @@ func main() {
mux.HandleFunc("/healthz", h.Healthz)
mux.HandleFunc("/search", h.Search)
addr := ":" + port
addr := fmt.Sprintf(":%d", cfg.Server.Port)
log.Printf("searxng-go listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}