-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
68 lines (57 loc) · 2.21 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
59
60
61
62
63
64
65
66
67
68
package config
import (
"io/ioutil"
"os"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// Acme contains acme related configuration parameters
type Acme struct {
AccountEmail string `envconfig:"ACME_ACCOUNT_EMAIL" required:"true"`
DNSChallengeProvider string `envconfig:"ACME_DNS_CHALLENGE_PROVIDER" required:"true"`
DNSPropagationRequirement bool `envconfig:"ACME_DNS_PROPAGATION_REQUIREMENT" default:"true"`
ReregisterAccount bool `envconfig:"ACME_REREGISTER_ACCOUNT" default:"false"`
ServerURL string `envconfig:"ACME_SERVER_URL" default:"https://acme-staging-v02.api.letsencrypt.org/directory"`
}
// Vault contains vault related configuration parameters
type Vault struct {
ApproleRoleID string `envconfig:"VAULT_APPROLE_ROLE_ID"`
ApproleSecretID string `envconfig:"VAULT_APPROLE_SECRET_ID"`
KVStoragePath string `envconfig:"VAULT_KV_STORAGE_PATH" default:"secret/data/certificator/"`
}
type Log struct {
Format string `envconfig:"LOG_FORMAT" default:"JSON"`
Level string `envconfig:"LOG_LEVEL" default:"INFO"`
}
// Config contains all configuration parameters
type Config struct {
Acme Acme
Vault Vault
Log Log
DNSAddress string `envconfig:"DNS_ADDRESS" default:"127.0.0.1:53"`
Environment string `envconfig:"ENVIRONMENT" default:"prod"`
DomainsFile string `envconfig:"CERTIFICATOR_DOMAINS_FILE" default:"/code/domains.yml"`
RenewBeforeDays int `envconfig:"CERTIFICATOR_RENEW_BEFORE_DAYS" default:"30"`
Domains []string `yaml:"domains"`
}
// LoadConfig loads configuration options to variable
func LoadConfig() (Config, error) {
var cfg Config
err := envconfig.Process("", &cfg)
if err != nil {
return Config{}, errors.Wrapf(err, "failed getting config from env")
}
f, err := os.Open(cfg.DomainsFile)
if err != nil {
return Config{}, errors.Wrapf(err, "opening %s", cfg.DomainsFile)
}
content, err := ioutil.ReadAll(f)
if err != nil {
return Config{}, errors.Wrapf(err, "reading content of %s", cfg.DomainsFile)
}
if err := yaml.Unmarshal(content, &cfg); err != nil {
return Config{}, errors.Wrapf(err, "parsing %s", cfg.DomainsFile)
}
return cfg, err
}