This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
128 lines (113 loc) · 3.28 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
conf "github.com/datatogether/config"
"os"
"path/filepath"
)
// server modes
const (
DEVELOP_MODE = "develop"
PRODUCTION_MODE = "production"
TEST_MODE = "test"
)
// config holds all configuration for the server. It pulls from three places (in order):
// 1. environment variables
// 2. .[MODE].env OR .env
//
// globally-set env variables win.
// it's totally fine to not have, say, .env.develop defined, and just
// rely on a base ".env" file. But if you're in production mode & ".env.production"
// exists, that will be read *instead* of .env
//
// configuration is read at startup and cannot be alterd without restarting the server.
type config struct {
// port to listen on, will be read from PORT env variable if present.
Port string
// root url
UrlRoot string
// port to listen on for RPC calls
RpcPort string
// url of postgres app db
PostgresDbUrl string
// url of message que server
AmqpUrl string
// url for IPFS api methods
IpfsApiUrl string
// redis connection URL
RedisUrl string
// Public Key to use for signing. required.
PublicKey string
// TLS (HTTPS) enable support via LetsEncrypt, default false
// not needed if operating behind a TLS proxy
TLS bool
// if true, requests that have X-Forwarded-Proto: http will be redirected
// to their https variant, useful if operating behind a TLS proxy
ProxyForceHttps bool
// key for sending emails
PostmarkKey string
// list of email addresses that should get notifications
EmailNotificationRecipients []string
// CertbotResponse is only for doing manual SSL certificate generation via LetsEncrypt.
CertbotResponse string
}
// initConfig pulls configuration from config.json
func initConfig(mode string) (cfg *config, err error) {
cfg = &config{}
if path := configFilePath(mode, cfg); path != "" {
log.Infof("loading config file: %s", filepath.Base(path))
if err := conf.Load(cfg, path); err != nil {
log.Info("error loading config:", err)
}
} else {
if err := conf.Load(cfg); err != nil {
log.Info("error loading config:", err)
}
}
// make sure port is set
if cfg.Port == "" {
cfg.Port = "8080"
}
err = requireConfigStrings(map[string]string{
"PORT": cfg.Port,
"POSTGRES_DB_URL": cfg.PostgresDbUrl,
})
// output to stdout in dev mode
if mode == DEVELOP_MODE {
log.Out = os.Stdout
}
return
}
func packagePath(path string) string {
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/datatogether/task_mgmt", path)
}
// requireConfigStrings panics if any of the passed in values aren't set
func requireConfigStrings(values map[string]string) error {
for key, value := range values {
if value == "" {
return fmt.Errorf("%s env variable or config key must be set", key)
}
}
return nil
}
// checks for .[mode].env file to read configuration from if the file exists
// defaults to .env, returns "" if no file is present
func configFilePath(mode string, cfg *config) string {
fileName := packagePath(fmt.Sprintf(".%s.env", mode))
if !fileExists(fileName) {
fileName = packagePath(".env")
if !fileExists(fileName) {
return ""
}
}
return fileName
}
// Does this file exist?
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// outputs any notable settings to stdout
func printConfigInfo() {
// TODO
}