-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
111 lines (92 loc) · 2.25 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
package main
import (
"fmt"
"os"
"strconv"
)
func loadEnvString(key string, result *string) {
s, ok := os.LookupEnv(key)
if !ok {
return
}
*result = s
}
func loadEnvUint(key string, result *uint) {
s, ok := os.LookupEnv(key)
if !ok {
return
}
n, err := strconv.Atoi(s)
if err != nil {
return
}
*result = uint(n)
}
/* Configuration */
/* PgSQL Configuration */
type pgSqlConfig struct {
Host string `json:"host"`
Port uint `json:"port"`
Database string `json:"database"`
SslMode string `json:"ssl_mode"`
User string `json:"user"`
Password string `json:"password"`
}
func (p pgSqlConfig) ConnStr() string {
return fmt.Sprintf("host=%s port=%d user=%s password=%s database=%s sslmode=%s", p.Host, p.Port, p.User, p.Password, p.Database, p.SslMode)
}
func defaultPgSql() pgSqlConfig {
return pgSqlConfig{
Host: "localhost",
Port: 5432,
Database: "database",
User: "",
Password: "",
SslMode: "disable",
}
}
func (p *pgSqlConfig) loadFromEnv(dbName string) {
loadEnvString("POSTGRES_HOST", &p.Host)
loadEnvUint("POSTGRES_PORT", &p.Port)
loadEnvString(dbName, &p.Database)
loadEnvString("POSTGRES_SSLMODE", &p.SslMode)
loadEnvString("POSTGRES_USERNAME", &p.User)
loadEnvString("POSTGRES_PASSWORD", &p.Password)
}
/* Listen Configuration */
type listenConfig struct {
Host string `json:"host"`
Port uint `json:"port"`
}
func (l listenConfig) Addr() string {
return fmt.Sprintf("%s:%d", l.Host, l.Port)
}
func defaultListenConfig() listenConfig {
return listenConfig{
Host: "127.0.0.1",
Port: 8080,
}
}
func (l *listenConfig) loadFromEnv() {
loadEnvString("LISTEN_HOST", &l.Host)
loadEnvUint("LISTEN_PORT", &l.Port)
}
type config struct {
Listen listenConfig `json:"listen"`
CrawlerDB pgSqlConfig `json:"crawler_db"`
BeneficialDB pgSqlConfig `json:"beneficial_db"`
BackendApiKey string `json:"api_key"`
ServerSalt string `json:"salt"`
}
func (c *config) loadFromEnv() {
c.Listen.loadFromEnv()
c.CrawlerDB.loadFromEnv("POSTGRES_CRAWLER_DB_NAME")
c.BeneficialDB.loadFromEnv("POSTGRES_BENEFICIAL_OWNERSHIP_DB_NAME")
}
func defaultConfig() config {
return config{
Listen: defaultListenConfig(),
CrawlerDB: defaultPgSql(),
BeneficialDB: defaultPgSql(),
}
}