-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.ts
More file actions
41 lines (37 loc) · 1.53 KB
/
server.ts
File metadata and controls
41 lines (37 loc) · 1.53 KB
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
import type { Server } from 'http';
import app from './app';
import DBConnection from './db/Connection';
import { init as initWidgetsHandler } from './handler/widgets/WidgetsHandler';
import { init as initTemplatesHandler } from './handler/templates';
import { getLogger } from './handler/LoggerHandler';
import { isDevelopmentOrTest } from './utils';
import { getMode, init } from './serverSettings';
import { getBackendConfig } from './config';
const logger = getLogger('Server');
init();
export default DBConnection.init()
.then(() => {
logger.info('DB connection initialized successfully.');
return Promise.all([initWidgetsHandler(), initTemplatesHandler()]);
})
.then(() => {
logger.info('Widgets and templates data initialized successfully.');
return new Promise((resolve, reject) => {
const { host, port } = getBackendConfig();
const server = host ? app.listen(port, host) : app.listen(port);
server.on('error', reject);
server.on('listening', () => {
logger.info(`Server started in mode ${getMode()}`);
if (isDevelopmentOrTest) {
logger.info('Server started for development');
}
logger.info(`Stage runs on ${host}:${port}!`);
resolve(server);
});
});
})
.catch(error => {
logger.error(`Server initialization failed`, error);
// eslint-disable-next-line no-process-exit
process.exit(1);
}) as Promise<Server>;