Add support for loading .env files

This commit is contained in:
Divyam Ahuja 2024-10-26 09:13:56 +05:30
parent 6f3da69252
commit 06d38a5e2f
3 changed files with 36 additions and 0 deletions

1
.gitignore vendored
View file

@ -0,0 +1 @@
.env

28
internal/pkg/env/env.go vendored Normal file
View file

@ -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
}

7
syntax.env Normal file
View file

@ -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