3 const http
= require('http');
4 const DB
= require('./src/db');
5 const Logger
= require('./src/logger');
6 const Service
= require('./src/service');
7 const Config
= require('./config');
8 const { fileScope
} = require('./src/common');
9 const { version
} = require('./package.json');
11 const _scope
= fileScope(__filename
)('main');
13 const PORT
= process
.env
.PORT
|| 4001;
14 const ADDR
= process
.env
.LISTEN_ADDR
|| '127.0.0.1';
16 (async
function main () {
17 let config
, logger
, db
, service
;
19 config
= new Config(process
.env
.NODE_ENV
);
20 logger
= new Logger(config
);
21 db
= new DB(logger
, config
);
22 await db
.initialize();
23 service
= new Service(logger
, db
, config
);
25 http
.createServer((req
, res
) => {
26 service
.dispatch(req
, res
);
27 }).listen(PORT
, ADDR
, (err
) => {
29 logger
.error(_scope
, 'error creating server', err
);
32 logger
.info(_scope
, 'server started', { version
, listenAddress: ADDR
, listenPort: PORT
});
35 (logger
|| console
).error(_scope
, 'error starting server', e
);
36 db
&& db
.listener
&& await db
.listener
.stop();