-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathconfig.go
More file actions
644 lines (505 loc) · 22.5 KB
/
Copy pathconfig.go
File metadata and controls
644 lines (505 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package config
import (
"errors"
"fmt"
"strings"
"time"
authn "github.com/agntcy/dir/server/authn/config"
authz "github.com/agntcy/dir/server/authz/config"
dbconfig "github.com/agntcy/dir/server/database/config"
events "github.com/agntcy/dir/server/events/config"
ratelimitconfig "github.com/agntcy/dir/server/middleware/ratelimit/config"
naming "github.com/agntcy/dir/server/naming/config"
publication "github.com/agntcy/dir/server/publication/config"
routing "github.com/agntcy/dir/server/routing/config"
store "github.com/agntcy/dir/server/store/config"
oci "github.com/agntcy/dir/server/store/oci/config"
"github.com/agntcy/dir/utils/logging"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
const (
// Config params.
DefaultEnvPrefix = "DIRECTORY_SERVER"
DefaultConfigName = "server.config"
DefaultConfigType = "yml"
DefaultConfigPath = "/etc/agntcy/dir"
// API configuration.
DefaultListenAddress = "0.0.0.0:8888"
// OASF Validation configuration.
// Connection management configuration.
// These defaults are based on production gRPC best practices and provide
// a balance between resource efficiency and connection stability.
// DefaultMaxConcurrentStreams limits concurrent RPC streams per connection.
// This prevents a single connection from monopolizing server resources.
// Value: 1000 is industry standard, sufficient for most clients.
DefaultMaxConcurrentStreams = 1000
// DefaultMaxRecvMsgSize limits maximum received message size (4 MB).
// Protects against memory exhaustion from large messages.
// Value: 4 MB covers 99% of OCI artifacts and metadata.
DefaultMaxRecvMsgSize = 4 * 1024 * 1024
// DefaultMaxSendMsgSize limits maximum sent message size (4 MB).
// Value: 4 MB matches receive limit for consistency.
DefaultMaxSendMsgSize = 4 * 1024 * 1024
// DefaultConnectionTimeout limits time for connection establishment (120 seconds).
// Prevents hanging connection attempts from slow clients.
// Value: 2 minutes allows for slow networks without wasting resources.
DefaultConnectionTimeout = 120 * time.Second
// DefaultMaxConnectionIdle closes idle connections after this duration (15 minutes).
// An idle connection has no active RPC streams.
// Value: 15 minutes balances resource cleanup vs connection churn.
DefaultMaxConnectionIdle = 15 * time.Minute
// DefaultMaxConnectionAge forces connection rotation after this duration (30 minutes).
// Prevents long-lived connections from accumulating issues.
// Value: 30 minutes ensures regular TLS session rotation for security.
DefaultMaxConnectionAge = 30 * time.Minute
// DefaultMaxConnectionAgeGrace is grace period after MaxConnectionAge (5 minutes).
// Allows inflight RPCs to complete before force-closing connection.
// Value: 5 minutes provides sufficient time for most operations.
DefaultMaxConnectionAgeGrace = 5 * time.Minute
// DefaultKeepaliveTime is interval for sending keepalive pings (5 minutes).
// Detects dead connections when client crashes or network partitions.
// Value: 5 minutes detects issues fast without excessive traffic.
DefaultKeepaliveTime = 5 * time.Minute
// DefaultKeepaliveTimeout is wait time for keepalive ping response (1 minute).
// Connection is closed if no pong received within this timeout.
// Value: 1 minute allows for network delays without long waits.
DefaultKeepaliveTimeout = 1 * time.Minute
// DefaultMinTime is minimum time between client keepalive pings (1 minute).
// Prevents clients from abusing keepalive by sending excessive pings.
// Value: 1 minute prevents abuse while allowing reasonable client detection.
DefaultMinTime = 1 * time.Minute
// DefaultPermitWithoutStream allows keepalive pings without active streams.
// Enables clients to detect dead connections even when idle.
// Value: true provides better connection health detection.
DefaultPermitWithoutStream = true
// Metrics configuration.
// DefaultMetricsEnabled enables Prometheus metrics collection.
DefaultMetricsEnabled = true
// DefaultMetricsAddress is the default listen address for the metrics HTTP server.
DefaultMetricsAddress = ":9090"
// HTTP gateway (grpc-gateway) configuration.
// DefaultHTTPGatewayEnabled controls whether the REST gateway runs by default.
DefaultHTTPGatewayEnabled = false
// DefaultHTTPGatewayAddress is the default gateway listen address.
DefaultHTTPGatewayAddress = ":8889"
// DefaultHTTPGatewayPublicURL is the default public base URL for the gateway.
DefaultHTTPGatewayPublicURL = "http://localhost:8889"
// DefaultHTTPGatewayCatalogTitle is the default AI Catalog UI title.
DefaultHTTPGatewayCatalogTitle = "AI Catalog"
)
var logger = logging.Logger("config")
type Config struct {
// API configuration
ListenAddress string `json:"listen_address,omitempty" mapstructure:"listen_address"`
// OASF Validation configuration
OASFAPIValidation OASFAPIValidationConfig `json:"oasf_api_validation" mapstructure:"oasf_api_validation"`
// Logging configuration
Logging LoggingConfig `json:"logging" mapstructure:"logging"`
// Connection management configuration
Connection ConnectionConfig `json:"connection" mapstructure:"connection"`
// Rate limiting configuration
RateLimit ratelimitconfig.Config `json:"ratelimit" mapstructure:"ratelimit"`
// Authn configuration (JWT or X.509 authentication)
Authn authn.Config `json:"authn" mapstructure:"authn"`
// Authz configuration
Authz authz.Config `json:"authz" mapstructure:"authz"`
// Store configuration
Store store.Config `json:"store" mapstructure:"store"`
// Routing configuration
Routing routing.Config `json:"routing" mapstructure:"routing"`
// Database configuration
Database dbconfig.Config `json:"database" mapstructure:"database"`
// Sync configuration
Sync SyncConfig `json:"sync" mapstructure:"sync"`
// Publication configuration
Publication publication.Config `json:"publication" mapstructure:"publication"`
// Events configuration
Events events.Config `json:"events" mapstructure:"events"`
// Metrics configuration
Metrics MetricsConfig `json:"metrics" mapstructure:"metrics"`
// Naming holds name verification cache config (TTL for naming API; reconciler name task performs re-verification).
Naming naming.Config `json:"naming,omitzero" mapstructure:"naming"`
// HTTPGateway exposes the gRPC services over HTTP/JSON via grpc-gateway.
HTTPGateway HTTPGatewayConfig `json:"http_gateway,omitzero" mapstructure:"http_gateway"`
}
// HTTPGatewayConfig configures the in-process grpc-gateway sidecar.
// It is disabled by default.
type HTTPGatewayConfig struct {
// Enabled toggles the HTTP gateway sidecar.
Enabled bool `json:"enabled,omitempty" mapstructure:"enabled"`
// ListenAddress is the HTTP gateway listen address (e.g. ":8889").
ListenAddress string `json:"listen_address,omitempty" mapstructure:"listen_address"`
// PublicURL is the public base URL for the gateway (e.g. "https://api.example.com").
PublicURL string `json:"public_url,omitempty" mapstructure:"public_url"`
// CatalogTitle is the display name shown in the embedded AI Catalog UI.
CatalogTitle string `json:"catalog_title,omitempty" mapstructure:"catalog_title"`
}
// WithDefaults returns a copy with empty fields filled from package defaults.
func (c HTTPGatewayConfig) WithDefaults() HTTPGatewayConfig {
out := c
if out.ListenAddress == "" {
out.ListenAddress = DefaultHTTPGatewayAddress
}
if out.PublicURL == "" {
out.PublicURL = DefaultHTTPGatewayPublicURL
}
if strings.TrimSpace(out.CatalogTitle) == "" {
out.CatalogTitle = DefaultHTTPGatewayCatalogTitle
}
return out
}
type SyncConfig struct {
// AuthConfig holds authentication configuration for sync operations.
AuthConfig oci.AuthConfig `json:"auth_config" mapstructure:"auth_config"`
}
// OASFAPIValidationConfig defines OASF API validation configuration.
type OASFAPIValidationConfig struct {
// SchemaURL is the OASF schema URL for API-based validation.
// This is required - records will be validated using the OASF API validator.
// The default value is set in the Helm chart values.yaml (apiserver.config.oasf_api_validation.schema_url).
SchemaURL string `json:"schema_url,omitempty" mapstructure:"schema_url"`
}
// LoggingConfig defines gRPC request/response logging configuration.
type LoggingConfig struct {
// Verbose enables verbose logging mode (includes request/response payloads).
// Default: false (production mode - logs only start/finish with metadata).
Verbose bool `json:"verbose,omitempty" mapstructure:"verbose"`
}
// ConnectionConfig defines gRPC connection management configuration.
// These settings control connection lifecycle, resource limits, and keepalive behavior
// to prevent resource exhaustion and detect dead connections.
type ConnectionConfig struct {
// MaxConcurrentStreams limits concurrent RPCs per connection.
// Prevents a single connection from monopolizing server resources.
// Default: 1000
MaxConcurrentStreams uint32 `json:"max_concurrent_streams,omitempty" mapstructure:"max_concurrent_streams"`
// MaxRecvMsgSize limits the maximum message size the server can receive (in bytes).
// Protects against memory exhaustion from large messages.
// Default: 4194304 (4 MB)
MaxRecvMsgSize int `json:"max_recv_msg_size,omitempty" mapstructure:"max_recv_msg_size"`
// MaxSendMsgSize limits the maximum message size the server can send (in bytes).
// Default: 4194304 (4 MB)
MaxSendMsgSize int `json:"max_send_msg_size,omitempty" mapstructure:"max_send_msg_size"`
// ConnectionTimeout limits the time for connection establishment.
// Prevents hanging connection attempts from slow clients.
// Default: 120s (2 minutes)
ConnectionTimeout time.Duration `json:"connection_timeout,omitempty" mapstructure:"connection_timeout"`
// Keepalive configuration for connection health management.
Keepalive KeepaliveConfig `json:"keepalive" mapstructure:"keepalive"`
}
// KeepaliveConfig defines keepalive parameters for connection health.
// Keepalive pings detect dead connections (client crash, network partition)
// and automatically close idle or aged connections to free resources.
type KeepaliveConfig struct {
// MaxConnectionIdle is the duration after which idle connections are closed.
// An idle connection has no active RPC streams.
// Default: 15m (15 minutes)
MaxConnectionIdle time.Duration `json:"max_connection_idle,omitempty" mapstructure:"max_connection_idle"`
// MaxConnectionAge is the maximum duration a connection may exist.
// Forces connection rotation to prevent resource leaks and ensure TLS session rotation.
// Default: 30m (30 minutes)
MaxConnectionAge time.Duration `json:"max_connection_age,omitempty" mapstructure:"max_connection_age"`
// MaxConnectionAgeGrace is the grace period after MaxConnectionAge
// to allow inflight RPCs to complete before force-closing the connection.
// Default: 5m (5 minutes)
MaxConnectionAgeGrace time.Duration `json:"max_connection_age_grace,omitempty" mapstructure:"max_connection_age_grace"`
// Time is the duration after which a keepalive ping is sent
// on idle connections to check if the connection is still alive.
// Default: 5m (5 minutes)
Time time.Duration `json:"time,omitempty" mapstructure:"time"`
// Timeout is the duration the server waits for a keepalive ping response.
// If no response is received, the connection is closed.
// Default: 1m (1 minute)
Timeout time.Duration `json:"timeout,omitempty" mapstructure:"timeout"`
// MinTime is the minimum duration clients should wait between keepalive pings.
// Prevents clients from abusing keepalive by sending excessive pings.
// Default: 1m (1 minute)
MinTime time.Duration `json:"min_time,omitempty" mapstructure:"min_time"`
// PermitWithoutStream allows clients to send keepalive pings
// even when there are no active RPC streams.
// Enables clients to detect dead connections proactively.
// Default: true
PermitWithoutStream bool `json:"permit_without_stream,omitempty" mapstructure:"permit_without_stream"`
}
// MetricsConfig holds Prometheus metrics configuration.
type MetricsConfig struct {
// Enabled enables Prometheus metrics collection.
// Default: true
Enabled bool `json:"enabled,omitempty" mapstructure:"enabled"`
// Address is the HTTP listen address for the metrics endpoint.
// The metrics server runs on a separate port from the gRPC server.
// Default: ":9090"
Address string `json:"address,omitempty" mapstructure:"address"`
}
// DefaultConnectionConfig returns connection configuration with production-safe defaults.
// These defaults are based on industry best practices for production gRPC deployments
// and provide a balance between resource efficiency, connection stability, and security.
func DefaultConnectionConfig() ConnectionConfig {
return ConnectionConfig{
MaxConcurrentStreams: DefaultMaxConcurrentStreams,
MaxRecvMsgSize: DefaultMaxRecvMsgSize,
MaxSendMsgSize: DefaultMaxSendMsgSize,
ConnectionTimeout: DefaultConnectionTimeout,
Keepalive: KeepaliveConfig{
MaxConnectionIdle: DefaultMaxConnectionIdle,
MaxConnectionAge: DefaultMaxConnectionAge,
MaxConnectionAgeGrace: DefaultMaxConnectionAgeGrace,
Time: DefaultKeepaliveTime,
Timeout: DefaultKeepaliveTimeout,
MinTime: DefaultMinTime,
PermitWithoutStream: DefaultPermitWithoutStream,
},
}
}
// WithDefaults returns the connection configuration with defaults applied
// if the configuration is not set or has zero values.
// This method checks if MaxConcurrentStreams is 0 (indicating unset configuration)
// and returns the default configuration in that case.
func (c ConnectionConfig) WithDefaults() ConnectionConfig {
// If MaxConcurrentStreams is 0, the config was not set - use defaults
if c.MaxConcurrentStreams == 0 {
return DefaultConnectionConfig()
}
return c
}
type ConfigOptions struct {
file string
}
type ConfigOption func(*ConfigOptions)
func WithFile(file string) ConfigOption {
return func(opts *ConfigOptions) {
opts.file = file
}
}
//nolint:maintidx
func LoadConfig(opts ...ConfigOption) (*Config, error) {
var options ConfigOptions
for _, opt := range opts {
opt(&options)
}
v := viper.NewWithOptions(
viper.KeyDelimiter("."),
viper.EnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")),
)
if options.file != "" {
v.SetConfigFile(options.file)
} else {
v.SetConfigName(DefaultConfigName)
}
v.SetConfigType(DefaultConfigType)
v.AddConfigPath(DefaultConfigPath)
v.SetEnvPrefix(DefaultEnvPrefix)
v.AllowEmptyEnv(true)
v.AutomaticEnv()
// Read the config file
if err := v.ReadInConfig(); err != nil {
fileNotFoundError := viper.ConfigFileNotFoundError{}
if errors.As(err, &fileNotFoundError) {
logger.Info("Config file not found, use defaults.")
} else {
return nil, fmt.Errorf("failed to read configuration file: %w", err)
}
}
//
// API configuration
//
_ = v.BindEnv("listen_address")
v.SetDefault("listen_address", DefaultListenAddress)
//
// OASF Validation configuration
//
_ = v.BindEnv("oasf_api_validation.schema_url")
// Note: No default set here - default should come from Helm chart values.yaml
// Schema URL is required for OASF API validation
//
// Logging configuration (gRPC request/response logging)
//
_ = v.BindEnv("logging.verbose")
v.SetDefault("logging.verbose", false)
//
// Rate limiting configuration
//
_ = v.BindEnv("ratelimit.enabled")
v.SetDefault("ratelimit.enabled", false)
_ = v.BindEnv("ratelimit.global_rps")
v.SetDefault("ratelimit.global_rps", 0.0)
_ = v.BindEnv("ratelimit.global_burst")
v.SetDefault("ratelimit.global_burst", 0)
_ = v.BindEnv("ratelimit.per_client_rps")
v.SetDefault("ratelimit.per_client_rps", 0.0)
_ = v.BindEnv("ratelimit.per_client_burst")
v.SetDefault("ratelimit.per_client_burst", 0)
// Note: method_limits (per-method rate limit overrides) can only be configured
// via YAML/JSON config file due to its complex nested map structure.
// Environment variable configuration for method limits is not supported.
// Example config:
// ratelimit:
// method_limits:
// "/agntcy.dir.store.v1.StoreService/CreateRecord":
// rps: 50
// burst: 100
//
// Authn configuration (authentication: JWT or X.509)
//
_ = v.BindEnv("authn.enabled")
v.SetDefault("authn.enabled", "false")
_ = v.BindEnv("authn.mode")
v.SetDefault("authn.mode", "x509")
_ = v.BindEnv("authn.socket_path")
v.SetDefault("authn.socket_path", "")
_ = v.BindEnv("authn.audiences")
v.SetDefault("authn.audiences", "")
//
// Authz configuration (authorization policies)
//
_ = v.BindEnv("authz.enabled")
v.SetDefault("authz.enabled", "false")
_ = v.BindEnv("authz.enforcer_policy_file_path")
v.SetDefault("authz.enforcer_policy_file_path", DefaultConfigPath+"/authz_policies.csv")
//
// Store configuration
//
_ = v.BindEnv("store.provider")
v.SetDefault("store.provider", store.DefaultProvider)
_ = v.BindEnv("store.oci.local_dir")
v.SetDefault("store.oci.local_dir", "")
_ = v.BindEnv("store.oci.cache_dir")
v.SetDefault("store.oci.cache_dir", "")
_ = v.BindEnv("store.oci.registry_address")
v.SetDefault("store.oci.registry_address", oci.DefaultRegistryAddress)
_ = v.BindEnv("store.oci.repository_name")
v.SetDefault("store.oci.repository_name", oci.DefaultRepositoryName)
_ = v.BindEnv("store.oci.auth_config.insecure")
v.SetDefault("store.oci.auth_config.insecure", oci.DefaultAuthConfigInsecure)
_ = v.BindEnv("store.oci.auth_config.username")
_ = v.BindEnv("store.oci.auth_config.password")
_ = v.BindEnv("store.oci.auth_config.access_token")
_ = v.BindEnv("store.oci.auth_config.refresh_token")
//
// Store verification configuration
//
_ = v.BindEnv("store.verification.enabled")
v.SetDefault("store.verification.enabled", store.DefaultVerificationEnabled)
//
// Routing configuration
//
_ = v.BindEnv("routing.listen_address")
v.SetDefault("routing.listen_address", routing.DefaultListenAddress)
_ = v.BindEnv("routing.directory_api_address")
v.SetDefault("routing.directory_api_address", "")
_ = v.BindEnv("routing.bootstrap_peers")
v.SetDefault("routing.bootstrap_peers", strings.Join(routing.DefaultBootstrapPeers, ","))
_ = v.BindEnv("routing.key_path")
v.SetDefault("routing.key_path", "")
_ = v.BindEnv("routing.datastore_dir")
v.SetDefault("routing.datastore_dir", "")
//
// Routing GossipSub configuration
// Note: Only enable/disable is configurable. Protocol parameters (topic, message size)
// are hardcoded in server/routing/pubsub/constants.go for network compatibility.
//
_ = v.BindEnv("routing.gossipsub.enabled")
v.SetDefault("routing.gossipsub.enabled", routing.DefaultGossipSubEnabled)
//
// Database configuration
//
_ = v.BindEnv("database.type")
v.SetDefault("database.type", dbconfig.DefaultType)
// SQLite configuration
_ = v.BindEnv("database.sqlite.path")
v.SetDefault("database.sqlite.path", dbconfig.DefaultSQLitePath)
// PostgreSQL configuration
_ = v.BindEnv("database.postgres.host")
v.SetDefault("database.postgres.host", dbconfig.DefaultPostgresHost)
_ = v.BindEnv("database.postgres.port")
v.SetDefault("database.postgres.port", dbconfig.DefaultPostgresPort)
_ = v.BindEnv("database.postgres.database")
v.SetDefault("database.postgres.database", dbconfig.DefaultPostgresDatabase)
_ = v.BindEnv("database.postgres.username")
_ = v.BindEnv("database.postgres.password")
_ = v.BindEnv("database.postgres.ssl_mode")
v.SetDefault("database.postgres.ssl_mode", dbconfig.DefaultPostgresSSLMode)
//
// Sync configuration
//
_ = v.BindEnv("sync.auth_config.username")
_ = v.BindEnv("sync.auth_config.password")
//
// Publication configuration
//
_ = v.BindEnv("publication.scheduler_interval")
v.SetDefault("publication.scheduler_interval", publication.DefaultPublicationSchedulerInterval)
_ = v.BindEnv("publication.worker_count")
v.SetDefault("publication.worker_count", publication.DefaultPublicationWorkerCount)
_ = v.BindEnv("publication.worker_timeout")
v.SetDefault("publication.worker_timeout", publication.DefaultPublicationWorkerTimeout)
//
// Events configuration
//
_ = v.BindEnv("events.subscriber_buffer_size")
v.SetDefault("events.subscriber_buffer_size", events.DefaultSubscriberBufferSize)
_ = v.BindEnv("events.log_slow_consumers")
v.SetDefault("events.log_slow_consumers", events.DefaultLogSlowConsumers)
_ = v.BindEnv("events.log_published_events")
v.SetDefault("events.log_published_events", events.DefaultLogPublishedEvents)
//
// Metrics configuration
//
_ = v.BindEnv("metrics.enabled")
v.SetDefault("metrics.enabled", DefaultMetricsEnabled)
_ = v.BindEnv("metrics.address")
v.SetDefault("metrics.address", DefaultMetricsAddress)
//
// Naming (name verification cache TTL for API responses; re-verification is done by the reconciler name task)
//
_ = v.BindEnv("naming.ttl")
v.SetDefault("naming.ttl", naming.DefaultTTL)
//
// Connection management configuration
//
// Design Decision: No environment variables for connection management.
// Rationale:
// - 11 env vars would be too many and too technical for most users
// - Production-safe defaults work for 90% of deployments
// - Advanced users can use YAML config file for fine-grained control
// - Follows industry best practices (Kubernetes, Prometheus, etc.)
//
// For advanced configuration, use YAML config file:
// connection:
// max_concurrent_streams: 2000
// max_recv_msg_size: 8388608 # 8 MB
// keepalive:
// max_connection_idle: 10m
// # ... other settings
//
// No viper defaults needed - defaults are applied via ConnectionConfig.WithDefaults()
// after loading to ensure clean separation between loading and defaulting logic.
//
// Gateway configuration
//
_ = v.BindEnv("http_gateway.enabled")
v.SetDefault("http_gateway.enabled", DefaultHTTPGatewayEnabled)
_ = v.BindEnv("http_gateway.listen_address")
v.SetDefault("http_gateway.listen_address", DefaultHTTPGatewayAddress)
_ = v.BindEnv("http_gateway.public_url")
v.SetDefault("http_gateway.public_url", DefaultHTTPGatewayPublicURL)
_ = v.BindEnv("http_gateway.catalog_title")
v.SetDefault("http_gateway.catalog_title", DefaultHTTPGatewayCatalogTitle)
// Load configuration into struct
decodeHooks := mapstructure.ComposeDecodeHookFunc(
mapstructure.TextUnmarshallerHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
)
config := &Config{}
if err := v.Unmarshal(config, viper.DecodeHook(decodeHooks)); err != nil {
return nil, fmt.Errorf("failed to load configuration: %w", err)
}
// Apply connection management defaults if not configured
// This happens after unmarshal so YAML config takes precedence over defaults
config.Connection = config.Connection.WithDefaults()
return config, nil
}