This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-node.js
78 lines (69 loc) · 2.08 KB
/
server-node.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
const config = require('./config-node.js');
const path = require('path');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const server = require('fastify')({
logger: config.logger
});
function App() {
server.register(require('fastify-file-upload'), {
limits: { fileSize: config.limitSize },
useTempFiles : true,
tempFileDir : 'temp/'
});
server.register(require('fastify-static'), {
root: path.join(__dirname, config.staticDirPath),
prefix: '/'+config.staticDirPath+'/',
acceptRanges:config.acceptRanges,
cacheControl: config.cacheControl,
dotfiles:config.dotfiles,
etag:config.etag,
extensions:config.extensions,
immutable: config.immutable,
lastModified:config.lastModified,
maxAge: config.maxAge,
wildcard: false,
serve:false
});
// Route
server.register(require('./routes/default.js'));
server.register(require('./routes/node.js'));
// Hooks
server.addHook('onRequest', (request, reply, done) => {
if(request.raw.method !== 'GET') {
if(request.headers.x_token===config.x_token){
done();
} else {
reply.code(400).send({status:400,message:'Bad Request!'});
}
} else {
done();
}
});
// Custom Error Handler
server.setErrorHandler(function (error, request, reply) {
server.log.error(error);
reply.send({status:500,message:'Whoops, Something went wrong!'});
});
server.listen(config.nodePort, function (err) {
if (err) {
server.log.error(err);
process.exit(1);
}
});
}
if(config.useWorker) {
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', worker => {
console.log(`Worker ${worker.process.pid} died`);
});
} else {
App();
}
} else {
App();
}