Files
archie/internal/config/services.go
T
sam 96e5d7b84b refactor: strip doc and issue narration from code comments
Remove PRD/doc paths, bead IDs, phase notes and deleted-test references
from comments; drop test-only hooks left by the test deletion. Includes
in-progress archied bootstrap, gateway and query changes.
2026-10-03 21:47:46 +10:00

80 lines
3.3 KiB
Go

package config
import (
"fmt"
"strings"
)
// Services maps a registered service name to its connection settings, keyed by
// the [services.<name>] section it was decoded from.
//
// It was a struct with one field per service. That stated each service's name
// three times over -- here, in the loader's defaulting branches, and at every
// consumer's field access -- so a new service meant editing all three and
// nothing prevented them disagreeing. Which names are legitimate now comes
// from RegisterService; see ServiceSpec.
type Services map[string]ServiceConnection
// Get returns the connection configured for name, or the zero value when the
// configuration carries no section for it. A registered service always has an
// entry once defaults have been applied.
func (s Services) Get(name string) ServiceConnection {
return s[name]
}
// ResolvedToken is the bearer token a client presents when dialling name: the
// explicit target_token if set, otherwise the environment variable the service
// registered. getenv is passed in rather than read directly so this stays free
// of internal/secret, which imports this package.
//
// This replaces two resolvers that were identical apart from the variable name
// each hard-coded.
func (s Services) ResolvedToken(name string, getenv func(string) string) string {
if token := s[name].TargetToken; token != "" {
return token
}
spec, ok := LookupService(name)
if !ok || spec.TokenEnv == "" {
return ""
}
return getenv(spec.TokenEnv)
}
// RequireTarget returns the dial address for name, or an error naming the
// config key when the operator has not supplied one.
//
// A service registered with an empty default target is one the operator must
// configure, so this is the registry's own contract rather than a consumer's
// private rule. It stays a lazy check at composition time, not a load-time
// validation: a process that never dials a given service must still be able to
// load a configuration that omits it.
func (s Services) RequireTarget(name string) (string, error) {
target := strings.TrimSpace(s[name].Target)
if target == "" {
return "", fmt.Errorf("services.%s.target is required", name)
}
return target, nil
}
// ServiceConnection is a service's gRPC addresses plus the bearer token a
// client presents when the target is non-loopback. A client reads only Target
// and TargetToken; Listen belongs to the process that owns the service. The
// struct does not itself register services or start listeners. Changes require
// a daemon restart.
type ServiceConnection struct {
Target string `toml:"target" yaml:"target"`
// Listen is the address the process that OWNS this service binds
// (archie-state-store, archie-gateway). It is read only by that process;
// every client ignores it. Defaulted from the service's registration, so
// a loaded config is never empty here -- an empty address reaches
// net.Listen as "any free port", which would silently move the service
// off the address its clients dial.
Listen string `toml:"listen" yaml:"listen"`
// TargetToken is the bearer token a client (daemon or agent) presents
// when dialing a non-loopback service target, and the standalone
// archie-state-store server validates. Empty means no token, which is
// the loopback-only, daemon-only listener topology permitted without
// auth.
TargetToken string `toml:"target_token" yaml:"target_token"`
}