refactor!: make addrkit a multi-city address normalizer
- 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
This commit is contained in:
+32
-32
@@ -1,9 +1,9 @@
|
||||
// Command addrkitd runs the address resolution service.
|
||||
//
|
||||
// It loads the canonical registry into memory, wires the optional adapters
|
||||
// (ArcGIS portal, LLM rewriter) and serves the versioned JSON API. The process
|
||||
// fails closed: if the registry cannot be loaded it exits rather than serving
|
||||
// an empty index.
|
||||
// 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 (
|
||||
@@ -17,13 +17,12 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.totmin.ru/en2zmax/addrkit/internal/adapters/arcgis"
|
||||
"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"
|
||||
"git.totmin.ru/en2zmax/addrkit/internal/domain/outages"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -40,10 +39,14 @@ func main() {
|
||||
|
||||
version, entries, err := registrystore.Load(cfg.DataPath)
|
||||
if err != nil {
|
||||
// Fail closed: no registry, no service.
|
||||
// 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{
|
||||
@@ -52,40 +55,47 @@ func main() {
|
||||
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.NewResolver(index, rewriter)
|
||||
resolver := match.NewResolverWithOptions(index, rewriter, match.ResolverOptions{
|
||||
Parser: parser,
|
||||
OnRewriteError: func(err error) {
|
||||
logger.Warn("llm rewriter failed; using deterministic result", "error", err)
|
||||
},
|
||||
})
|
||||
|
||||
var portal outages.Source
|
||||
if cfg.Portal.URL != "" {
|
||||
portal = arcgis.New(
|
||||
cfg.Portal.URL,
|
||||
time.Duration(cfg.Portal.TimeoutSeconds)*time.Second,
|
||||
cfg.Portal.Retries,
|
||||
)
|
||||
logger.Info("outages module enabled", "url", cfg.Portal.URL)
|
||||
} else {
|
||||
// portal.url is optional: without it only /v1/resolve is served.
|
||||
logger.Info("outages module disabled (portal.url not set)")
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
||||
loc := operationalLocation()
|
||||
srv := httpapi.New(resolver, portal, logger, loc)
|
||||
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, "tz", loc.String())
|
||||
logger.Info("listening", "addr", cfg.Addr)
|
||||
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
logger.Error("serve", "error", err)
|
||||
stop()
|
||||
@@ -116,13 +126,3 @@ func newLogger(level string) *slog.Logger {
|
||||
}
|
||||
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: lvl}))
|
||||
}
|
||||
|
||||
// operationalLocation returns Asia/Novosibirsk when the tz database is
|
||||
// available and falls back to a fixed UTC+7 offset otherwise (containers
|
||||
// without tzdata).
|
||||
func operationalLocation() *time.Location {
|
||||
if loc, err := time.LoadLocation("Asia/Novosibirsk"); err == nil {
|
||||
return loc
|
||||
}
|
||||
return time.FixedZone("Asia/Novosibirsk", 7*3600)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user