forked from corestoreio/parrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
72 lines (66 loc) · 1.49 KB
/
configuration.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package config
import (
"os"
"gopkg.in/yaml.v2"
)
type AppConfig struct {
Port string `yaml:"port"`
DBName string `yaml:"dbName"`
DBConn string `yaml:"dbConn"`
AuthIssuer string `yaml:"authIssuer"`
AuthSigningKey string `yaml:"authSigningKey"`
}
func FromYaml(data []byte) (*AppConfig, error) {
conf := &AppConfig{}
err := yaml.Unmarshal(data, conf)
if err != nil {
return nil, err
}
return conf, nil
}
func SetOrDefault(conf *AppConfig) {
if conf.Port == "" {
conf.Port = "9990"
}
if conf.DBName == "" {
conf.DBName = "postgres"
}
if conf.DBConn == "" {
conf.DBConn = "postgres://postgres@localhost:5432/parrot?sslmode=disable"
}
if conf.AuthIssuer == "" {
conf.AuthIssuer = "parrot@localhost"
}
if conf.AuthSigningKey == "" {
conf.AuthSigningKey = "secret"
}
}
func FromEnv() (*AppConfig, error) {
port, ok := os.LookupEnv("PARROT_API_PORT")
if !ok {
port = "9990"
}
dbName, ok := os.LookupEnv("PARROT_DB_NAME")
if !ok {
dbName = "postgres"
}
dbConn, ok := os.LookupEnv("PARROT_DB_CONN")
if !ok {
dbConn = "postgres://postgres@localhost:5432/parrot?sslmode=disable"
}
authIssuer, ok := os.LookupEnv("PARROT_AUTH_ISSUER")
if !ok {
authIssuer = "parrot@localhost"
}
authSigningKey, ok := os.LookupEnv("PARROT_AUTH_SIGNING_KEY")
if !ok {
authSigningKey = "secret"
}
return &AppConfig{
Port: port,
DBName: dbName,
DBConn: dbConn,
AuthIssuer: authIssuer,
AuthSigningKey: authSigningKey,
}, nil
}