@ladjs/api 7.1.2 → 8.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 +12 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -29,7 +29,7 @@ const { boolean } = require('boolean');
|
|
|
29
29
|
const { ratelimit } = require('koa-simple-ratelimit');
|
|
30
30
|
|
|
31
31
|
class API {
|
|
32
|
-
constructor(config) {
|
|
32
|
+
constructor(config, client) {
|
|
33
33
|
this.config = {
|
|
34
34
|
...sharedConfig('API'),
|
|
35
35
|
rateLimitIgnoredGlobs: [],
|
|
@@ -41,9 +41,17 @@ class API {
|
|
|
41
41
|
...this.config.cabin
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
+
// initialize redis
|
|
45
|
+
this.client = client
|
|
46
|
+
? client
|
|
47
|
+
: new Redis(this.config.redis, cabin, this.config.redisMonitor);
|
|
48
|
+
|
|
44
49
|
// initialize the app
|
|
45
50
|
const app = new Koa();
|
|
46
51
|
|
|
52
|
+
// allow middleware to access redis client
|
|
53
|
+
app.context.client = this.client;
|
|
54
|
+
|
|
47
55
|
// listen for error and log events emitted by app
|
|
48
56
|
app.on('error', (err, ctx) => {
|
|
49
57
|
const level = err.status && err.status < 500 ? 'warn' : 'error';
|
|
@@ -52,16 +60,6 @@ class API {
|
|
|
52
60
|
});
|
|
53
61
|
app.on('log', cabin.log);
|
|
54
62
|
|
|
55
|
-
// initialize redis
|
|
56
|
-
const client = new Redis(
|
|
57
|
-
this.config.redis,
|
|
58
|
-
cabin,
|
|
59
|
-
this.config.redisMonitor
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
// allow middleware to access redis client
|
|
63
|
-
app.context.client = client;
|
|
64
|
-
|
|
65
63
|
// override koa's undocumented error handler
|
|
66
64
|
app.context.onerror = errorHandler(false, cabin);
|
|
67
65
|
|
|
@@ -111,7 +109,7 @@ class API {
|
|
|
111
109
|
|
|
112
110
|
return ratelimit({
|
|
113
111
|
...this.config.rateLimit,
|
|
114
|
-
db: client
|
|
112
|
+
db: this.client
|
|
115
113
|
})(ctx, next);
|
|
116
114
|
});
|
|
117
115
|
}
|
|
@@ -169,15 +167,13 @@ class API {
|
|
|
169
167
|
}
|
|
170
168
|
|
|
171
169
|
// start server on either http or https
|
|
172
|
-
|
|
170
|
+
this.server =
|
|
173
171
|
this.config.protocol === 'https'
|
|
174
172
|
? https.createServer(this.config.ssl, app.callback())
|
|
175
173
|
: http.createServer(app.callback());
|
|
176
174
|
|
|
177
|
-
// expose app
|
|
175
|
+
// expose the app
|
|
178
176
|
this.app = app;
|
|
179
|
-
this.server = server;
|
|
180
|
-
this.client = client;
|
|
181
177
|
|
|
182
178
|
// bind listen/close to this
|
|
183
179
|
this.listen = this.listen.bind(this);
|