generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
205 lines (171 loc) · 8.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
193
194
195
196
197
198
199
200
201
202
203
204
205
package config
import (
"crypto/tls"
"strings"
"time"
"github.com/datumforge/entx"
"github.com/datumforge/fgax"
"github.com/datumforge/geodetic/pkg/geodeticclient"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/mcuadros/go-defaults"
"github.com/datumforge/datum/internal/httpserve/handlers"
"github.com/datumforge/datum/pkg/analytics/posthog"
"github.com/datumforge/datum/pkg/cache"
"github.com/datumforge/datum/pkg/events/kafka/kafkaconfig"
"github.com/datumforge/datum/pkg/middleware/cachecontrol"
"github.com/datumforge/datum/pkg/middleware/cors"
"github.com/datumforge/datum/pkg/middleware/mime"
"github.com/datumforge/datum/pkg/middleware/ratelimit"
"github.com/datumforge/datum/pkg/middleware/redirect"
"github.com/datumforge/datum/pkg/middleware/secure"
"github.com/datumforge/datum/pkg/otelx"
"github.com/datumforge/datum/pkg/sessions"
"github.com/datumforge/datum/pkg/tokens"
"github.com/datumforge/datum/pkg/utils/emails"
"github.com/datumforge/datum/pkg/utils/sentry"
"github.com/datumforge/datum/pkg/utils/storage/fs"
"github.com/datumforge/datum/pkg/utils/storage/gcs"
"github.com/datumforge/datum/pkg/utils/storage/s3"
"github.com/datumforge/datum/pkg/utils/totp"
)
var (
DefaultConfigFilePath = "./config/.config.yaml"
)
// Config contains the configuration for the datum server
type Config struct {
// RefreshInterval determines how often to reload the config
RefreshInterval time.Duration `json:"refreshInterval" koanf:"refreshInterval" default:"10m"`
// Server contains the echo server settings
Server Server `json:"server" koanf:"server"`
// Auth contains the authentication token settings and provider(s)
Auth Auth `json:"auth" koanf:"auth"`
// Authz contains the authorization settings for fine grained access control
Authz fgax.Config `json:"authz" koanf:"authz"`
// DB contains the database configuration for the ent client
DB entx.Config `json:"db" koanf:"db"`
// Geodetic contains the geodetic client configuration
Geodetic geodeticclient.Config `json:"geodetic" koanf:"geodetic"`
// Redis contains the redis configuration for the key-value store
Redis cache.Config `json:"redis" koanf:"redis"`
// Tracer contains the tracing config for opentelemetry
Tracer otelx.Config `json:"tracer" koanf:"tracer"`
// Email contains email sending configuration for the server
Email emails.Config `json:"email" koanf:"email"`
// Sessions config for user sessions and cookies
Sessions sessions.Config `json:"sessions" koanf:"sessions"`
// Sentry contains the sentry configuration for error tracking
Sentry sentry.Config `json:"sentry" koanf:"sentry"`
// PostHog contains the configuration for the PostHog analytics
PostHog posthog.Config `json:"posthog" koanf:"posthog"`
// TOTP contains the configuration for the TOTP provider
TOTP totp.Config `json:"totp" koanf:"totp"`
// Ratelimit contains the configuration for the rate limiter
Ratelimit ratelimit.Config `json:"ratelimit" koanf:"ratelimit"`
// Storage contains the configuration for the storage provider
ObjStorage ObjectStorage `json:"objstorage" koanf:"objstorage"`
// EventPublisher contains the configuration for the event publisher
Events kafkaconfig.Config `json:"publisherConfig" koanf:"publisherConfig"`
}
// ObjectStorage contains the configuration for the object storage provider
type ObjectStorage struct {
// S3 contains the s3 configuration for the object storage
S3 s3.Config `json:"s3" koanf:"s3"`
// GCS contains the gcs configuration for the object storage
GCS gcs.Config `json:"gcs" koanf:"gcs"`
// FS contains the fs configuration for the object storage
FS fs.Config `json:"fs" koanf:"fs"`
}
// Server settings for the echo server
type Server struct {
// Debug enables debug mode for the server
Debug bool `json:"debug" koanf:"debug" default:"false"`
// Dev enables echo's dev mode options
Dev bool `json:"dev" koanf:"dev" default:"false"`
// Listen sets the listen address to serve the echo server on
Listen string `json:"listen" koanf:"listen" jsonschema:"required" default:":17608"`
// ShutdownGracePeriod sets the grace period for in flight requests before shutting down
ShutdownGracePeriod time.Duration `json:"shutdownGracePeriod" koanf:"shutdownGracePeriod" default:"10s"`
// ReadTimeout sets the maximum duration for reading the entire request including the body
ReadTimeout time.Duration `json:"readTimeout" koanf:"readTimeout" default:"15s"`
// WriteTimeout sets the maximum duration before timing out writes of the response
WriteTimeout time.Duration `json:"writeTimeout" koanf:"writeTimeout" default:"15s"`
// IdleTimeout sets the maximum amount of time to wait for the next request when keep-alives are enabled
IdleTimeout time.Duration `json:"idleTimeout" koanf:"idleTimeout" default:"30s"`
// ReadHeaderTimeout sets the amount of time allowed to read request headers
ReadHeaderTimeout time.Duration `json:"readHeaderTimeout" koanf:"readHeaderTimeout" default:"2s"`
// TLS contains the tls configuration settings
TLS TLS `json:"tls" koanf:"tls"`
// CORS contains settings to allow cross origin settings and insecure cookies
CORS cors.Config `json:"cors" koanf:"cors"`
// Secure contains settings for the secure middleware
Secure secure.Config `json:"secure" koanf:"secure"`
// Redirect contains settings for the redirect middleware
Redirects redirect.Config `json:"redirects" koanf:"redirects"`
// CacheControl contains settings for the cache control middleware
CacheControl cachecontrol.Config `json:"cacheControl" koanf:"cacheControl"`
// Mime contains settings for the mime middleware
Mime mime.Config `json:"mime" koanf:"mime"`
}
// Auth settings including oauth2 providers and datum token configuration
type Auth struct {
// Enabled authentication on the server, not recommended to disable
Enabled bool `json:"enabled" koanf:"enabled" default:"true"`
// Token contains the token config settings for Datum issued tokens
Token tokens.Config `json:"token" koanf:"token" jsonschema:"required" alias:"tokenconfig"`
// SupportedProviders are the supported oauth providers that have been configured
SupportedProviders []string `json:"supportedProviders" koanf:"supportedProviders"`
// Providers contains supported oauth2 providers configuration
Providers handlers.OauthProviderConfig `json:"providers" koanf:"providers"`
}
// TLS settings for the server for secure connections
type TLS struct {
// Config contains the tls.Config settings
Config *tls.Config `json:"config" koanf:"config" jsonschema:"-"`
// Enabled turns on TLS settings for the server
Enabled bool `json:"enabled" koanf:"enabled" default:"false"`
// CertFile location for the TLS server
CertFile string `json:"certFile" koanf:"certFile" default:"server.crt"`
// CertKey file location for the TLS server
CertKey string `json:"certKey" koanf:"certKey" default:"server.key"`
// AutoCert generates the cert with letsencrypt, this does not work on localhost
AutoCert bool `json:"autoCert" koanf:"autoCert" default:"false"`
}
// Load is responsible for loading the configuration from a YAML file and environment variables.
// If the `cfgFile` is empty or nil, it sets the default configuration file path.
// Config settings are taken from default values, then from the config file, and finally from environment
// the later overwriting the former.
func Load(cfgFile *string) (*Config, error) {
k := koanf.New(".")
if cfgFile == nil || *cfgFile == "" {
*cfgFile = DefaultConfigFilePath
}
// load defaults
conf := &Config{}
defaults.SetDefaults(conf)
// parse yaml config
if err := k.Load(file.Provider(*cfgFile), yaml.Parser()); err != nil {
panic(err)
}
// unmarshal the config
if err := k.Unmarshal("", &conf); err != nil {
panic(err)
}
// load env vars
if err := k.Load(env.ProviderWithValue("DATUM_", ".", func(s string, v string) (string, interface{}) {
key := strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(s, "DATUM_")), "_", ".")
if strings.Contains(v, ",") {
return key, strings.Split(v, ",")
}
return key, v
}), nil); err != nil {
panic(err)
}
// unmarshal the env vars
if err := k.Unmarshal("", &conf); err != nil {
panic(err)
}
return conf, nil
}