Frames

Untitled

0
1const Logger = require('./utils/Logger');
2Logger.configure();
3Logger.setDefaultLevel('DEBUG');
4
5const logger = Logger.for('app');
6
7const express = require('express');
8const bodyParser = require('body-parser');
9const { Pool, Query } = require('pg');
10
11const submit = Query.prototype.submit;
12Query.prototype.submit = function () {
13 const text = this.text;
14 const values = this.values || [];
15 const query = text.replace(/\$([0-9]+)/g, (m, v) => JSON.stringify(values[parseInt(v) - 1]));
16 logger.debug('SQL query:', '\n\t' + query.split(/\n/).join('\n\t'));
17 submit.apply(this, arguments);
18};
19
20const Config = require('./config/Config');
21const DatabaseGateway = require('./app/core/DatabaseGateway');
22
23const pool = new Pool(Config.database);
24
25DatabaseGateway.setDefaultDriver(pool);
26
27const app = express();
28
29app.options('/*', (_, res) => {
30 res.header('Access-Control-Allow-Origin', '*');
31 res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
32 res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
33 res.send(200);
34});
35
36app.use(bodyParser.json());
37app.use(bodyParser.urlencoded({ extended: false }));
38
39app.use(require('./app/appRouter'));
40
41app.listen(process.env.PORT || 3001, () => {
42 logger.info('Application has been started!');
43});
44
45if (Config.application.isDevelopment) {
46 const path = require('path');
47 const filePath = path.join(__dirname, './routes.txt');
48
49 require('express-print-routes')(app, filePath);
50}
51
52process.on('SIGINT', () => process.exit(0));