package config import ( "sort" ) // Service contexts. The context decides which addresses a service must end up // with: a process that owns a service needs a listen address, and a process // that dials one needs a target. const ( // ServiceClient is dialled but not hosted by this repository. ServiceClient = "client" // ServiceServer is hosted but not dialled. ServiceServer = "server" // ServiceBoth is hosted by one of our processes and dialled by others. ServiceBoth = "both" ) // ServiceSpec declares one service: name and default addresses. type ServiceSpec struct { // Context is ServiceClient, ServiceServer, or ServiceBoth. Context string // Name is the [services.] section and the key callers look up. Name string // Target is the default dial address. Empty means there is no default // and the operator must supply one, which is how "services.state.target // is required" is stated without a branch that names the service. Target string // Listen is the default bind address for the process that owns the // service. It is never empty for a hosted service: an empty address // reaches net.Listen as "any free port", silently moving the service // off the address its clients dial. Listen string } // Registered service names. They live beside the registry below so the // spelling a consumer looks up and the spelling that was registered are the // same token, not two string literals that a typo can separate. const ( ServiceNameGateway = "gateway" ServiceNameState = "state" ServiceNameMessaging = "messaging" ServiceNameDaemon = "daemon" ) // serviceRegistry holds the services this repository ships. What differs // between them is data here, not a branch elsewhere: the gateway's target is // defaulted while the State Store's is supplied by the operator. var serviceRegistry = map[string]ServiceSpec{ ServiceNameGateway: { Context: ServiceBoth, Name: ServiceNameGateway, Target: "127.0.0.1:8585", Listen: "127.0.0.1:8585", }, ServiceNameState: { Context: ServiceClient, Name: ServiceNameState, Target: "", Listen: "127.0.0.1:9090", }, ServiceNameMessaging: { Context: ServiceBoth, Name: ServiceNameMessaging, Target: "127.0.0.1:8586", Listen: "127.0.0.1:8586", }, ServiceNameDaemon: { Context: ServiceBoth, Name: ServiceNameDaemon, Target: "127.0.0.1:8587", Listen: "127.0.0.1:8587", }, } // LookupService returns the spec registered for name. The registry is the // authority on which service names exist, which is what lets the loader still // report a typo'd [services.gatway] once Services is a map that would // otherwise decode any section at all. func LookupService(name string) (ServiceSpec, bool) { spec, ok := serviceRegistry[name] return spec, ok } // RegisteredServices returns a copy of every spec, ordered by name so // defaulting and validation run in a stable order regardless of map iteration. func RegisteredServices() []ServiceSpec { out := make([]ServiceSpec, 0, len(serviceRegistry)) for _, spec := range serviceRegistry { out = append(out, spec) } sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) return out }