-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
182 lines (149 loc) · 4.18 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package app
import (
"strings"
"github.com/caarlos0/env/v10"
"github.com/pkg/errors"
"github.com/dashotv/fae"
)
func setupConfig(app *Application) error {
app.Config = &Config{}
if err := env.Parse(app.Config); err != nil {
return errors.Wrap(err, "parsing config")
}
if err := app.Config.Validate(); err != nil {
return errors.Wrap(err, "failed to validate config")
}
return nil
}
type Config struct {
Mode string `env:"MODE" envDefault:"dev"`
Logger string `env:"LOGGER" envDefault:"dev"`
Port int `env:"PORT" envDefault:"10080"`
//golem:template:app/config_partial_struct
// DO NOT EDIT. This section is managed by github.com/dashotv/golem.
// Models (Database)
Connections ConnectionSet `env:"CONNECTIONS"`
// Router Auth
Auth bool `env:"AUTH" envDefault:"false"`
ClerkSecretKey string `env:"CLERK_SECRET_KEY"`
ClerkToken string `env:"CLERK_TOKEN"`
// Events
NatsURL string `env:"NATS_URL"`
// Workers
MinionConcurrency int `env:"MINION_CONCURRENCY" envDefault:"10"`
MinionDebug bool `env:"MINION_DEBUG" envDefault:"false"`
MinionBufferSize int `env:"MINION_BUFFER_SIZE" envDefault:"100"`
MinionURI string `env:"MINION_URI"`
MinionDatabase string `env:"MINION_DATABASE"`
MinionCollection string `env:"MINION_COLLECTION"`
//golem:template:app/config_partial_struct
JackettURL string `env:"JACKETT_URL"`
JackettKey string `env:"JACKETT_KEY"`
NZBGeekURL string `env:"NZBGEEK_URL"`
NZBGeekKey string `env:"NZBGEEK_KEY"`
RiftURL string `env:"RIFT_URL"`
Words []string `env:"WORDS"`
}
func (c *Config) Validate() error {
list := []func() error{
c.validateMode,
c.validateLogger,
//golem:template:app/config_partial_validate
// DO NOT EDIT. This section is managed by github.com/dashotv/golem.
c.validateDefaultConnection,
//golem:template:app/config_partial_validate
}
for _, fn := range list {
if err := fn(); err != nil {
return err
}
}
return nil
}
func (c *Config) validateMode() error {
switch c.Mode {
case "dev", "release":
return nil
default:
return errors.New("invalid mode (must be 'dev' or 'release')")
}
}
func (c *Config) validateLogger() error {
switch c.Logger {
case "dev", "release":
return nil
default:
return errors.New("invalid logger (must be 'dev' or 'release')")
}
}
//golem:template:app/config_partial_connection
// DO NOT EDIT. This section is managed by github.com/dashotv/golem.
func (c *Config) validateDefaultConnection() error {
if len(c.Connections) == 0 {
return fae.New("you must specify a default connection")
}
var def *Connection
for n, c := range c.Connections {
if n == "default" || n == "Default" {
def = c
break
}
}
if def == nil {
return fae.New("no 'default' found in connections list")
}
if def.Database == "" {
return fae.New("default connection must specify database")
}
if def.URI == "" {
return fae.New("default connection must specify URI")
}
return nil
}
type Connection struct {
URI string `yaml:"uri,omitempty"`
Database string `yaml:"database,omitempty"`
Collection string `yaml:"collection,omitempty"`
}
func (c *Connection) UnmarshalText(text []byte) error {
vals := strings.Split(string(text), ",")
c.URI = vals[0]
c.Database = vals[1]
c.Collection = vals[2]
return nil
}
type ConnectionSet map[string]*Connection
func (c *ConnectionSet) UnmarshalText(text []byte) error {
*c = make(map[string]*Connection)
for _, conn := range strings.Split(string(text), ";") {
kv := strings.Split(conn, "=")
vals := strings.Split(kv[1]+",,", ",")
(*c)[kv[0]] = &Connection{
URI: vals[0],
Database: vals[1],
Collection: vals[2],
}
}
return nil
}
func (c *Config) ConnectionFor(name string) (*Connection, error) {
def, ok := c.Connections["default"]
if !ok {
return nil, fae.Errorf("connection for %s: no default connection found", name)
}
conn, ok := c.Connections[name]
if !ok {
return nil, fae.Errorf("no connection named '%s'", name)
}
if conn.URI == "" {
conn.URI = def.URI
}
if conn.Database == "" {
conn.Database = def.Database
}
if conn.Collection == "" {
conn.Collection = def.Collection
}
return conn, nil
}
//golem:template:app/config_partial_connection