You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
977 B
Go
45 lines
977 B
Go
// Loads ".env" file and provides environment variables as global values.
|
|
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
// Mode represents the MODE environment variable
|
|
var Mode string
|
|
|
|
// Host represents the HOST environment variable
|
|
var Host string
|
|
|
|
// Url represents the URL environment variable
|
|
var Url string
|
|
|
|
// Email represents the EMAIL environment variable
|
|
var Email string
|
|
|
|
func loadEnv(target *string, name, defaultValue string) {
|
|
value := os.Getenv(name)
|
|
if len(strings.TrimSpace(value)) == 0 {
|
|
*target = defaultValue
|
|
} else {
|
|
*target = value
|
|
}
|
|
log.Printf("%s = %v", name, *target)
|
|
}
|
|
|
|
// Load calls "godotenv.Load()" and sets all the above exported variables
|
|
func Load() {
|
|
godotenv.Load()
|
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
|
|
|
loadEnv(&Mode, "MODE", "production")
|
|
loadEnv(&Host, "HOST", "localhost:4000")
|
|
loadEnv(&Url, "URL", "http://localhost:3000")
|
|
loadEnv(&Email, "EMAIL", "mail@example.org")
|
|
}
|