Files
archie/internal/app/archieui/config.go
T

168 lines
5.0 KiB
Go

package archieui
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
"github.com/samcharles93/archie-core/internal/webui"
)
// Resolve returns the effective Options: flags first, then the config file's
// projection, then defaults, and validates the result.
func Resolve(o Options, log *slog.Logger) (Options, error) {
var tls config.TLS
if o.Config != "" {
cfg, found, err := readProjection(o.Config, o.Overlay, log)
if err != nil {
return Options{}, err
}
if found {
o = merge(o, cfg)
tls = cfg.tls
o.instance = cfg.instance
}
}
if o.instance == "" {
o.instance = configuration.InstanceName()
}
o = withDefaults(o)
id, err := rpctls.Load(tls.CAFile, tls.CertDir, presence.UI)
if err != nil {
return Options{}, err
}
o.identity = id
if err := o.validate(); err != nil {
return Options{}, err
}
return o, nil
}
// readProjection loads the configuration source and returns the six values
// the UI process is allowed to see. An absent source is not an error (the
// process is fully drivable by flags); an unreadable or invalid one is.
func readProjection(path, overlay string, log *slog.Logger) (projection, bool, error) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) && overlay == "" {
return projection{}, false, nil
}
doc, err := configuration.New(log).Resolve(path, overlay)
if err != nil {
return projection{}, false, fmt.Errorf("read ui configuration: %w", err)
}
return project(doc.Config), true, nil
}
// projection is the file's contribution, narrowed at the point of decode so
// no other configuration value is carried past this function.
type projection struct {
listen string
trustForwardedHeaders bool
gateway ServiceTarget
state ServiceTarget
messaging ServiceTarget
daemon ServiceTarget
tls config.TLS
capture CaptureOptions
instance string
}
func project(cfg config.Config) projection {
return projection{
listen: cfg.Web.Listen,
trustForwardedHeaders: cfg.Web.TrustForwardedHeaders,
gateway: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameGateway).Target},
messaging: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameMessaging).Target},
state: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target},
daemon: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameDaemon).Target},
tls: cfg.TLS,
instance: cfg.Instance,
capture: CaptureOptions{
Retention: cfg.Capture.Retention.Std(),
MaxEvents: cfg.Capture.MaxEvents,
MaxBodyBytes: cfg.Capture.MaxBodyBytes,
RatePerSecond: cfg.Capture.RatePerSecond,
RateBurst: cfg.Capture.RateBurst,
},
}
}
// merge fills only the fields the flags left empty.
func merge(o Options, p projection) Options {
if o.Listen == "" {
o.Listen = p.listen
}
if o.TrustForwardedHeaders == nil {
trust := p.trustForwardedHeaders
o.TrustForwardedHeaders = &trust
}
o.Gateway = mergeTarget(o.Gateway, p.gateway)
o.Messaging = mergeTarget(o.Messaging, p.messaging)
o.State = mergeTarget(o.State, p.state)
o.Daemon = mergeTarget(o.Daemon, p.daemon)
if o.Capture.Retention <= 0 {
o.Capture.Retention = p.capture.Retention
}
if o.Capture.MaxEvents <= 0 {
o.Capture.MaxEvents = p.capture.MaxEvents
}
if o.Capture.MaxBodyBytes <= 0 {
o.Capture.MaxBodyBytes = p.capture.MaxBodyBytes
}
if o.Capture.RatePerSecond <= 0 {
o.Capture.RatePerSecond = p.capture.RatePerSecond
}
if o.Capture.RateBurst <= 0 {
o.Capture.RateBurst = p.capture.RateBurst
}
return o
}
func withDefaults(o Options) Options {
if o.Messaging.Target == "" {
spec, _ := config.LookupService(config.ServiceNameMessaging)
o.Messaging.Target = spec.Target
}
// "off" disables the daemon's dashboard but means the default listener here.
if o.Listen == "" || o.Listen == "off" {
o.Listen = defaultListen
}
if o.DependencyTimeout <= 0 {
o.DependencyTimeout = defaultDependencyTimeout
}
if o.EventPollInterval <= 0 {
o.EventPollInterval = webui.DefaultEventPollInterval
}
if o.ReadHeaderTimeout <= 0 {
o.ReadHeaderTimeout = defaultReadHeaderTimeout
}
if o.ShutdownTimeout <= 0 {
o.ShutdownTimeout = defaultShutdownTimeout
}
return o
}
// Browser access is gated by accounts; service endpoints must still be configured.
func (o Options) validate() error {
if o.Gateway.Target == "" {
return fmt.Errorf("gateway target is required (-gateway-target or [services.gateway].target)")
}
if o.State.Target == "" {
return fmt.Errorf("state store target is required (-state-target or [services.state].target)")
}
return nil
}
func mergeTarget(o, p ServiceTarget) ServiceTarget {
if o.Target == "" {
o.Target = p.Target
}
return o
}