-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
130 lines (113 loc) · 3.78 KB
/
config.go
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
package service
import (
"fmt"
"time"
"github.com/plgd-dev/hub/v2/pkg/config"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/net/grpc/client"
"github.com/plgd-dev/hub/v2/pkg/net/grpc/server"
otelClient "github.com/plgd-dev/hub/v2/pkg/opentelemetry/collector/client"
natsClient "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventbus/nats/client"
)
type Config struct {
Log log.Config `yaml:"log" json:"log"`
APIs APIsConfig `yaml:"apis" json:"apis"`
Clients ClientsConfig `yaml:"clients" json:"clients"`
}
func (c *Config) Validate() error {
if err := c.Log.Validate(); err != nil {
return fmt.Errorf("log.%w", err)
}
if err := c.APIs.Validate(); err != nil {
return fmt.Errorf("apis.%w", err)
}
if err := c.Clients.Validate(); err != nil {
return fmt.Errorf("clients.%w", err)
}
return nil
}
// Config represent application configuration
type APIsConfig struct {
GRPC GRPCConfig `yaml:"grpc" json:"grpc"`
}
type GRPCConfig struct {
OwnerCacheExpiration time.Duration `yaml:"ownerCacheExpiration" json:"ownerCacheExpiration"`
SubscriptionBufferSize int `yaml:"subscriptionBufferSize" json:"subscriptionBufferSize"`
server.Config `yaml:",inline" json:",inline"`
}
func (c *GRPCConfig) Validate() error {
if c.OwnerCacheExpiration <= 0 {
return fmt.Errorf("ownerCacheExpiration('%v')", c.OwnerCacheExpiration)
}
if c.SubscriptionBufferSize < 0 {
return fmt.Errorf("subscriptionBufferSize('%v')", c.SubscriptionBufferSize)
}
return c.Config.Validate()
}
func (c *APIsConfig) Validate() error {
if err := c.GRPC.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
return nil
}
type IdentityStoreConfig struct {
Connection client.Config `yaml:"grpc" json:"grpc"`
}
func (c *IdentityStoreConfig) Validate() error {
if err := c.Connection.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
return nil
}
type ClientsConfig struct {
IdentityStore IdentityStoreConfig `yaml:"identityStore" json:"identityStore"`
Eventbus EventBusConfig `yaml:"eventBus" json:"eventBus"`
ResourceAggregate GrpcServerConfig `yaml:"resourceAggregate" json:"resourceAggregate"`
ResourceDirectory GrpcServerConfig `yaml:"resourceDirectory" json:"resourceDirectory"`
CertificateAuthority GrpcServerConfig `yaml:"certificateAuthority" json:"certificateAuthority"`
OpenTelemetryCollector otelClient.Config `yaml:"openTelemetryCollector" json:"openTelemetryCollector"`
}
type EventBusConfig struct {
GoPoolSize int `yaml:"goPoolSize" json:"goPoolSize"`
NATS natsClient.Config `yaml:"nats" json:"nats"`
}
func (c *EventBusConfig) Validate() error {
if err := c.NATS.Validate(); err != nil {
return fmt.Errorf("nats.%w", err)
}
return nil
}
func (c *ClientsConfig) Validate() error {
if err := c.IdentityStore.Validate(); err != nil {
return fmt.Errorf("identityStore.%w", err)
}
if err := c.Eventbus.Validate(); err != nil {
return fmt.Errorf("eventbus.%w", err)
}
if err := c.ResourceAggregate.Validate(); err != nil {
return fmt.Errorf("resourceAggregate.%w", err)
}
if err := c.ResourceDirectory.Validate(); err != nil {
return fmt.Errorf("resourceDirectory.%w", err)
}
if err := c.CertificateAuthority.Validate(); err != nil {
return fmt.Errorf("certificateAuthority.%w", err)
}
if err := c.OpenTelemetryCollector.Validate(); err != nil {
return fmt.Errorf("openTelemetryCollector.%w", err)
}
return nil
}
type GrpcServerConfig struct {
Connection client.Config `yaml:"grpc" json:"grpc"`
}
func (c *GrpcServerConfig) Validate() error {
if err := c.Connection.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
return nil
}
// String return string representation of Config
func (c Config) String() string {
return config.ToString(c)
}