/
config.go
58 lines (50 loc) · 1.19 KB
/
config.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
50
51
52
53
54
55
56
57
58
package config
import (
"github.com/joho/godotenv"
"os"
)
const (
defaultPort = "8080"
defaultUrl = "https://api.coindesk.com/v1/bpi/currentprice.json"
defaultDbUrl = "mongodb://localhost:27017"
defaultDbName = "pricer"
)
// Secrets contain all the config that this application needs
type Secrets struct {
Port string `json:"port"`
CoindeskURL string `json:"coindesk_url"`
DBName string `json:"db_name"`
DBURL string `json:"dburl"`
}
// LoadSecrets loads secrets from the environment and returns it
// if a .env file is present, it would be loaded first
// default values are also set
func LoadSecrets(filename ...string) *Secrets {
f := ".env"
if len(filename) > 0 {
f = filename[0]
}
_ = godotenv.Load(f)
secrets := &Secrets{}
port, ok := os.LookupEnv("PORT")
if !ok {
port = defaultPort
}
secrets.Port = port
coindeskUrl, ok := os.LookupEnv("COINDESK_URL")
if !ok {
coindeskUrl = defaultUrl
}
secrets.CoindeskURL = coindeskUrl
dbUrl, ok := os.LookupEnv("DATABASE_URL")
if !ok {
dbUrl = defaultDbUrl
}
secrets.DBURL = dbUrl
dbName, ok := os.LookupEnv("DATABASE_NAME")
if !ok {
dbName = defaultDbName
}
secrets.DBName = dbName
return secrets
}