-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
314 lines (245 loc) · 9.36 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
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
package frame
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
)
const ctxKeyConfiguration = contextKey("configurationKey")
// Config Option that helps to specify or override the configuration object of our service.
func Config(config any) Option {
return func(s *Service) {
s.configuration = config
}
}
func (s *Service) Config() any {
return s.configuration
}
// ConfigToContext adds service configuration to the current supplied context
func ConfigToContext(ctx context.Context, config any) context.Context {
return context.WithValue(ctx, ctxKeyConfiguration, config)
}
// ConfigFromContext extracts service configuration from the supplied context if any exist
func ConfigFromContext(ctx context.Context) any {
return ctx.Value(ctxKeyConfiguration)
}
type ConfigurationDefault struct {
LogLevel string `default:"info" envconfig:"LOG_LEVEL"`
RunServiceSecurely bool `default:"true" envconfig:"RUN_SERVICE_SECURELY"`
ServerPort string `default:":7000" envconfig:"PORT"`
HttpServerPort string `default:":8080" envconfig:"HTTP_PORT"`
GrpcServerPort string `default:":50051" envconfig:"GRPC_PORT"`
CORSEnabled bool `default:"false" envconfig:"CORS_ENABLED"`
CORSAllowCredentials bool `default:"false" envconfig:"CORS_ALLOW_CREDENTIALS"`
CORSAllowedHeaders []string `default:"Authorization" envconfig:"CORS_ALLOWED_HEADERS"`
CORSExposedHeaders []string `default:"*" envconfig:"CORS_EXPOSED_HEADERS"`
CORSAllowedOrigins []string `default:"*" envconfig:"CORS_ALLOWED_ORIGINS"`
CORSAllowedMethods []string `default:"GET,HEAD,POST,PUT,OPTIONS" envconfig:"CORS_ALLOWED_METHODS"`
CORSMaxAge int `default:"3600" envconfig:"CORS_MAX_AGE"`
TLSCertificatePath string `envconfig:"TLS_CERTIFICATE_PATH"`
TLSCertificateKeyPath string `envconfig:"TLS_CERTIFICATE_KEY_PATH"`
Oauth2WellKnownJwk string `envconfig:"OAUTH2_WELL_KNOWN_JWK"`
Oauth2JwtVerifyAudience string `envconfig:"OAUTH2_JWT_VERIFY_AUDIENCE"`
Oauth2JwtVerifyIssuer string `envconfig:"OAUTH2_JWT_VERIFY_ISSUER"`
Oauth2ServiceURI string `envconfig:"OAUTH2_SERVICE_URI"`
Oauth2ServiceClientSecret string `envconfig:"OAUTH2_SERVICE_CLIENT_SECRET"`
Oauth2ServiceAudience string `envconfig:"OAUTH2_SERVICE_AUDIENCE"`
Oauth2ServiceAdminURI string `envconfig:"OAUTH2_SERVICE_ADMIN_URI"`
AuthorizationServiceReadURI string `envconfig:"AUTHORIZATION_SERVICE_READ_URI"`
AuthorizationServiceWriteURI string `envconfig:"AUTHORIZATION_SERVICE_WRITE_URI"`
DatabasePrimaryURL []string `envconfig:"DATABASE_URL"`
DatabaseReplicaURL []string `envconfig:"REPLICA_DATABASE_URL"`
DatabaseMigrate string `default:"false" envconfig:"DO_MIGRATION"`
DatabaseMigrationPath string `default:"./migrations/0001" envconfig:"MIGRATION_PATH"`
DatabaseSkipDefaultTransaction bool `default:"true" envconfig:"SKIP_DEFAULT_TRANSACTION"`
DatabaseMaxIdleConnections int `default:"2" envconfig:"DATABASE_MAX_IDLE_CONNECTIONS"`
DatabaseMaxOpenConnections int `default:"5" envconfig:"DATABASE_MAX_OPEN_CONNECTIONS"`
DatabaseMaxConnectionLifeTimeSeconds int `default:"300" envconfig:"DATABASE_MAX_CONNECTION_LIFE_TIME_IN_SECONDS"`
EventsQueueName string `default:"frame.events.internal_._queue" envconfig:"EVENTS_QUEUE_NAME"`
EventsQueueUrl string `default:"mem://frame.events.internal_._queue" envconfig:"EVENTS_QUEUE_URL"`
}
type ConfigurationSecurity interface {
IsRunSecurely() bool
}
var _ ConfigurationSecurity = new(ConfigurationDefault)
func (c *ConfigurationDefault) IsRunSecurely() bool {
return c.RunServiceSecurely
}
type ConfigurationLogLevel interface {
LoggingLevel() string
LoggingLevelIsDebug() bool
}
var _ ConfigurationLogLevel = new(ConfigurationDefault)
func (c *ConfigurationDefault) LoggingLevel() string {
return strings.ToLower(c.LogLevel)
}
func (c *ConfigurationDefault) LoggingLevelIsDebug() bool {
return c.LoggingLevel() == "debug" || c.LoggingLevel() == "trace"
}
type ConfigurationPorts interface {
Port() string
HttpPort() string
GrpcPort() string
}
var _ ConfigurationPorts = new(ConfigurationDefault)
func (c *ConfigurationDefault) Port() string {
if i, err := strconv.Atoi(c.ServerPort); err == nil && i > 0 {
return fmt.Sprintf(":%s", strings.TrimSpace(c.ServerPort))
}
if strings.HasPrefix(":", c.ServerPort) || strings.Contains(c.ServerPort, ":") {
return c.ServerPort
}
return ":80"
}
func (c *ConfigurationDefault) HttpPort() string {
if i, err := strconv.Atoi(c.HttpServerPort); err == nil && i > 0 {
return fmt.Sprintf(":%s", strings.TrimSpace(c.HttpServerPort))
}
if strings.HasPrefix(":", c.HttpServerPort) || strings.Contains(c.HttpServerPort, ":") {
return c.HttpServerPort
}
return c.Port()
}
func (c *ConfigurationDefault) GrpcPort() string {
if i, err := strconv.Atoi(c.GrpcServerPort); err == nil && i > 0 {
return fmt.Sprintf(":%s", strings.TrimSpace(c.GrpcServerPort))
}
if strings.HasPrefix(":", c.GrpcServerPort) || strings.Contains(c.GrpcServerPort, ":") {
return c.GrpcServerPort
}
return c.Port()
}
type ConfigurationCORS interface {
IsCORSEnabled() bool
IsCORSAllowCredentials() bool
GetCORSAllowedHeaders() []string
GetCORSExposedHeaders() []string
GetCORSAllowedOrigins() []string
GetCORSAllowedMethods() []string
GetCORSMaxAge() int
}
var _ ConfigurationCORS = new(ConfigurationDefault)
func (c *ConfigurationDefault) IsCORSEnabled() bool {
return c.CORSEnabled
}
func (c *ConfigurationDefault) IsCORSAllowCredentials() bool {
return c.CORSAllowCredentials
}
func (c *ConfigurationDefault) GetCORSMaxAge() int {
return c.CORSMaxAge
}
func (c *ConfigurationDefault) GetCORSAllowedHeaders() []string {
return c.CORSAllowedHeaders
}
func (c *ConfigurationDefault) GetCORSExposedHeaders() []string {
return c.CORSExposedHeaders
}
func (c *ConfigurationDefault) GetCORSAllowedOrigins() []string {
return c.CORSAllowedOrigins
}
func (c *ConfigurationDefault) GetCORSAllowedMethods() []string {
return c.CORSAllowedMethods
}
type ConfigurationOAUTH2 interface {
GetOauthWellKnownJwk() string
GetOauth2ServiceURI() string
GetOauth2ServiceClientSecret() string
GetOauth2ServiceAudience() string
GetOauth2ServiceAdminURI() string
}
var _ ConfigurationOAUTH2 = new(ConfigurationDefault)
func (c *ConfigurationDefault) GetOauthWellKnownJwk() string {
return c.Oauth2WellKnownJwk
}
func (c *ConfigurationDefault) GetOauth2ServiceURI() string {
return c.Oauth2ServiceURI
}
func (c *ConfigurationDefault) GetOauth2ServiceClientSecret() string {
return c.Oauth2ServiceClientSecret
}
func (c *ConfigurationDefault) GetOauth2ServiceAudience() string {
return c.Oauth2ServiceAudience
}
func (c *ConfigurationDefault) GetOauth2ServiceAdminURI() string {
return c.Oauth2ServiceAdminURI
}
type ConfigurationAuthorization interface {
GetAuthorizationServiceReadURI() string
GetAuthorizationServiceWriteURI() string
}
var _ ConfigurationAuthorization = new(ConfigurationDefault)
func (c *ConfigurationDefault) GetAuthorizationServiceReadURI() string {
return c.AuthorizationServiceReadURI
}
func (c *ConfigurationDefault) GetAuthorizationServiceWriteURI() string {
return c.AuthorizationServiceWriteURI
}
type ConfigurationDatabase interface {
GetDatabasePrimaryHostURL() []string
GetDatabaseReplicaHostURL() []string
DoDatabaseMigrate() bool
SkipDefaultTransaction() bool
GetMaxIdleConnections() int
GetMaxOpenConnections() int
GetMaxConnectionLifeTimeInSeconds() time.Duration
GetDatabaseMigrationPath() string
}
var _ ConfigurationDatabase = new(ConfigurationDefault)
func (c *ConfigurationDefault) GetDatabasePrimaryHostURL() []string {
return c.DatabasePrimaryURL
}
func (c *ConfigurationDefault) GetDatabaseReplicaHostURL() []string {
return c.DatabasePrimaryURL
}
func (c *ConfigurationDefault) DoDatabaseMigrate() bool {
isMigration, err := strconv.ParseBool(c.DatabaseMigrate)
if err != nil {
isMigration = false
}
stdArgs := os.Args[1:]
return isMigration || (len(stdArgs) > 0 && stdArgs[0] == "migrate")
}
func (c *ConfigurationDefault) SkipDefaultTransaction() bool {
return c.DatabaseSkipDefaultTransaction
}
func (c *ConfigurationDefault) GetMaxIdleConnections() int {
return c.DatabaseMaxIdleConnections
}
func (c *ConfigurationDefault) GetMaxOpenConnections() int {
return c.DatabaseMaxOpenConnections
}
func (c *ConfigurationDefault) GetMaxConnectionLifeTimeInSeconds() time.Duration {
return time.Duration(c.DatabaseMaxConnectionLifeTimeSeconds) * time.Second
}
func (c *ConfigurationDefault) GetDatabaseMigrationPath() string {
return c.DatabaseMigrationPath
}
type ConfigurationEvents interface {
GetEventsQueueName() string
GetEventsQueueUrl() string
}
var _ ConfigurationEvents = new(ConfigurationDefault)
func (c *ConfigurationDefault) GetEventsQueueName() string {
return c.EventsQueueName
}
func (c *ConfigurationDefault) GetEventsQueueUrl() string {
return c.EventsQueueUrl
}
type ConfigurationTLS interface {
TLSCertPath() string
TLSCertKeyPath() string
SetTLSCertAndKeyPath(certificatePath, certificateKeyPath string)
}
var _ ConfigurationTLS = new(ConfigurationDefault)
func (c *ConfigurationDefault) TLSCertKeyPath() string {
return c.TLSCertificateKeyPath
}
func (c *ConfigurationDefault) TLSCertPath() string {
return c.TLSCertificatePath
}
func (c *ConfigurationDefault) SetTLSCertAndKeyPath(certificatePath, certificateKeyPath string) {
c.TLSCertificatePath = certificatePath
c.TLSCertificateKeyPath = certificateKeyPath
}