-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
157 lines (131 loc) · 3.93 KB
/
server.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const helmet = require("helmet");
const express = require("express");
const https = require("https");
const http = require("http");
const compression = require("compression");
const path = require("path");
const fs = require("fs");
const forceSSL = require("express-force-ssl");
const pckg = require("./package.json");
const rateLimit = require("express-rate-limit");
// System config loading
const properties = require("./config/server/server-config.json");
const httpPort = properties.port.http;
const httpsPortP = properties.port.https;
const key = properties.path.key;
const cert = properties.path.cert;
const maxAge = properties.max_age;
const windowMs = properties.rate_limit.windowMs;
const max = properties.rate_limit.max;
const APIUrl = process.env.REACT_APP_LOIDE_API_SERVER
? "http://".concat(process.env.REACT_APP_LOIDE_API_SERVER)
: "http://localhost:8084";
// This function validates the JSON schemas
var jpointer = require("json-pointer");
var Ajv = require("ajv");
validateJsonSchemas();
const app = express();
const server = http.createServer(app);
var enableHTTPS = false;
if (key.length !== 0 && cert.length !== 0) {
enableHTTPS = true;
let options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
};
// Enable redirect from HTTP to HTTPS
app.use(forceSSL);
app.set("forceSSLOptions", {
httpsPort: httpsPortP,
});
var secureServer = https.createServer(options, app);
}
app.use(
helmet({
hsts: {
maxAge: maxAge,
},
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
connectSrc: ["'self'", APIUrl, "https://is.gd"],
},
},
}),
);
app.use(compression());
app.use(
rateLimit({
windowMs: windowMs,
max: max,
}),
);
app.use(express.static(path.join(__dirname, "build")));
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
if (enableHTTPS) {
secureServer.listen(httpsPortP, function () {
print_log("LoIDE-PWA listening on secure port " + httpsPortP);
print_log("Version: " + pckg.version);
});
}
server.listen(httpPort, function () {
print_log("LoIDE-PWA listening on port " + httpPort);
print_log("Version: " + pckg.version);
});
function print_log(statement) {
console.log("%s: %s", new Date().toLocaleString(), statement);
}
function validateJsonSchemas() {
// Validate JSON file with the relative scheme
var servicesValidation = validateSchema(
"./config/server/server-config.json",
"./config/server/server-config-schema.json",
);
if (servicesValidation.criticalError) {
console.log("Fatal error: configuration files are not setted up properly!");
process.exit(1);
}
}
function validateSchema(jsonPath, schemaPath) {
// Loading files
var json = require(jsonPath);
var schema = require(schemaPath);
// Config
var ajv = new Ajv({
allErrors: true,
});
// Compiling the schema
var compiledSchema = ajv.compile(schema);
var validated = false;
var printError = true;
var response = {};
while (!validated) {
// Validating
var validatedJson = compiledSchema(json);
// If some there is some error, the nearest parent object in the file, containing this error, is deleted
if (!validatedJson) {
// Prints the errors only the first time
if (printError) {
console.log(compiledSchema.errors);
printError = false;
}
for (var index in compiledSchema.errors) {
var path = compiledSchema.errors[index].dataPath;
if (path === "") {
// 'This' case happen when there is a problem in to the root of the json file (eg. when the file is empty)
console.log("Fatal error: " + jsonPath + " is not setted up properly!");
response.criticalError = true;
validated = true;
} else {
jpointer.remove(json, path);
}
}
} else {
console.log("Validated: " + jsonPath);
validated = true;
}
}
return response;
}