- remove the events/outages module, ArcGIS adapter, portal config and event statuses - add a configurable city dictionary/parser and make City part of StreetKey - seed: read city/city_name, namespace IDs by city, keep structures distinct - fix reliability: schema v2 with fail-closed load, deterministic ordering, input limits, config validation, atomic DB publish, LLM response caps - add read-only shadow mode comparing the old ES path with addrkit - breaking: registry.db must be re-seeded; config has no portal section
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
// Command addrkitd runs the address resolution service.
|
|
//
|
|
// It loads the canonical registry into memory, wires the optional LLM rewriter
|
|
// and serves the versioned JSON API. The process fails closed: if the registry
|
|
// cannot be loaded or is empty it exits rather than serving misleading
|
|
// "not found" answers.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.totmin.ru/en2zmax/addrkit/internal/adapters/httpapi"
|
|
"git.totmin.ru/en2zmax/addrkit/internal/adapters/llm"
|
|
"git.totmin.ru/en2zmax/addrkit/internal/adapters/registrystore"
|
|
"git.totmin.ru/en2zmax/addrkit/internal/config"
|
|
"git.totmin.ru/en2zmax/addrkit/internal/domain/address"
|
|
"git.totmin.ru/en2zmax/addrkit/internal/domain/match"
|
|
)
|
|
|
|
func main() {
|
|
cfgPath := flag.String("config", "config.json", "path to the JSON config file")
|
|
flag.Parse()
|
|
|
|
cfg, err := config.Load(*cfgPath)
|
|
if err != nil {
|
|
slog.Error("config", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
logger := newLogger(cfg.LogLevel)
|
|
|
|
version, entries, err := registrystore.Load(cfg.DataPath)
|
|
if err != nil {
|
|
// Fail closed: no usable registry, no service.
|
|
logger.Error("load registry", "error", err, "path", cfg.DataPath)
|
|
os.Exit(1)
|
|
}
|
|
if len(entries) == 0 {
|
|
logger.Error("registry is empty; refusing to serve", "path", cfg.DataPath)
|
|
os.Exit(1)
|
|
}
|
|
logger.Info("registry loaded", "version", version, "entries", len(entries))
|
|
|
|
index := match.NewIndex(version, entries, match.Options{
|
|
FuzzyThreshold: cfg.Resolve.FuzzyThreshold,
|
|
AmbiguityMargin: cfg.Resolve.AmbiguityMargin,
|
|
MaxCandidates: cfg.Resolve.MaxCandidates,
|
|
})
|
|
|
|
parser := address.NewParser(address.NewDictionary(cfg.Cities), cfg.DefaultCity)
|
|
|
|
var rewriter match.Rewriter
|
|
if cfg.LLM.Enabled {
|
|
rewriter = llm.New(cfg.LLM)
|
|
logger.Info("llm rewriter enabled", "url", cfg.LLM.URL, "model", cfg.LLM.Model)
|
|
}
|
|
resolver := match.NewResolverWithOptions(index, rewriter, match.ResolverOptions{
|
|
Parser: parser,
|
|
OnRewriteError: func(err error) {
|
|
logger.Warn("llm rewriter failed; using deterministic result", "error", err)
|
|
},
|
|
})
|
|
|
|
cities := resolver.Cities()
|
|
logger.Info("cities loaded", "count", len(cities), "cities", cities, "default", cfg.DefaultCity)
|
|
if cfg.DefaultCity != "" {
|
|
if len(cities) > 1 {
|
|
logger.Warn("default_city is set for a multi-city index; city-less queries will always use it", "default", cfg.DefaultCity)
|
|
} else if len(cities) == 1 && cities[0] != cfg.DefaultCity {
|
|
logger.Warn("default_city is not present in the index", "default", cfg.DefaultCity, "index_city", cities[0])
|
|
}
|
|
}
|
|
|
|
srv := httpapi.New(resolver, logger)
|
|
|
|
httpServer := &http.Server{
|
|
Addr: cfg.Addr,
|
|
Handler: srv.Handler(),
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 20 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
MaxHeaderBytes: 64 << 10,
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
go func() {
|
|
logger.Info("listening", "addr", cfg.Addr)
|
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
logger.Error("serve", "error", err)
|
|
stop()
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
logger.Info("shutting down")
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
logger.Error("shutdown", "error", err)
|
|
}
|
|
}
|
|
|
|
func newLogger(level string) *slog.Logger {
|
|
var lvl slog.Level
|
|
switch level {
|
|
case "debug":
|
|
lvl = slog.LevelDebug
|
|
case "warn":
|
|
lvl = slog.LevelWarn
|
|
case "error":
|
|
lvl = slog.LevelError
|
|
default:
|
|
lvl = slog.LevelInfo
|
|
}
|
|
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: lvl}))
|
|
}
|