From 06d38a5e2fc0946520f7ca7938b4faf7322748b0 Mon Sep 17 00:00:00 2001 From: Divyam Ahuja Date: Sat, 26 Oct 2024 09:13:56 +0530 Subject: [PATCH] Add support for loading .env files --- .gitignore | 1 + internal/pkg/env/env.go | 28 ++++++++++++++++++++++++++++ syntax.env | 7 +++++++ 3 files changed, 36 insertions(+) create mode 100644 internal/pkg/env/env.go create mode 100644 syntax.env diff --git a/.gitignore b/.gitignore index e69de29..2eea525 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/internal/pkg/env/env.go b/internal/pkg/env/env.go new file mode 100644 index 0000000..9370474 --- /dev/null +++ b/internal/pkg/env/env.go @@ -0,0 +1,28 @@ +package env + +import ( + "os" + + "github.com/joho/godotenv" +) + +func init() { + env := os.Getenv("FASTBIN_ENV") + if env == "" { + env = "development" + } + + godotenv.Load(".env." + env + ".local") + if env != "test" { + godotenv.Load(".env.local") + } + godotenv.Load(".env." + env) + godotenv.Load() +} + +func GetEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} diff --git a/syntax.env b/syntax.env new file mode 100644 index 0000000..3a6963a --- /dev/null +++ b/syntax.env @@ -0,0 +1,7 @@ +API_PORT=8080 +WEB_PORT=8081 +DB_HOST=localhost +DB_PORT=5432 +DB_DATABASE=fastbin +DB_USERNAME=username +DB_PASSWORD=password \ No newline at end of file