Replace template-based handlers (h.Index, h.Preferences) with the new spa handler. API routes (healthz, search, autocompleter, opensearch.xml) are registered first as exact matches, followed by the SPA catchall handler for all other routes. Remove unused views and io/fs imports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
127 lines
4.3 KiB
Go
127 lines
4.3 KiB
Go
// kafka — a privacy-respecting metasearch engine
|
|
// Copyright (C) 2026-present metamorphosis-dev
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/metamorphosis-dev/kafka/internal/autocomplete"
|
|
"github.com/metamorphosis-dev/kafka/internal/cache"
|
|
"github.com/metamorphosis-dev/kafka/internal/config"
|
|
"github.com/metamorphosis-dev/kafka/internal/httpapi"
|
|
"github.com/metamorphosis-dev/kafka/internal/middleware"
|
|
"github.com/metamorphosis-dev/kafka/internal/search"
|
|
"github.com/metamorphosis-dev/kafka/internal/spa"
|
|
)
|
|
|
|
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.
|
|
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,
|
|
EnginesConfig: cfg,
|
|
})
|
|
|
|
acSvc := autocomplete.NewService(cfg.Upstream.URL, cfg.HTTPTimeout())
|
|
|
|
h := httpapi.NewHandler(svc, acSvc.Suggestions, cfg.Server.SourceURL)
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// API routes - handled by Go
|
|
mux.HandleFunc("/healthz", h.Healthz)
|
|
mux.HandleFunc("/search", h.Search)
|
|
mux.HandleFunc("/autocompleter", h.Autocompleter)
|
|
mux.HandleFunc("/opensearch.xml", h.OpenSearch(cfg.Server.BaseURL))
|
|
|
|
// SPA handler - serves React app for all other routes
|
|
spaHandler := spa.NewHandler()
|
|
mux.Handle("/", spaHandler)
|
|
|
|
// Apply middleware: global rate limit → burst rate limit → per-IP rate limit → CORS → security headers → handler.
|
|
var handler http.Handler = mux
|
|
handler = middleware.SecurityHeaders(middleware.SecurityHeadersConfig{})(handler)
|
|
handler = middleware.CORS(middleware.CORSConfig{
|
|
AllowedOrigins: cfg.CORS.AllowedOrigins,
|
|
AllowedMethods: cfg.CORS.AllowedMethods,
|
|
AllowedHeaders: cfg.CORS.AllowedHeaders,
|
|
ExposedHeaders: cfg.CORS.ExposedHeaders,
|
|
MaxAge: cfg.CORS.MaxAge,
|
|
})(handler)
|
|
handler = middleware.RateLimit(middleware.RateLimitConfig{
|
|
Requests: cfg.RateLimit.Requests,
|
|
Window: cfg.RateLimitWindow(),
|
|
CleanupInterval: cfg.RateLimitCleanupInterval(),
|
|
TrustedProxies: cfg.RateLimit.TrustedProxies,
|
|
}, logger)(handler)
|
|
handler = middleware.GlobalRateLimit(middleware.GlobalRateLimitConfig{
|
|
Requests: cfg.GlobalRateLimit.Requests,
|
|
Window: cfg.GlobalRateLimitWindow(),
|
|
}, logger)(handler)
|
|
handler = middleware.BurstRateLimit(middleware.BurstRateLimitConfig{
|
|
Burst: cfg.BurstRateLimit.Burst,
|
|
BurstWindow: cfg.BurstWindow(),
|
|
Sustained: cfg.BurstRateLimit.Sustained,
|
|
SustainedWindow: cfg.SustainedWindow(),
|
|
}, logger)(handler)
|
|
|
|
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
|
logger.Info("kafka starting",
|
|
"addr", addr,
|
|
"cache", searchCache.Enabled(),
|
|
"rate_limit", cfg.RateLimit.Requests > 0,
|
|
)
|
|
log.Fatal(http.ListenAndServe(addr, handler))
|
|
}
|