From 681ee9f1d0f943405ef4d921fa01dba63e1fd2bc Mon Sep 17 00:00:00 2001 From: Lukas Schulte Pelkum Date: Thu, 15 Apr 2021 20:59:32 +0200 Subject: [PATCH] Fix some stuff --- cmd/transfer/main.go | 8 +++---- internal/config/config.go | 45 +++++++++++++++++++-------------------- internal/env/env.go | 6 +++--- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/cmd/transfer/main.go b/cmd/transfer/main.go index ea286ce..a54d0aa 100644 --- a/cmd/transfer/main.go +++ b/cmd/transfer/main.go @@ -4,7 +4,7 @@ import ( "log" "os" - "github.com/lus/pasty/internal/env" + "github.com/lus/pasty/internal/config" "github.com/lus/pasty/internal/shared" "github.com/lus/pasty/internal/storage" ) @@ -15,9 +15,9 @@ func main() { panic("Invalid command line arguments") } - // Load the optional .env file - log.Println("Loading the optional .env file...") - env.Load() + // Load the configuration + log.Println("Loading the application configuration...") + config.Load() // Create and initialize the first (from) driver from, err := storage.GetDriver(shared.StorageType(os.Args[1])) diff --git a/internal/config/config.go b/internal/config/config.go index 686e05c..a266121 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,7 +4,6 @@ import ( "strings" "time" - "github.com/joho/godotenv" "github.com/lus/pasty/internal/env" "github.com/lus/pasty/internal/shared" ) @@ -64,39 +63,39 @@ var Current *Config // Load loads the current config from environment variables and an optional .env file func Load() { - godotenv.Load() + env.Load() Current = &Config{ - WebAddress: env.MustString("PASTY_WEB_ADDRESS", ":8080"), - StorageType: shared.StorageType(strings.ToLower(env.MustString("PASTY_STORAGE_TYPE", "file"))), - HastebinSupport: env.MustBool("PASTY_HASTEBIN_SUPPORT", false), - IDLength: env.MustInt("PASTY_ID_LENGTH", 6), - DeletionTokenLength: env.MustInt("PASTY_DELETION_TOKEN_LENGTH", 12), - RateLimit: env.MustString("PASTY_RATE_LIMIT", "30-M"), + WebAddress: env.MustString("WEB_ADDRESS", ":8080"), + StorageType: shared.StorageType(strings.ToLower(env.MustString("STORAGE_TYPE", "file"))), + HastebinSupport: env.MustBool("HASTEBIN_SUPPORT", false), + IDLength: env.MustInt("ID_LENGTH", 6), + DeletionTokenLength: env.MustInt("DELETION_TOKEN_LENGTH", 12), + RateLimit: env.MustString("RATE_LIMIT", "30-M"), AutoDelete: &AutoDeleteConfig{ - Enabled: env.MustBool("PASTY_AUTODELETE", false), - Lifetime: env.MustDuration("PASTY_AUTODELETE_LIFETIME", 720*time.Hour), - TaskInterval: env.MustDuration("PASTY_AUTODELETE_TASK_INTERVAL", 5*time.Minute), + Enabled: env.MustBool("AUTODELETE", false), + Lifetime: env.MustDuration("AUTODELETE_LIFETIME", 720*time.Hour), + TaskInterval: env.MustDuration("AUTODELETE_TASK_INTERVAL", 5*time.Minute), }, File: &FileConfig{ - Path: env.MustString("PASTY_STORAGE_FILE_PATH", "./data"), + Path: env.MustString("STORAGE_FILE_PATH", "./data"), }, Postgres: &PostgresConfig{ - DSN: env.MustString("PASTY_STORAGE_POSTGRES_DSN", "postgres://pasty:pasty@localhost/pasty"), + DSN: env.MustString("STORAGE_POSTGRES_DSN", "postgres://pasty:pasty@localhost/pasty"), }, MongoDB: &MongoDBConfig{ - DSN: env.MustString("PASTY_STORAGE_MONGODB_CONNECTION_STRING", "mongodb://pasty:pasty@localhost/pasty"), - Database: env.MustString("PASTY_STORAGE_MONGODB_DATABASE", "pasty"), - Collection: env.MustString("PASTY_STORAGE_MONGODB_COLLECTION", "pastes"), + DSN: env.MustString("STORAGE_MONGODB_CONNECTION_STRING", "mongodb://pasty:pasty@localhost/pasty"), + Database: env.MustString("STORAGE_MONGODB_DATABASE", "pasty"), + Collection: env.MustString("STORAGE_MONGODB_COLLECTION", "pastes"), }, S3: &S3Config{ - Endpoint: env.MustString("PASTY_STORAGE_S3_ENDPOINT", ""), - AccessKeyID: env.MustString("PASTY_STORAGE_S3_ACCESS_KEY_ID", ""), - SecretAccessKey: env.MustString("PASTY_STORAGE_S3_SECRET_ACCESS_KEY", ""), - SecretToken: env.MustString("PASTY_STORAGE_S3_SECRET_TOKEN", ""), - Secure: env.MustBool("PASTY_STORAGE_S3_SECURE", true), - Region: env.MustString("PASTY_STORAGE_S3_REGION", ""), - Bucket: env.MustString("PASTY_STORAGE_S3_BUCKET", "pasty"), + Endpoint: env.MustString("STORAGE_S3_ENDPOINT", ""), + AccessKeyID: env.MustString("STORAGE_S3_ACCESS_KEY_ID", ""), + SecretAccessKey: env.MustString("STORAGE_S3_SECRET_ACCESS_KEY", ""), + SecretToken: env.MustString("STORAGE_S3_SECRET_TOKEN", ""), + Secure: env.MustBool("STORAGE_S3_SECURE", true), + Region: env.MustString("STORAGE_S3_REGION", ""), + Bucket: env.MustString("STORAGE_S3_BUCKET", "pasty"), }, } } diff --git a/internal/env/env.go b/internal/env/env.go index 4fc7e59..4a568a9 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -16,11 +16,11 @@ func Load() { // MustString returns the content of the environment variable with the given key or the given fallback func MustString(key, fallback string) string { - found := os.Getenv(static.EnvironmentVariablePrefix + key) - if found == "" { + value, found := os.LookupEnv(static.EnvironmentVariablePrefix + key) + if !found { return fallback } - return found + return value } // MustBool uses MustString and parses it into a boolean