/
env.go
49 lines (40 loc) · 1.1 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package env
import (
"os"
"strings"
"github.com/joho/godotenv"
)
const (
appEnvironment = "APP_ENV"
devEnvironment = "development"
)
func init() {
// error is ignored because we don't care if the .env is found and loaded,
// in production there isn't one anyway
godotenv.Load()
}
// Get returns the value of the specified environment variable
func Get(key string) string {
return os.Getenv(key)
}
// GetDefault returns the value of the specified environment variable
// If the variable is not defined or is empty, return the default value
func GetDefault(key, defaultValue string) string {
value := Get(key)
if strings.TrimSpace(value) == "" {
return defaultValue
}
return value
}
// AppEnvironment returns the name of the current environment the app is in
func AppEnvironment() string {
return strings.ToLower(Get(appEnvironment))
}
// IsDevelopment returns true if the current environment is development
func IsDevelopment() bool {
return AppEnvironment() == devEnvironment
}
// IsProduction returns if the current environment is production
func IsProduction() bool {
return !IsDevelopment()
}