mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-02-16 17:00:19 +00:00
feat: remove DbProvider env variable and calculate it dynamically (#1114)
This commit is contained in:
@@ -42,7 +42,7 @@ type EnvConfigSchema struct {
|
|||||||
LogLevel string `env:"LOG_LEVEL" options:"toLower"`
|
LogLevel string `env:"LOG_LEVEL" options:"toLower"`
|
||||||
LogJSON bool `env:"LOG_JSON"`
|
LogJSON bool `env:"LOG_JSON"`
|
||||||
AppURL string `env:"APP_URL" options:"toLower,trimTrailingSlash"`
|
AppURL string `env:"APP_URL" options:"toLower,trimTrailingSlash"`
|
||||||
DbProvider DbProvider `env:"DB_PROVIDER" options:"toLower"`
|
DbProvider DbProvider
|
||||||
DbConnectionString string `env:"DB_CONNECTION_STRING" options:"file"`
|
DbConnectionString string `env:"DB_CONNECTION_STRING" options:"file"`
|
||||||
EncryptionKey []byte `env:"ENCRYPTION_KEY" options:"file"`
|
EncryptionKey []byte `env:"ENCRYPTION_KEY" options:"file"`
|
||||||
Port string `env:"PORT"`
|
Port string `env:"PORT"`
|
||||||
@@ -131,17 +131,14 @@ func ValidateEnvConfig(config *EnvConfigSchema) error {
|
|||||||
return errors.New("ENCRYPTION_KEY must be at least 16 bytes long")
|
return errors.New("ENCRYPTION_KEY must be at least 16 bytes long")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch config.DbProvider {
|
switch {
|
||||||
case DbProviderSqlite:
|
case config.DbConnectionString == "":
|
||||||
if config.DbConnectionString == "" {
|
config.DbProvider = DbProviderSqlite
|
||||||
config.DbConnectionString = defaultSqliteConnString
|
config.DbConnectionString = defaultSqliteConnString
|
||||||
}
|
case strings.HasPrefix(config.DbConnectionString, "postgres://") || strings.HasPrefix(config.DbConnectionString, "postgresql://"):
|
||||||
case DbProviderPostgres:
|
config.DbProvider = DbProviderPostgres
|
||||||
if config.DbConnectionString == "" {
|
|
||||||
return errors.New("missing required env var 'DB_CONNECTION_STRING' for Postgres database")
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return errors.New("invalid DB_PROVIDER value. Must be 'sqlite' or 'postgres'")
|
config.DbProvider = DbProviderSqlite
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedAppUrl, err := url.Parse(config.AppURL)
|
parsedAppUrl, err := url.Parse(config.AppURL)
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should parse valid SQLite config correctly", func(t *testing.T) {
|
t.Run("should parse valid SQLite config correctly", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "SQLITE") // should be lowercased automatically
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "HTTP://LOCALHOST:3000")
|
t.Setenv("APP_URL", "HTTP://LOCALHOST:3000")
|
||||||
|
|
||||||
@@ -43,7 +42,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should parse valid Postgres config correctly", func(t *testing.T) {
|
t.Run("should parse valid Postgres config correctly", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "POSTGRES")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "postgres://user:pass@localhost/db")
|
t.Setenv("DB_CONNECTION_STRING", "postgres://user:pass@localhost/db")
|
||||||
t.Setenv("APP_URL", "https://example.com")
|
t.Setenv("APP_URL", "https://example.com")
|
||||||
|
|
||||||
@@ -52,20 +50,8 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
assert.Equal(t, DbProviderPostgres, EnvConfig.DbProvider)
|
assert.Equal(t, DbProviderPostgres, EnvConfig.DbProvider)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should fail with invalid DB_PROVIDER", func(t *testing.T) {
|
|
||||||
EnvConfig = defaultConfig()
|
|
||||||
t.Setenv("DB_PROVIDER", "invalid")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "test")
|
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
|
||||||
|
|
||||||
err := parseAndValidateEnvConfig(t)
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.ErrorContains(t, err, "invalid DB_PROVIDER value")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("should fail when ENCRYPTION_KEY is too short", func(t *testing.T) {
|
t.Run("should fail when ENCRYPTION_KEY is too short", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
t.Setenv("APP_URL", "http://localhost:3000")
|
||||||
t.Setenv("ENCRYPTION_KEY", "short")
|
t.Setenv("ENCRYPTION_KEY", "short")
|
||||||
@@ -77,7 +63,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should set default SQLite connection string when DB_CONNECTION_STRING is empty", func(t *testing.T) {
|
t.Run("should set default SQLite connection string when DB_CONNECTION_STRING is empty", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
t.Setenv("APP_URL", "http://localhost:3000")
|
||||||
|
|
||||||
err := parseAndValidateEnvConfig(t)
|
err := parseAndValidateEnvConfig(t)
|
||||||
@@ -85,19 +70,8 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
assert.Equal(t, defaultSqliteConnString, EnvConfig.DbConnectionString)
|
assert.Equal(t, defaultSqliteConnString, EnvConfig.DbConnectionString)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should fail when Postgres DB_CONNECTION_STRING is missing", func(t *testing.T) {
|
|
||||||
EnvConfig = defaultConfig()
|
|
||||||
t.Setenv("DB_PROVIDER", "postgres")
|
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
|
||||||
|
|
||||||
err := parseAndValidateEnvConfig(t)
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.ErrorContains(t, err, "missing required env var 'DB_CONNECTION_STRING' for Postgres")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("should fail with invalid APP_URL", func(t *testing.T) {
|
t.Run("should fail with invalid APP_URL", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "€://not-a-valid-url")
|
t.Setenv("APP_URL", "€://not-a-valid-url")
|
||||||
|
|
||||||
@@ -108,7 +82,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should fail when APP_URL contains path", func(t *testing.T) {
|
t.Run("should fail when APP_URL contains path", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "http://localhost:3000/path")
|
t.Setenv("APP_URL", "http://localhost:3000/path")
|
||||||
|
|
||||||
@@ -119,7 +92,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should fail with invalid INTERNAL_APP_URL", func(t *testing.T) {
|
t.Run("should fail with invalid INTERNAL_APP_URL", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("INTERNAL_APP_URL", "€://not-a-valid-url")
|
t.Setenv("INTERNAL_APP_URL", "€://not-a-valid-url")
|
||||||
|
|
||||||
@@ -130,7 +102,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should fail when INTERNAL_APP_URL contains path", func(t *testing.T) {
|
t.Run("should fail when INTERNAL_APP_URL contains path", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("INTERNAL_APP_URL", "http://localhost:3000/path")
|
t.Setenv("INTERNAL_APP_URL", "http://localhost:3000/path")
|
||||||
|
|
||||||
@@ -141,7 +112,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should parse boolean environment variables correctly", func(t *testing.T) {
|
t.Run("should parse boolean environment variables correctly", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
t.Setenv("APP_URL", "http://localhost:3000")
|
||||||
t.Setenv("UI_CONFIG_DISABLED", "true")
|
t.Setenv("UI_CONFIG_DISABLED", "true")
|
||||||
@@ -161,7 +131,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should parse string environment variables correctly", func(t *testing.T) {
|
t.Run("should parse string environment variables correctly", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "postgres")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "postgres://test")
|
t.Setenv("DB_CONNECTION_STRING", "postgres://test")
|
||||||
t.Setenv("APP_URL", "https://prod.example.com")
|
t.Setenv("APP_URL", "https://prod.example.com")
|
||||||
t.Setenv("APP_ENV", "PRODUCTION")
|
t.Setenv("APP_ENV", "PRODUCTION")
|
||||||
@@ -182,7 +151,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should normalize file backend and default upload path", func(t *testing.T) {
|
t.Run("should normalize file backend and default upload path", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
t.Setenv("APP_URL", "http://localhost:3000")
|
||||||
t.Setenv("FILE_BACKEND", "FS")
|
t.Setenv("FILE_BACKEND", "FS")
|
||||||
@@ -196,7 +164,6 @@ func TestParseEnvConfig(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("should fail with invalid FILE_BACKEND value", func(t *testing.T) {
|
t.Run("should fail with invalid FILE_BACKEND value", func(t *testing.T) {
|
||||||
EnvConfig = defaultConfig()
|
EnvConfig = defaultConfig()
|
||||||
t.Setenv("DB_PROVIDER", "sqlite")
|
|
||||||
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
t.Setenv("DB_CONNECTION_STRING", "file:test.db")
|
||||||
t.Setenv("APP_URL", "http://localhost:3000")
|
t.Setenv("APP_URL", "http://localhost:3000")
|
||||||
t.Setenv("FILE_BACKEND", "invalid")
|
t.Setenv("FILE_BACKEND", "invalid")
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ services:
|
|||||||
service: pocket-id
|
service: pocket-id
|
||||||
environment:
|
environment:
|
||||||
- APP_ENV=test
|
- APP_ENV=test
|
||||||
- DB_PROVIDER=postgres
|
|
||||||
- DB_CONNECTION_STRING=postgres://postgres:postgres@postgres:5432/pocket-id
|
- DB_CONNECTION_STRING=postgres://postgres:postgres@postgres:5432/pocket-id
|
||||||
- FILE_BACKEND=${FILE_BACKEND}
|
- FILE_BACKEND=${FILE_BACKEND}
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|||||||
Reference in New Issue
Block a user