@ladjs/web 11.1.2 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +13 -17
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -66,7 +66,7 @@ const reportUri = isSANB(process.env.WEB_URL)
|
|
|
66
66
|
|
|
67
67
|
class Web {
|
|
68
68
|
// eslint-disable-next-line complexity
|
|
69
|
-
constructor(config) {
|
|
69
|
+
constructor(config, client) {
|
|
70
70
|
this.config = {
|
|
71
71
|
...sharedConfig('WEB'),
|
|
72
72
|
meta: {},
|
|
@@ -170,9 +170,17 @@ class Web {
|
|
|
170
170
|
...this.config.cabin
|
|
171
171
|
});
|
|
172
172
|
|
|
173
|
+
// initialize redis
|
|
174
|
+
this.client = client
|
|
175
|
+
? client
|
|
176
|
+
: new Redis(this.config.redis, cabin, this.config.redisMonitor);
|
|
177
|
+
|
|
173
178
|
// initialize the app
|
|
174
179
|
const app = new Koa();
|
|
175
180
|
|
|
181
|
+
// allow middleware to access redis client
|
|
182
|
+
app.context.client = this.client;
|
|
183
|
+
|
|
176
184
|
// listen for error and log events emitted by app
|
|
177
185
|
app.on('error', (err, ctx) => {
|
|
178
186
|
const level = err.status && err.status < 500 ? 'warn' : 'error';
|
|
@@ -181,16 +189,6 @@ class Web {
|
|
|
181
189
|
});
|
|
182
190
|
app.on('log', cabin.log);
|
|
183
191
|
|
|
184
|
-
// initialize redis
|
|
185
|
-
const client = new Redis(
|
|
186
|
-
this.config.redis,
|
|
187
|
-
cabin,
|
|
188
|
-
this.config.redisMonitor
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
// allow middleware to access redis client
|
|
192
|
-
app.context.client = client;
|
|
193
|
-
|
|
194
192
|
// override koa's undocumented error handler
|
|
195
193
|
app.context.onerror = errorHandler(this.config.cookiesKey, cabin);
|
|
196
194
|
|
|
@@ -240,7 +238,7 @@ class Web {
|
|
|
240
238
|
|
|
241
239
|
return ratelimit({
|
|
242
240
|
...this.config.rateLimit,
|
|
243
|
-
db: client
|
|
241
|
+
db: this.client
|
|
244
242
|
})(ctx, next);
|
|
245
243
|
});
|
|
246
244
|
}
|
|
@@ -324,7 +322,7 @@ class Web {
|
|
|
324
322
|
app.keys = this.config.sessionKeys;
|
|
325
323
|
app.use(
|
|
326
324
|
session({
|
|
327
|
-
store: redisStore({ client }),
|
|
325
|
+
store: redisStore({ client: this.client }),
|
|
328
326
|
key: this.config.cookiesKey,
|
|
329
327
|
cookie: this.config.cookies,
|
|
330
328
|
genSid: this.config.genSid,
|
|
@@ -413,15 +411,13 @@ class Web {
|
|
|
413
411
|
}
|
|
414
412
|
|
|
415
413
|
// start server on either http or http2
|
|
416
|
-
|
|
414
|
+
this.server =
|
|
417
415
|
this.config.protocol === 'https'
|
|
418
416
|
? http2.createSecureServer(this.config.ssl, app.callback())
|
|
419
417
|
: http.createServer(app.callback());
|
|
420
418
|
|
|
421
|
-
// expose app
|
|
419
|
+
// expose the app
|
|
422
420
|
this.app = app;
|
|
423
|
-
this.server = server;
|
|
424
|
-
this.client = client;
|
|
425
421
|
|
|
426
422
|
// bind listen/close to this
|
|
427
423
|
this.listen = this.listen.bind(this);
|