Add support for loading .env files
This commit is contained in:
parent
6f3da69252
commit
06d38a5e2f
3 changed files with 36 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -0,0 +1 @@
|
||||||
|
.env
|
||||||
28
internal/pkg/env/env.go
vendored
Normal file
28
internal/pkg/env/env.go
vendored
Normal 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
7
syntax.env
Normal 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
|
||||||
Loading…
Reference in a new issue