feat(env): Added production and development environment
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m44s
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m44s
This commit is contained in:
@@ -28,6 +28,9 @@ func TestDefaults(t *testing.T) {
|
||||
if cfg.AdminUsername != "admin" {
|
||||
t.Fatalf("unexpected admin username: %s", cfg.AdminUsername)
|
||||
}
|
||||
if cfg.Environment != AppEnvironmentDevelopment {
|
||||
t.Fatalf("unexpected default environment: %s", cfg.Environment)
|
||||
}
|
||||
if cfg.AdminPassword != "" {
|
||||
t.Fatal("expected default admin password to be empty")
|
||||
}
|
||||
@@ -43,6 +46,7 @@ func TestEnvironmentOverrides(t *testing.T) {
|
||||
t.Setenv("WARPBOX_ADMIN_USERNAME", "root")
|
||||
t.Setenv("WARPBOX_ONE_TIME_DOWNLOAD_RETRY_ON_FAILURE", "true")
|
||||
t.Setenv("WARPBOX_SECURITY_ENABLED", "false")
|
||||
t.Setenv("WARPBOX_ENV", "production")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
@@ -73,6 +77,9 @@ func TestEnvironmentOverrides(t *testing.T) {
|
||||
if cfg.Source(SettingAPIEnabled) != SourceEnv {
|
||||
t.Fatalf("expected API setting source to be env, got %s", cfg.Source(SettingAPIEnabled))
|
||||
}
|
||||
if cfg.Environment != AppEnvironmentProduction {
|
||||
t.Fatalf("expected environment override to be production, got %s", cfg.Environment)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMegabyteSizeEnvironmentOverrides(t *testing.T) {
|
||||
@@ -120,6 +127,12 @@ func TestInvalidEnvironmentValues(t *testing.T) {
|
||||
if _, err := Load(); err == nil {
|
||||
t.Fatal("expected invalid boolean to fail")
|
||||
}
|
||||
|
||||
clearConfigEnv(t)
|
||||
t.Setenv("WARPBOX_ENV", "staging")
|
||||
if _, err := Load(); err == nil {
|
||||
t.Fatal("expected invalid environment mode to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsOverridePrecedence(t *testing.T) {
|
||||
@@ -170,6 +183,7 @@ func clearConfigEnv(t *testing.T) {
|
||||
"WARPBOX_ADMIN_PASSWORD",
|
||||
"WARPBOX_ADMIN_USERNAME",
|
||||
"WARPBOX_ADMIN_EMAIL",
|
||||
"WARPBOX_ENV",
|
||||
"WARPBOX_ADMIN_ENABLED",
|
||||
"WARPBOX_ALLOW_ADMIN_SETTINGS_OVERRIDE",
|
||||
"WARPBOX_ADMIN_COOKIE_SECURE",
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
var Definitions = []SettingDefinition{
|
||||
{Key: SettingDataDir, EnvName: "WARPBOX_DATA_DIR", Label: "Data directory", Type: SettingTypeText, Editable: false, HardLimit: true},
|
||||
{Key: SettingEnvironment, EnvName: "WARPBOX_ENV", Label: "Environment", Type: SettingTypeText, Editable: false, HardLimit: true},
|
||||
{Key: SettingGuestUploadsEnabled, EnvName: "WARPBOX_GUEST_UPLOADS_ENABLED", Label: "Guest uploads enabled", Type: SettingTypeBool, Editable: true},
|
||||
{Key: SettingAPIEnabled, EnvName: "WARPBOX_API_ENABLED", Label: "API enabled", Type: SettingTypeBool, Editable: true},
|
||||
{Key: SettingZipDownloadsEnabled, EnvName: "WARPBOX_ZIP_DOWNLOADS_ENABLED", Label: "ZIP downloads enabled", Type: SettingTypeBool, Editable: true},
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
func Load() (*Config, error) {
|
||||
cfg := &Config{
|
||||
DataDir: "./data",
|
||||
Environment: AppEnvironmentDevelopment,
|
||||
AdminUsername: "admin",
|
||||
AdminEnabled: AdminEnabledAuto,
|
||||
AllowAdminSettingsOverride: true,
|
||||
@@ -49,6 +50,14 @@ func Load() (*Config, error) {
|
||||
if err := cfg.applyStringEnv(SettingDataDir, "WARPBOX_DATA_DIR", &cfg.DataDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if raw := strings.TrimSpace(os.Getenv("WARPBOX_ENV")); raw != "" {
|
||||
env := AppEnvironment(strings.ToLower(raw))
|
||||
if env != AppEnvironmentDevelopment && env != AppEnvironmentProduction {
|
||||
return nil, fmt.Errorf("WARPBOX_ENV must be development or production")
|
||||
}
|
||||
cfg.Environment = env
|
||||
cfg.setValue(SettingEnvironment, string(env), SourceEnv)
|
||||
}
|
||||
if err := cfg.applyStringEnv("", "WARPBOX_ADMIN_PASSWORD", &cfg.AdminPassword); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -194,6 +203,7 @@ func (cfg *Config) EnsureDirectories() error {
|
||||
}
|
||||
func (cfg *Config) captureDefaults() {
|
||||
cfg.captureDefaultValue(SettingDataDir, cfg.DataDir)
|
||||
cfg.captureDefaultValue(SettingEnvironment, string(cfg.Environment))
|
||||
cfg.captureDefaultValue(SettingGuestUploadsEnabled, formatBool(cfg.GuestUploadsEnabled))
|
||||
cfg.captureDefaultValue(SettingAPIEnabled, formatBool(cfg.APIEnabled))
|
||||
cfg.captureDefaultValue(SettingZipDownloadsEnabled, formatBool(cfg.ZipDownloadsEnabled))
|
||||
|
||||
@@ -16,6 +16,13 @@ const (
|
||||
AdminEnabledFalse AdminEnabledMode = "false"
|
||||
)
|
||||
|
||||
type AppEnvironment string
|
||||
|
||||
const (
|
||||
AppEnvironmentDevelopment AppEnvironment = "development"
|
||||
AppEnvironmentProduction AppEnvironment = "production"
|
||||
)
|
||||
|
||||
const (
|
||||
SettingGuestUploadsEnabled = "guest_uploads_enabled"
|
||||
SettingAPIEnabled = "api_enabled"
|
||||
@@ -36,6 +43,7 @@ const (
|
||||
SettingThumbnailBatchSize = "thumbnail_batch_size"
|
||||
SettingThumbnailIntervalSeconds = "thumbnail_interval_seconds"
|
||||
SettingDataDir = "data_dir"
|
||||
SettingEnvironment = "environment"
|
||||
SettingActivityRetentionSeconds = "activity_retention_seconds"
|
||||
SettingSecurityEnabled = "security_enabled"
|
||||
SettingSecurityIPWhitelist = "security_ip_whitelist"
|
||||
@@ -86,6 +94,7 @@ type Config struct {
|
||||
AdminPassword string
|
||||
AdminUsername string
|
||||
AdminEmail string
|
||||
Environment AppEnvironment
|
||||
AdminEnabled AdminEnabledMode
|
||||
AdminCookieSecure bool
|
||||
AllowAdminSettingsOverride bool
|
||||
|
||||
Reference in New Issue
Block a user