update dependencies
[websub-hub] / server.js
1 'use strict';
2
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');
10
11 const _scope = fileScope(__filename)('main');
12
13 const PORT = process.env.PORT || 4001;
14 const ADDR = process.env.LISTEN_ADDR || '127.0.0.1';
15
16 (async function main () {
17 let config, logger, db, service;
18 try {
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);
24
25 http.createServer((req, res) => {
26 service.dispatch(req, res);
27 }).listen(PORT, ADDR, (err) => {
28 if (err) {
29 logger.error(_scope, 'error creating server', err);
30 throw err;
31 }
32 logger.info(_scope, 'server started', { version, listenAddress: ADDR, listenPort: PORT });
33 });
34 } catch (e) {
35 (logger || console).error(_scope, 'error starting server', e);
36 db && db.listener && await db.listener.stop();
37 }
38 })();