-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (96 loc) · 3.12 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
122
123
124
125
// Fix mem leak.
require('events').EventEmitter.defaultMaxListeners = 0;
// Load env vars
const { NODE_ENV, } = process.env;
if (NODE_ENV !== 'production') {
// eslint-disable-next-line global-require
require('./creds/env');
}
// Dependencies
const express = require('express');
const compression = require('compression');
const exphbs = require('express-handlebars');
const path = require('path');
const { routeCheck, } = require('express-suite');
// Winston Logger
const appLog = require('./config/system/logs').get('appLog');
// Custom modules
const { sentimentProccess, ResetProccess, } = require('./config/analysis/sentiment');
// Redis
const { setAsync, } = require('./config/database/redisConnection');
setAsync('csv', `${'\n'}text,location,textHuman`)
.then(() => appLog.info('csv key initialized'))
.catch(err => appLog.error(err));
// Global Constant
const PORT = process.env.PORT || 3005;
// Initialize the App
const app = express();
// Compression Module
app.use(compression({ level: 9, memLevel: 9, }));
// Disable etag
app.set('etag', false);
app.set('x-powered-by', false);
// BodyParser Middleware
app.use(express.urlencoded({
extended: true,
limit: '5mb',
}));
app.use(express.json({
limit: '5mb',
extended: true,
}));
// Set Static Folder (Absolute)
app.use('/', express.static(path.join(__dirname, '/assets')));
// Handlebars Middleware
app.engine('handlebars', exphbs({
defaultLayout: 'main',
}));
// Set view engine
app.set('view engine', 'handlebars');
app.engine('handlebars', exphbs({
defaultLayout: 'main',
}));
// Express config
app.all('*', (req, res, next) => {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', 'false');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET');
// No cache
// res.setHeader('Cache-Control', 'max-age=120,private');
res.setHeader('Cache-Control', 'no-cache');
next();
});
// Start the app with socket io;
const io = require('socket.io')(app.listen(PORT, () => {
appLog.info(`Server is running in ${NODE_ENV} mode on port ${PORT}`);
}), {
allowUpgrades: true,
transports: ['websocket'],
});
// Run the model every 13 sec
setInterval(async () => {
await sentimentProccess();
}, 13000);
// Export socket io Server before the route so it's loaded when used in the routes
module.exports = { io, };
// Load Routes
const tweet = require('./routes/tweet');
// Use Routes
app.use('/', tweet);
// Route Check
app.use(routeCheck(app));
// Clear redis key every 5min
setInterval(async () => {
await ResetProccess();
}, 60000 * 10);
// Handle SIGINT from terminal
process.on('SIGINT', () => process.exit(0));
// Handle SIGUP from nodemon
process.on('SIGUP', () => process.exit(0));