-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.js
42 lines (35 loc) · 1.04 KB
/
api.js
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
// eslint-disable-next-line import/no-unassigned-import
require('./config/env');
const API = require('@ladjs/api');
const Graceful = require('@ladjs/graceful');
const Mongoose = require('@ladjs/mongoose');
const ip = require('ip');
const logger = require('./helpers/logger');
const apiConfig = require('./config/api');
const api = new API(apiConfig);
if (!module.parent) {
const mongoose = new Mongoose({ ...api.config.mongoose, logger });
const graceful = new Graceful({
mongooses: [mongoose],
servers: [api],
redisClients: [api.client],
logger
});
graceful.listen();
(async () => {
try {
await api.listen(api.config.port);
if (process.send) process.send('ready');
const { port } = api.server.address();
logger.info(
`Lad API server listening on ${port} (LAN: ${ip.address()}:${port})`
);
await mongoose.connect();
} catch (err) {
logger.error(err);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
})();
}
module.exports = api;