This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
/
config.go
158 lines (136 loc) · 3.86 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
package config
import (
"os"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/rl404/mal-db/internal/errors"
)
// List of available cache type.
const (
NoCache = "nocache"
InMemory = "inmemory"
Redis = "redis"
Memcache = "memcache"
)
// List of available pubsub type.
const (
NSQ = "nsq"
RabbitMQ = "rabbitmq"
)
var caches = map[string]int{NoCache: 1, InMemory: 1, Redis: 1, Memcache: 1}
var pubsubs = map[string]int{Redis: 1, NSQ: 1, RabbitMQ: 1}
// Config is configuration model for whole malscraper project.
type Config struct {
// Web server config.
Web webConfig `envconfig:"WEB"`
// Worker config.
Worker workerConfig `envconfig:"WORKER"`
// Cache config.
Cache cacheConfig `envconfig:"CACHE"`
// Database config.
DB dbConfig `envconfig:"DB"`
// Logging config.
Log logConfig `envconfig:"LOG"`
// Elasticsearch config.
ES esConfig `envconfig:"ES"`
// PubSub config.
PubSub pubSubConfig `envconfig:"PUBSUB"`
}
type webConfig struct {
// HTTP port.
Port string `envconfig:"PORT" default:"8006"`
// Read timeout (in seconds).
ReadTimeout int `envconfig:"READ_TIMEOUT" default:"5"`
// Write timeout (in seconds).
WriteTimeout int `envconfig:"WRITE_TIMEOUT" default:"5"`
// Graceful shutdown timeout (in seconds).
GracefulTimeout int `envconfig:"GRACEFUL_TIMEOUT" default:"10"`
}
type workerConfig struct {
// Old data age limit. Recent parsed data will not be re-parsed.
AgeLimit int `envconfig:"AGE_LIMIT" default:"604800"`
// Break time between parsing.
BreakTime int `envconfig:"BREAK_TIME" default:"5"`
}
type cacheConfig struct {
// Type of caching (string).
Dialect string `envconfig:"DIALECT" default:"inmemory"`
// Cache address with format `host:port`.
Address string `envconfig:"ADDRESS"`
// Cache password if exists.
Password string `envconfig:"PASSWORD"`
// Caching time duration (in seconds).
Time int `envconfig:"TIME" default:"86400"`
}
type logConfig struct {
// Log level.
Level int `envconfig:"LEVEL" default:"4"`
// Log color.
Color bool `envconfig:"COLOR" default:"true"`
}
type dbConfig struct {
// Database host and port.
Address string `envconfig:"ADDRESS"`
// Database name.
Name string `envconfig:"NAME"`
// Database user.
User string `envconfig:"USER"`
// Database password.
Password string `envconfig:"PASSWORD"`
// Max open connection.
MaxConnOpen int `envconfig:"MAX_CONN_OPEN" default:"10"`
// Max idle connection.
MaxConnIdle int `envconfig:"MAX_CONN_IDLE" default:"10"`
// Max connection lifetime.
MaxConnLifetime int `envconfig:"MAX_CONN_LIFETIME" default:"60"`
}
type esConfig struct {
// Elasticsearch addresses. Split by comma.
Address string `envconfig:"ADDRESS"`
// Elasticsearch username.
User string `envconfig:"USER"`
// Elasticsearch password.
Password string `envconfig:"PASSWORD"`
}
type pubSubConfig struct {
// Type of pubsub.
Dialect string `envconfig:"DIALECT"`
// Pubsub address.
Address string `envconfig:"ADDRESS"`
// Pubsub user if exists.
User string `envconfig:"USER"`
// Pubsub password if exists.
Password string `envconfig:"PASSWORD"`
}
const envPath = "../../.env"
const envPrefix = "MAL"
// GetConfig to read and parse config from `.env`.
func GetConfig() (cfg Config, err error) {
// Load .env file.
_ = godotenv.Load(envPath)
// Convert env to struct.
if err = envconfig.Process(envPrefix, &cfg); err != nil {
return cfg, err
}
// Override port.
if port := os.Getenv("PORT"); port != "" {
cfg.Web.Port = port
}
// Validate cache type.
if caches[cfg.Cache.Dialect] == 0 {
return cfg, errors.ErrInvalidCacheType
}
// Require database config.
if cfg.DB.Address == "" {
return cfg, errors.ErrRequiredDB
}
// Validate pubsub type.
if pubsubs[cfg.PubSub.Dialect] == 0 {
return cfg, errors.ErrInvalidPubsubType
}
// Require pubsub config.
if cfg.PubSub.Address == "" {
return cfg, errors.ErrRequiredPubsub
}
return cfg, nil
}