forked from requilence/integram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
104 lines (85 loc) · 3.58 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
package integram
import (
"fmt"
"github.com/kelseyhightower/envconfig"
"github.com/requilence/url"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
type Mode string
const (
InstanceModeMultiProcessMain Mode = "multi-main" // run only the main worker. It will process the outgoing messages queue, route the incoming webhooks to specific services and resolve webpreviews
InstanceModeMultiProcessService Mode = "multi-service" // run only one the registred services and their workers. Main instance must be running in order to the outgoing TG messages could be sent
InstanceModeSingleProcess Mode = "single" // run all-in-one process – main worker and all registred services
)
type BotConfig struct {
Token string `envconfig:"BOT_TOKEN" required:"true"`
}
type config struct {
BaseURL string `envconfig:"INTEGRAM_BASE_URL" required:"true"`
InstanceMode Mode `envconfig:"INTEGRAM_INSTANCE_MODE" default:"single"` // please refer to the constants declaration
TGPoolBatchSize int `envconfig:"INTEGRAM_TG_POOL_BATCH_SIZE" default:"100"` // Number of jobs fetching from Redis at once
// service webhook ratelimiter
RateLimitMemstore int `envconfig:"INTEGRAM_RATELIMIT_MEMSTORE_SIZE" default:"0"` // max number of keys to store. set 0 to disable ratelimiter
RateLimitPerMinute int `envconfig:"INTEGRAM_RATELIMIT_PER_MINUTE" default:"30"` // max number of incoming requests per minute for user/chat
RateLimitBurst int `envconfig:"INTEGRAM_RATELIMIT_BURST" default:"10"` // max number of requests in a row
TGPool int `envconfig:"INTEGRAM_TG_POOL" default:"10"` // Maximum simultaneously message sending
MongoURL string `envconfig:"INTEGRAM_MONGO_URL" default:"mongodb://localhost:27017/integram"`
RedisURL string `envconfig:"INTEGRAM_REDIS_URL" default:"127.0.0.1:6379"`
Port string `envconfig:"INTEGRAM_PORT" default:"7000"`
Debug bool `envconfig:"INTEGRAM_DEBUG" default:"1"`
MongoLogging bool `envconfig:"INTEGRAM_MONGO_LOGGING" default:"0"`
MongoStatistic bool `envconfig:"INTEGRAM_MONGO_STATISTIC" default:"0"`
ConfigDir string `envconfig:"INTEGRAM_CONFIG_DIR" default:"./.conf"` // default is $GOPATH/.conf
// -----
// only make sense for InstanceModeMultiProcessService
HealthcheckIntervalInSecond int `envconfig:"INTEGRAM_HEALTHCHECK_INTERVAL" default:"30"` // interval to ping each service instance by the main instance
StandAloneServiceURL string `envconfig:"INTEGRAM_STANDALONE_SERVICE_URL"` // default will be depending on the each service's name, e.g. http://trello:7000
}
var Config config
func (c *config) IsMainInstance() bool {
if c.InstanceMode == InstanceModeMultiProcessMain {
return true
}
return false
}
func (c *config) ParseBaseURL() *url.URL {
u, err := url.Parse(c.BaseURL)
if err != nil {
panic("PANIC: can't parse INTEGRAM_BASE_URL: '" + c.BaseURL + "'")
}
if u.Scheme == "" {
u.Scheme = "https"
}
return u
}
func (c *config) IsStandAloneServiceInstance() bool {
if c.InstanceMode == InstanceModeMultiProcessService {
return true
}
return false
}
func (c *config) IsSingleProcessInstance() bool {
if c.InstanceMode == InstanceModeSingleProcess {
return true
}
return false
}
func init() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
go func() {
for sig := range c {
println(sig)
fmt.Println("Got A HUP Signal: reloading config from ENV")
err := envconfig.Process("", &Config)
if err != nil {
log.WithError(err).Error("HUP envconfig error")
}
}
}()
envconfig.MustProcess("", &Config)
Config.ParseBaseURL()
}