This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
222 lines (191 loc) · 7.2 KB
/
server.js
File metadata and controls
222 lines (191 loc) · 7.2 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require('appoptics-apm');
const cluster = require('cluster');
const express = require('express');
const session = require('express-session');
const pgSession = require('connect-pg-simple')(session);
if(cluster.isMaster) {
var cpuCount = require('os').cpus().length;
console.log('CpuFull', cpuCount);
cpuCount = Math.ceil(cpuCount / 2);
console.log('Cpu Count: ', cpuCount);
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.id + ' is online.');
});
cluster.on('exit', (worker, code, signal) => {
if (code !== 0 && !worker.exitedAfterDisconnect) {
console.log(`Worker ${worker.id} crashed. ` +
'Starting a new worker...');
cluster.fork();
}
});
} else {
const conn = require('./common/conn');
const staticFn = express['static'];
const xmlparser = require('express-xml-bodyparser');
const async = require('async');
const loc = require('./common/loc');
const logger = require('./common/Logger').applicationWideLogger;
const config = require('./config');
process.on('uncaughtException', function (err) {
logger.error("Caught exception: ", err);
});
const SymbologyToPostgreSqlMigration = require('./migration/SymbologyToPostgreSql');
const PgPool = require('./postgresql/PgPool');
const DatabaseSchema = require('./postgresql/DatabaseSchema');
const CreateDefaultUserAndGroup = require('./migration/CreateDefaultUserAndGroup');
const IdOfTheResourceMayBeText = require('./migration/IdOfTheResourceMayBeText');
const PrepareForInternalUser = require('./migration/PrepareForInternalUser');
const AddCustomInfoToWms = require('./migration/AddCustomInfoToWms');
const MigrateAwayFromGeonode = require('./migration/MigrateAwayFromGeonode');
const AddAuditInformation = require('./migration/AddAuditInformation');
const AddGetDatesToWmsLayers = require('./migration/AddGetDatesToWmsLayers');
const AddPhoneToUser = require('./migration/AddPhoneToUser');
const AddMetadataToLayer = require('./migration/2_10_1_AddMetadataToLayer');
const AddSourceUrlToLayer = require('./migration/2_12_AddSourceUrlToLayer');
const AddSession = require('./migration/2_14_AddSession');
const CompoundAuthentication = require('./security/CompoundAuthentication');
const PgAuthentication = require('./security/PgAuthentication');
const SsoAuthentication = require('./security/SsoAuthentication');
const pool = new PgPool({
user: config.pgDataUser,
database: config.pgDataDatabase,
password: config.pgDataPassword,
host: config.pgDataHost,
port: config.pgDataPort
});
let app;
function initServer(err) {
logger.info('server#initServer Initialize the server.');
if (err) {
console.log('Error: while initializing server: ', err);
return;
}
// Order is important
// Limit size of uploaded files
app.use(express.limit('2048mb'));
// Log the requests to see when the error occurs.
app.use(function(req, res, next) {
logger.info("Request: "+ req.method + " - " + req.url);
next();
});
app.use(express.cookieParser());
app.use(express.bodyParser({limit: '50mb', parameterLimit: 1000000}));
app.use(xmlparser());
app.use(session({
store: new pgSession({
pool: pool,
schemaName: 'data'
}),
secret: "panther",
resave: false,
saveUninitialized: false
}));
app.use(function (request, response, next) {
response.locals.ssid = request.cookies.sessionid;
response.locals.isAdmin = request.session.groups && request.session.groups.indexOf("admingroup") != -1;
next();
});
app.use(loc.langParser);
app.use(function(request, response, next){
if(request.session && !request.session.sldMap) {
request.session.sldMap = {};
}
if(request.session && !request.session.sldMapTemp) {
request.session.sldMapTemp = {};
}
if(request.session && !request.session.densityMap) {
request.session.densityMap = {};
}
if(request.session && !request.session.chartConfMap) {
request.session.chartConfMap = {};
}
if(request.session && !request.session.confMap) {
request.session.confMap = {};
}
next();
});
// Allow CORS on the node level.
app.use(function(req, res, next) {
// TODO: Fix security issues.
var url = req.headers.origin || 'http://localhost:63342';
res.header("Access-Control-Allow-Origin", url);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, access-control-allow-credentials, access-control-allow-origin, content-type, cookie");
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, DELETE");
next();
});
// End of allow CORS.
// Make sure that every request knows the current information about user.
app.use((request, response, next) => {
let authenticators = [new PgAuthentication(pool, config.postgreSqlSchema)];
if(config.toggles.useEoSso) {
authenticators.unshift(new SsoAuthentication(pool, config.postgreSqlSchema));
}
new CompoundAuthentication(authenticators).authenticate(request, response, next).then(() => {
logger.info('server#authentication User: ', request.session.user.id);
if(!request.session.user && config.toggles.loggedOnly) {
logger.info('server#authentication User not logged in');
response.redirect(config.notAuthenticatedUrl);
} else {
next();
}
}).catch(err => {
logger.error(`server#authentication Error: `, err);
if(config.toggles.loggedOnly) {
response.redirect(config.notAuthenticatedUrl);
}
})
});
require('./routes/routes')(app);
require('./routes/finish')(app);
app.use('/', staticFn(__dirname + '/public'));
app.use('/ipr', staticFn(__dirname + '/public/ipr'));
logger.info('Going to listen on port ' + config.localPort + '...');
let server = app.listen(config.localPort);
server.setTimeout(600000);
logger.info('Listening on port ' + config.localPort);
}
new DatabaseSchema(pool, config.postgreSqlSchema).create().then(function(){
return new SymbologyToPostgreSqlMigration(config.postgreSqlSchema).run();
}).then(()=>{
return new CreateDefaultUserAndGroup(config.postgreSqlSchema).run();
}).then(()=>{
return new IdOfTheResourceMayBeText(config.postgreSqlSchema).run();
}).then(()=>{
return new PrepareForInternalUser(config.postgreSqlSchema).run();
}).then(()=>{
return new AddCustomInfoToWms(config.postgreSqlSchema).run();
}).then(()=>{
return new MigrateAwayFromGeonode(config.postgreSqlSchema).run();
}).then(()=>{
return new AddAuditInformation(config.postgreSqlSchema).run();
}).then(()=>{
return new AddGetDatesToWmsLayers(config.postgreSqlSchema).run();
}).then(()=>{
return new AddPhoneToUser(config.postgreSqlSchema).run();
}).then(()=>{
return new AddMetadataToLayer(config.postgreSqlSchema).run();
}).then(()=>{
return new AddSourceUrlToLayer(config.postgreSqlSchema).run();
}).then(()=>{
return new AddSession(config.postgreSqlSchema).run();
}).then(function(){
logger.info('Finished Migrations.');
app = express();
async.series([
function(callback) {
conn.init(app,callback);
},
function(callback) {
loc.init(callback);
}],
initServer
);
}).catch((err) => {
logger.error('Error with Migration. Error: ', err);
});
}