-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
134 lines (121 loc) · 3.91 KB
/
index.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
126
127
128
129
130
131
132
133
134
/*
* @author Martin Høgh <mh@mapcentia.com>
* @copyright 2013-2019 MapCentia ApS
* @license http://www.gnu.org/licenses/#AGPL GNU AFFERO GENERAL PUBLIC LICENSE 3
*/
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
let path = require('path');
require('dotenv').config({path: path.join(__dirname, ".env")});
let express = require('express');
let http = require('http');
let cluster = require('cluster');
let sticky = require('sticky-session');
let compression = require('compression');
let bodyParser = require('body-parser');
let cookieParser = require('cookie-parser');
let session = require('express-session');
let cors = require('cors');
let config = require('./config/config.js');
let store;
let app = express();
const MAXAGE = (config.sessionMaxAge || 86400) * 1000;
if (!config?.gc2?.host) {
if (!config?.gc2) {
config.gc2 = {};
}
config.gc2.host = process.env.GC2_HOST;
}
if (!config?.gc2?.host) {
console.error("No GC2 host set. Set it through the environment variable GC2_HOST or in config/config.js");
process.exit(0)
}
app.use(compression());
app.use(cors());
app.use(cookieParser());
app.use(bodyParser.json({
limit: '50mb'
})
);
app.use(bodyParser.text({
limit: '50mb'
})
);
// to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb'
}));
app.set('trust proxy', 1); // trust first proxy
if (typeof config?.redis?.host === "string") {
let redis = require("redis");
let redisStore = require('connect-redis')(session);
let client = redis.createClient({
host: config.redis.host.split(":")[0],
port: config.redis.host.split(":")[1] || 6379,
db: config?.redis?.db || 3,
retry_strategy: function (options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
return undefined;
}
return Math.min(options.attempt * 100, 3000);
}
});
store = new redisStore({
client: client,
ttl: MAXAGE
});
} else {
let fileStore = require('session-file-store')(session);
store = new fileStore({
path: "/tmp/sessions",
ttl: MAXAGE
});
}
let sess = {
store: store,
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
name: "connect.gc2",
cookie: {secure: false, httpOnly: false, maxAge: MAXAGE},
};
if (app.get('env') === 'production') {
app.set('trust proxy', 1) // trust first proxy
sess.cookie.secure = true
sess.cookie.sameSite = 'none'
}
app.use(session(sess));
app.use('/app/:db/:schema?', express.static(path.join(__dirname, 'public'), {maxage: '60s'}));
if (config.staticRoutes) {
for (let key in config.staticRoutes) {
if (config.staticRoutes.hasOwnProperty(key)) {
console.log(key + " -> " + config.staticRoutes[key]);
app.use('/app/:db/:schema/' + key, express.static(path.join(__dirname, config.staticRoutes[key]), {maxage: '60s'}));
}
}
}
app.use('/', express.static(path.join(__dirname, 'public'), {maxage: '100d'}));
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules'), {maxage: '100d'}));
app.use(require('./controllers'));
app.use(require('./extensions'));
app.enable('trust proxy');
const port = process.env.PORT ? process.env.PORT : 3000;
const server = http.createServer(app);
if (!sticky.listen(server, port, {})) {
// Master code
server.once('listening', function () {
console.log(`server started on port ${port}`);
});
} else {
console.log('worker: ' + cluster.worker.id);
}
global.io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log(socket.id);
});