/
context.go
243 lines (209 loc) · 4.91 KB
/
context.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package ctx
import (
"fmt"
"os"
"time"
"github.com/docker/go-units"
appctx "github.com/nixys/nxs-go-appctx/v3"
"github.com/nixys/nxs-support-bot/ds/primedb"
"github.com/nixys/nxs-support-bot/ds/redmine"
tgbot "github.com/nixys/nxs-support-bot/modules/bot"
"github.com/nixys/nxs-support-bot/modules/cache"
"github.com/nixys/nxs-support-bot/modules/issues"
"github.com/nixys/nxs-support-bot/modules/localization"
"github.com/nixys/nxs-support-bot/modules/task-handlers/rdmnhndlr"
"github.com/nixys/nxs-support-bot/modules/users"
"github.com/sirupsen/logrus"
)
// Ctx defines application custom context
type Ctx struct {
Cache cacheSettings
Bot *tgbot.Bot
API apiCtx
Rdmnhndlr rdmnhndlr.RdmnHndlr
Log *logrus.Logger
}
type apiCtx struct {
Bind string
TLS *apiTLSCtx
ClientMaxBodySizeBytes int64
RedmineSecretToken string
}
type apiTLSCtx struct {
CertFile string
KeyFie string
}
type cacheSettings struct {
C cache.Cache
TTL time.Duration
}
type feedbackSettings struct {
ProjectID int64
UserID int64
}
func AppCtxInit() (any, error) {
c := &Ctx{}
args, err := ArgsRead()
if err != nil {
return nil, err
}
conf, err := confRead(args.ConfigPath)
if err != nil {
tmpLogError("ctx init", err)
return nil, err
}
c.Log, err = logInit(conf.LogFile, conf.LogLevel)
if err != nil {
tmpLogError("ctx init", err)
return nil, err
}
// Connect to MySQL
primeDB, err := primedb.Connect(primedb.Settings{
Host: conf.MySQL.Host,
Port: conf.MySQL.Port,
Database: conf.MySQL.DB,
User: conf.MySQL.User,
Password: conf.MySQL.Password,
})
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
// Set redmine context
rdmn := redmine.Init(conf.Redmine.Host, conf.Redmine.Key)
redisHost := fmt.Sprintf("%s:%d", conf.Redis.Host, conf.Redis.Port)
// Set cache
c.Cache.C, err = cache.Init(cache.Settings{
Redmine: rdmn,
RedisHost: redisHost,
})
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
// Localization init
lb, err := localization.Init(conf.Localization.Path)
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
var feedback *feedbackSettings
if conf.Redmine.Feedback != nil {
proj, err := rdmn.ProjectGetByIdentifier(conf.Redmine.Feedback.ProjectIdentifier)
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
feedback = &feedbackSettings{
ProjectID: proj.ID,
UserID: conf.Redmine.Feedback.UserID,
}
}
iss := issues.Init(
issues.Settings{
DB: primeDB,
Redmine: rdmn,
Feedback: (*issues.FeedbackSettings)(feedback),
},
)
usrs := users.Init(
users.Settings{
DB: primeDB,
Cache: c.Cache.C,
Redmine: rdmn,
Feedback: (*users.FeedbackSettings)(feedback),
},
)
// Set bot context
c.Bot, err = tgbot.Init(tgbot.Settings{
APIToken: conf.Telegram.APIToken,
Log: c.Log,
Cache: c.Cache.C,
RedisHost: redisHost,
LangBundle: lb,
Issues: iss,
Users: usrs,
Feedback: (*tgbot.FeedbackSettings)(feedback),
})
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
c.Rdmnhndlr = rdmnhndlr.Init(
rdmnhndlr.Settings{
Bot: c.Bot,
LangBundle: lb,
Users: usrs,
Issues: iss,
Feedback: (*rdmnhndlr.FeedbackSettings)(feedback),
},
)
bts, err := units.RAMInBytes(conf.API.ClientMaxBodySize)
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
c.API = apiCtx{
Bind: conf.API.Bind,
TLS: func() *apiTLSCtx {
if conf.API.TLS == nil {
return nil
}
return &apiTLSCtx{
CertFile: conf.API.TLS.CertFile,
KeyFie: conf.API.TLS.KeyFie,
}
}(),
ClientMaxBodySizeBytes: bts,
RedmineSecretToken: conf.API.RedmineSecretToken,
}
c.Cache.TTL, err = time.ParseDuration(conf.Cache.TTL)
if err != nil {
c.Log.WithFields(logrus.Fields{
"details": err,
}).Errorf("ctx init")
return nil, err
}
return c, nil
}
func tmpLogError(msg string, err error) {
l, _ := appctx.DefaultLogInit(os.Stderr, logrus.InfoLevel, &logrus.JSONFormatter{})
l.WithFields(logrus.Fields{
"details": err,
}).Errorf(msg)
}
func logInit(file, level string) (*logrus.Logger, error) {
var (
f *os.File
err error
)
switch file {
case "stdout":
f = os.Stdout
case "stderr":
f = os.Stderr
default:
f, err = os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("log init: %w", err)
}
}
// Validate log level
l, err := logrus.ParseLevel(level)
if err != nil {
return nil, fmt.Errorf("log init: %w", err)
}
return appctx.DefaultLogInit(f, l, &logrus.JSONFormatter{})
}