-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
192 lines (170 loc) · 6.13 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
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
package service
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/pkg/build"
"github.com/plgd-dev/hub/v2/pkg/config"
"github.com/plgd-dev/hub/v2/pkg/config/property/urischeme"
"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"
pkgTime "github.com/plgd-dev/hub/v2/pkg/time"
natsClient "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventbus/nats/client"
eventstoreConfig "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/config"
)
type Config struct {
HubID string `yaml:"hubID" json:"hubId"`
Log log.Config `yaml:"log" json:"log"`
APIs APIsConfig `yaml:"apis" json:"apis"`
Clients ClientsConfig `yaml:"clients" json:"clients"`
ExposedHubConfiguration PublicConfiguration `yaml:"publicConfiguration" json:"publicConfiguration"`
}
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)
}
if err := c.ExposedHubConfiguration.Validate(); err != nil {
return fmt.Errorf("publicConfiguration.%w", err)
}
if _, err := uuid.Parse(c.HubID); err != nil {
return fmt.Errorf("hubID('%v') - %w", c.HubID, err)
}
return nil
}
// Config represent application configuration
type APIsConfig struct {
GRPC GRPCConfig `yaml:"grpc" json:"grpc"`
}
func (c *APIsConfig) Validate() error {
if err := c.GRPC.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
return nil
}
type GRPCConfig struct {
OwnerCacheExpiration time.Duration `yaml:"ownerCacheExpiration" json:"ownerCacheExpiration"`
server.Config `yaml:",inline" json:",inline"`
}
func (c *GRPCConfig) Validate() error {
if c.OwnerCacheExpiration <= 0 {
return fmt.Errorf("ownerCacheExpiration('%v')", c.OwnerCacheExpiration)
}
return c.Config.Validate()
}
type ClientsConfig struct {
Eventbus EventBusConfig `yaml:"eventBus" json:"eventBus"`
Eventstore EventStoreConfig `yaml:"eventStore" json:"eventStore"`
IdentityStore IdentityStoreConfig `yaml:"identityStore" json:"identityStore"`
OpenTelemetryCollector otelClient.Config `yaml:"openTelemetryCollector" json:"openTelemetryCollector"`
}
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.Eventstore.Validate(); err != nil {
return fmt.Errorf("eventstore.%w", err)
}
if err := c.OpenTelemetryCollector.Validate(); err != nil {
return fmt.Errorf("openTelemetryCollector.%w", err)
}
return nil
}
type EventBusConfig struct {
GoPoolSize int `yaml:"goPoolSize" json:"goPoolSize"`
NATS natsClient.Config `yaml:"nats" json:"nats"`
}
func (c *EventBusConfig) Validate() error {
if c.GoPoolSize <= 0 {
return fmt.Errorf("goPoolSize('%v')", c.GoPoolSize)
}
if err := c.NATS.Validate(); err != nil {
return fmt.Errorf("nats.%w", err)
}
return nil
}
type EventStoreConfig struct {
ProjectionCacheExpiration time.Duration `yaml:"cacheExpiration" json:"cacheExpiration"`
Connection eventstoreConfig.Config `yaml:",inline" json:",inline"`
}
func (c *EventStoreConfig) Validate() error {
if c.ProjectionCacheExpiration <= 0 {
return fmt.Errorf("cacheExpiration('%v')", c.ProjectionCacheExpiration)
}
return c.Connection.Validate()
}
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 PublicConfiguration struct {
CAPool urischeme.URIScheme `yaml:"caPool" json:"caPool" description:"file path to the root certificate in PEM format"`
OwnerClaim string `yaml:"ownerClaim" json:"ownerClaim"`
DeviceIDClaim string `yaml:"deviceIDClaim" json:"deviceIdClaim"`
CoapGateway string `yaml:"coapGateway" json:"coapGateway"`
DefaultCommandTimeToLive time.Duration `yaml:"defaultCommandTimeToLive" json:"defaultCommandTimeToLive"`
Authority string `yaml:"authority" json:"authority"`
CertificateAuthority string `yaml:"certificateAuthority" json:"certificateAuthority"`
cloudCertificateAuthorities string `yaml:"-"`
}
func (c *PublicConfiguration) Validate() error {
if c.CAPool == "" {
return fmt.Errorf("caPool('%v')", c.CAPool)
}
if c.OwnerClaim == "" {
return fmt.Errorf("ownerClaim('%v')", c.OwnerClaim)
}
if c.CoapGateway == "" {
return fmt.Errorf("coapGateway('%v')", c.CoapGateway)
}
if c.CAPool == "" {
return fmt.Errorf("caPool('%v')", c.CAPool)
}
if c.Authority == "" {
return fmt.Errorf("authority('%v')", c.Authority)
}
if c.CertificateAuthority == "" {
return fmt.Errorf("certificateAuthority('%v')", c.CertificateAuthority)
}
return nil
}
func (c PublicConfiguration) ToProto(hubID string) *pb.HubConfigurationResponse {
return &pb.HubConfigurationResponse{
JwtOwnerClaim: c.OwnerClaim,
JwtDeviceIdClaim: c.DeviceIDClaim,
Id: hubID,
CoapGateway: c.CoapGateway,
CertificateAuthorities: c.cloudCertificateAuthorities,
DefaultCommandTimeToLive: int64(c.DefaultCommandTimeToLive),
CurrentTime: pkgTime.UnixNano(time.Now()),
Authority: c.Authority,
CertificateAuthority: c.CertificateAuthority,
BuildInfo: &pb.BuildInfo{
Version: build.Version,
CommitHash: build.CommitHash,
BuildDate: build.BuildDate,
CommitDate: build.CommitDate,
ReleaseUrl: build.ReleaseURL,
},
}
}
// String return string representation of Config
func (c Config) String() string {
return config.ToString(c)
}