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 {
|
||||
|
||||
@@ -21,6 +21,9 @@ func TestLoadFromEnvUsesOperationalDefaults(t *testing.T) {
|
||||
if config.DatabaseURL != "" {
|
||||
t.Fatalf("DatabaseURL = %q, want empty", config.DatabaseURL)
|
||||
}
|
||||
if !config.DatabaseMigrateOnStart {
|
||||
t.Fatal("DatabaseMigrateOnStart = false, want startup migrations enabled by default")
|
||||
}
|
||||
if config.RedisAddr != "localhost:6379" || config.RedisDB != 0 || config.RedisTimeout != time.Second || config.RedisPassword != "" {
|
||||
t.Fatalf("unexpected Redis defaults")
|
||||
}
|
||||
@@ -34,16 +37,17 @@ func TestLoadFromEnvUsesOperationalDefaults(t *testing.T) {
|
||||
|
||||
func TestLoadFromEnvReadsAddressesAndTimeouts(t *testing.T) {
|
||||
values := map[string]string{
|
||||
"HTTP_ADDR": "127.0.0.1:18080",
|
||||
"REDIS_ADDR": "cache:6379",
|
||||
"REDIS_PASSWORD": " local-secret ",
|
||||
"REDIS_DB": "2",
|
||||
"REDIS_TIMEOUT": "500ms",
|
||||
"METRICS_ADDR": "127.0.0.1:19090",
|
||||
"DATABASE_URL": " postgres://app:secret@db/app ",
|
||||
"LOG_LEVEL": " DEBUG ",
|
||||
"READINESS_TIMEOUT": "2s",
|
||||
"SHUTDOWN_TIMEOUT": "15s",
|
||||
"HTTP_ADDR": "127.0.0.1:18080",
|
||||
"REDIS_ADDR": "cache:6379",
|
||||
"REDIS_PASSWORD": " local-secret ",
|
||||
"REDIS_DB": "2",
|
||||
"REDIS_TIMEOUT": "500ms",
|
||||
"METRICS_ADDR": "127.0.0.1:19090",
|
||||
"DATABASE_URL": " postgres://app:secret@db/app ",
|
||||
"DATABASE_MIGRATE_ON_START": " FALSE ",
|
||||
"LOG_LEVEL": " DEBUG ",
|
||||
"READINESS_TIMEOUT": "2s",
|
||||
"SHUTDOWN_TIMEOUT": "15s",
|
||||
}
|
||||
config, err := LoadFromEnv(testEnvironment(values))
|
||||
if err != nil {
|
||||
@@ -58,6 +62,9 @@ func TestLoadFromEnvReadsAddressesAndTimeouts(t *testing.T) {
|
||||
if config.DatabaseURL != "postgres://app:secret@db/app" {
|
||||
t.Fatalf("DatabaseURL was not trimmed: %q", config.DatabaseURL)
|
||||
}
|
||||
if config.DatabaseMigrateOnStart {
|
||||
t.Fatal("DatabaseMigrateOnStart = true, want the explicit false value")
|
||||
}
|
||||
if config.LogLevel != "debug" {
|
||||
t.Fatalf("LogLevel was not normalized: %q", config.LogLevel)
|
||||
}
|
||||
@@ -84,6 +91,10 @@ func TestLoadFromEnvRejectsInvalidSettings(t *testing.T) {
|
||||
{name: "negative Redis database", values: map[string]string{"REDIS_DB": "-1"}, want: "Config.ValidateRedis - REDIS_DB"},
|
||||
{name: "invalid Redis timeout", values: map[string]string{"REDIS_TIMEOUT": "soon"}, want: "config.durationOrDefault - parse REDIS_TIMEOUT"},
|
||||
{name: "zero Redis timeout", values: map[string]string{"REDIS_TIMEOUT": "0s"}, want: "Config.ValidateRedis - REDIS_TIMEOUT"},
|
||||
{name: "invalid migrate on start", values: map[string]string{"DATABASE_MIGRATE_ON_START": "sometimes"}, want: "config.boolOrDefault - DATABASE_MIGRATE_ON_START"},
|
||||
{name: "numeric migrate on start", values: map[string]string{"DATABASE_MIGRATE_ON_START": "1"}, want: "config.boolOrDefault - DATABASE_MIGRATE_ON_START"},
|
||||
{name: "short migrate on start", values: map[string]string{"DATABASE_MIGRATE_ON_START": "t"}, want: "config.boolOrDefault - DATABASE_MIGRATE_ON_START"},
|
||||
{name: "empty migrate on start", values: map[string]string{"DATABASE_MIGRATE_ON_START": ""}, want: "config.boolOrDefault - DATABASE_MIGRATE_ON_START"},
|
||||
{name: "invalid log level", values: map[string]string{"LOG_LEVEL": "trace"}, want: "Config.Validate - LOG_LEVEL"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in new issue
Block a user