-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·121 lines (103 loc) · 4.13 KB
/
app.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').config();
const { Console } = require('console');
const express = require('express'),
load = require('express-load'),
util = require('util'),
compression = require('compression'),
requestTimeout = require('express-timeout'),
responseTime = require('response-time'),
bodyParser = require('body-parser'),
parseCookie = require('cookie-parser'),
requestParam = require('request-param'),
morgan = require('morgan'),
cors = require('cors');
const app = express();
const http = require('http').Server(app);
const cookie = parseCookie('LAPIG-JOBS')
load('config.js', {'verbose': false})
.then('utils')
.then('database')
.then('mailer')
.then('middleware')
.into(app);
const allowedOrigins = [
'http://localhost:4200',
'https://atlasdaspastagens.ufg.br',
'https://covidgoias.ufg.br',
'https://maps.lapig.iesa.ufg.br',
'https://cepf.lapig.iesa.ufg.br',
'https://araticum.lapig.iesa.ufg.br',
'http://localhost:3000'
];
const corsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.includes(origin) || !origin) {
callback(null, true);
} else {
callback(new Error('Origin not allowed by CORS on LAPIG-JOBS'));
}
},
};
app.options('*', cors(corsOptions));
app.all('*', cors(corsOptions));
app.database.client.init(function () {
app.middleware.repository.init(() => {
app.database.client.init_general(() => {
app.mailer.transporter.verify((error, success) => {
if (error) {
app.utils.logger.error('Timeout: ' + error)
console.error(error);
} else {
app.use(cookie);
app.use(compression());
app.use(requestTimeout({
'timeout': 2000 * 60 * 30 * 24,
'callback': function (err, options) {
let response = options.res;
if (err) {
app.utils.logger.error('Timeout: ' + err)
console.log('Timeout: ' + err.stack);
}
response.end();
}
}));
app.use(responseTime());
app.use(requestParam());
app.use(morgan('tiny'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({limit: '1gb'}));
app.use(function (error, request, response, next) {
console.log('ServerError: ', error.stack);
app.utils.logger.error('ServerError: ', error.stack)
next();
});
load('controllers')
.then('routes')
.then('jobs')
.into(app);
const analysisAtlas = app.jobs.analysisAtlas;
const emailContact = app.jobs.emailContact;
const httpServer = http.listen(app.config.port, function () {
app.utils.logger.error('Start Lapig Jobs')
app.utils.logger.info(`LAPIG-JOBS Server @ [port ${app.config.port}] [pid ${process.pid.toString()}]` );
if(success){
app.utils.logger.info('Mailer is ready to send messages');
}
emailContact.start();
analysisAtlas.start();
});
[`exit`, `uncaughtException`].forEach((event) => {
if (event === 'uncaughtException') {
process.on(event, (e) => {
})
} else {
process.on(event, (e) => {
httpServer.close(() => process.exit())
})
}
})
}
});
});
});
})