feat(backend): add transactional outbox and embedded goose migrations
This commit is contained in:
1 parent
653f3775f8
commit
15e2e50561
40 files changed
+2471
-67
No files matched your search
+53
-19
@@ -18,27 +18,32 @@ const (
|
||||
defaultRedisTimeout = time.Second
|
||||
defaultReadinessTimeout = time.Second
|
||||
defaultShutdownTimeout = 10 * time.Second
|
||||
maxPortNumber = 65535
|
||||
// Startup migrations are on by default so a deployed binary never serves
|
||||
// with a schema older than the one it was built for.
|
||||
defaultDatabaseMigrateOnStart = true
|
||||
maxPortNumber = 65535
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HTTPAddr string
|
||||
MetricsAddr string
|
||||
DatabaseURL string
|
||||
RedisAddr string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
RedisTimeout time.Duration
|
||||
SeaweedFSEndpoint string
|
||||
SeaweedFSAccessKey string
|
||||
SeaweedFSSecretKey string
|
||||
SeaweedFSBucket string
|
||||
SeaweedFSRegion string
|
||||
SeaweedFSTimeout time.Duration
|
||||
KafkaBrokers []string
|
||||
LogLevel string
|
||||
ReadinessTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
HTTPAddr string
|
||||
MetricsAddr string
|
||||
DatabaseURL string
|
||||
// DatabaseMigrateOnStart applies embedded migrations before serving.
|
||||
DatabaseMigrateOnStart bool
|
||||
RedisAddr string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
RedisTimeout time.Duration
|
||||
SeaweedFSEndpoint string
|
||||
SeaweedFSAccessKey string
|
||||
SeaweedFSSecretKey string
|
||||
SeaweedFSBucket string
|
||||
SeaweedFSRegion string
|
||||
SeaweedFSTimeout time.Duration
|
||||
KafkaBrokers []string
|
||||
LogLevel string
|
||||
ReadinessTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
}
|
||||
|
||||
type LookupEnv func(string) (string, bool)
|
||||
@@ -51,7 +56,7 @@ func LoadFromEnv(lookup LookupEnv) (Config, error) {
|
||||
config := Config{
|
||||
HTTPAddr: valueOrDefault(lookup, "HTTP_ADDR", defaultHTTPAddr),
|
||||
MetricsAddr: valueOrDefault(lookup, "METRICS_ADDR", defaultMetricsAddr),
|
||||
DatabaseURL: strings.TrimSpace(valueOrDefault(lookup, "DATABASE_URL", "")),
|
||||
DatabaseURL: DatabaseURLFromEnv(lookup),
|
||||
RedisAddr: strings.TrimSpace(valueOrDefault(lookup, "REDIS_ADDR", defaultRedisAddr)),
|
||||
RedisPassword: valueOrDefault(lookup, "REDIS_PASSWORD", ""),
|
||||
RedisTimeout: defaultRedisTimeout,
|
||||
@@ -72,6 +77,11 @@ func LoadFromEnv(lookup LookupEnv) (Config, error) {
|
||||
}
|
||||
|
||||
var err error
|
||||
config.DatabaseMigrateOnStart, err = boolOrDefault(lookup, "DATABASE_MIGRATE_ON_START", defaultDatabaseMigrateOnStart)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
config.SeaweedFSTimeout, err = durationOrDefault(lookup, "SEAWEEDFS_TIMEOUT", config.SeaweedFSTimeout)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
@@ -130,6 +140,30 @@ func valueOrDefault(lookup LookupEnv, key, fallback string) string {
|
||||
return fallback
|
||||
}
|
||||
|
||||
// DatabaseURLFromEnv reads DATABASE_URL for commands that need only the
|
||||
// database, such as the migration command.
|
||||
func DatabaseURLFromEnv(lookup LookupEnv) string {
|
||||
return strings.TrimSpace(valueOrDefault(lookup, "DATABASE_URL", ""))
|
||||
}
|
||||
|
||||
// boolOrDefault accepts only "true" or "false", ignoring case and surrounding
|
||||
// spaces. strconv.ParseBool would also accept 1, t, and similar shorthands,
|
||||
// which are easy to mistype in deployment files.
|
||||
func boolOrDefault(lookup LookupEnv, key string, fallback bool) (bool, error) {
|
||||
value, ok := lookup(key)
|
||||
if !ok {
|
||||
return fallback, nil
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "true":
|
||||
return true, nil
|
||||
case "false":
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("config.boolOrDefault - %s must be true or false", key)
|
||||
}
|
||||
}
|
||||
|
||||
func durationOrDefault(lookup LookupEnv, key string, fallback time.Duration) (time.Duration, error) {
|
||||
value, ok := lookup(key)
|
||||
if !ok {
|
||||
|
||||
Reference in new issue
Block a user