@@ -15,102 +15,114 @@ config.https = config.https || {};
15
15
const logger = require('morgan');
16
16
const debug = require('debug')('pep-proxy:app');
17
17
const express = require('express');
18
+ const os = require('os');
19
+ const cluster = require('cluster');
20
+ const clusterWorkerSize = os.cpus().length;
18
21
19
- process.on('uncaughtException', function(err) {
22
+ process.on('uncaughtException', function (err) {
20
23
debug('Caught exception: ' + err);
21
24
});
22
25
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
23
26
24
- const app = express();
25
-
26
- // Set logs in development
27
- if (config.debug) {
28
- app.use(logger('dev'));
29
- }
30
-
31
- //app.use(express.bodyParser());
32
-
33
- app.use(function(req, res, next) {
34
- const bodyChunks = [];
35
- req.on('data', function(chunk) {
36
- bodyChunks.push(chunk);
37
- });
38
-
39
- req.on('end', function() {
40
- if (bodyChunks.length > 0) {
41
- req.body = Buffer.concat(bodyChunks);
42
- }
43
- next();
44
- });
45
- });
46
-
47
- app.disable('x-powered-by');
48
- app.use(errorhandler({ log: debug}));
49
- app.use(cors(config.cors));
50
-
51
27
let port = config.pep_port || 80;
52
28
if (config.https.enabled) {
53
29
port = config.https.port || 443;
54
30
}
55
- app.set('port', port);
56
31
57
- for (const p in config.public_paths) {
58
- debug('Public paths', config.public_paths[p]);
59
- app.all(config.public_paths[p], Root.public);
60
- }
32
+ function start_server() {
33
+ const app = express();
61
34
62
- app.all('/*', Root.pep);
35
+ // Set logs in development
36
+ if (config.debug) {
37
+ app.use(logger('dev'));
38
+ }
63
39
40
+ app.use(function (req, res, next) {
41
+ const bodyChunks = [];
42
+ req.on('data', function (chunk) {
43
+ bodyChunks.push(chunk);
44
+ });
64
45
65
- function connectIDM(callback) {
66
- IDM.authenticate(
67
- function(token) {
68
- debug('Success authenticating PEP proxy. Proxy Auth-token: ', token);
69
- callback();
70
- },
71
- function(status, e) {
72
- debug('Error in IDM communication', e);
73
- callback(e);
74
- }
75
- );
76
- }
46
+ req.on('end', function () {
47
+ if (bodyChunks.length > 0) {
48
+ req.body = Buffer.concat(bodyChunks);
49
+ }
50
+ next();
51
+ });
52
+ });
77
53
78
- if (config.https.enabled === true) {
79
- const options = {
80
- key: fs.readFileSync(config.https.key_file),
81
- cert: fs.readFileSync(config.https.cert_file),
82
- };
83
-
84
- https
85
- .createServer(options, function(req, res) {
86
- app.handle(req, res);
87
- })
88
- .listen(app.get('port'));
89
- } else {
90
- app.listen(app.get('port'));
54
+ app.disable('x-powered-by');
55
+ app.use(errorhandler({ log: debug }));
56
+ app.use(cors(config.cors));
57
+ app.set('port', port);
58
+
59
+ for (const p in config.public_paths) {
60
+ debug('Public paths', config.public_paths[p]);
61
+ app.all(config.public_paths[p], Root.public);
62
+ }
63
+
64
+ app.all('/*', Root.pep);
65
+
66
+ if (config.https.enabled === true) {
67
+ const options = {
68
+ key: fs.readFileSync(config.https.key_file),
69
+ cert: fs.readFileSync(config.https.cert_file)
70
+ };
71
+
72
+ https
73
+ .createServer(options, function (req, res) {
74
+ app.handle(req, res);
75
+ })
76
+ .listen(app.get('port'));
77
+ } else {
78
+ app.listen(app.get('port'));
79
+ }
91
80
}
92
81
93
- let retry = 20;
94
82
function connect() {
95
- const connect_with_retry = () => {
96
- connectIDM((err) => {
97
- if (err) {
98
- retry--;
99
- if (retry === 0) {
100
- debug('Error found after [%d] attempts: %s', 20, error);
101
- process.exit(1);
102
- } else {
103
- console.log('retry after 5 seconds.');
104
- //eslint-disable-next-line snakecase/snakecase
105
- setTimeout(connect_with_retry, 5000);
83
+ let retry = 20;
84
+ return new Promise((resolve, reject) => {
85
+ const connect_with_retry = () => {
86
+ IDM.authenticate(
87
+ (token) => {
88
+ debug('Success authenticating PEP proxy.');
89
+ resolve(token);
90
+ },
91
+ (status, e) => {
92
+ debug('Error in IDM communication', e);
93
+ retry--;
94
+ if (retry === 0) {
95
+ reject(e);
96
+ } else {
97
+ debug('retry after 5 seconds.');
98
+ //eslint-disable-next-line snakecase/snakecase
99
+ setTimeout(connect_with_retry, 5000);
100
+ }
106
101
}
107
- } else {
108
- debug('Success authenticating PEP proxy.');
109
- }
110
- });
111
- };
112
- connect_with_retry();
102
+ );
103
+ };
104
+ connect_with_retry();
105
+ });
113
106
}
114
107
115
108
debug('Starting PEP proxy in port ' + port + '. IdM authentication ...');
116
- connect();
109
+ connect().then(
110
+ (token) => {
111
+ debug('Success authenticating PEP proxy. Proxy Auth-token: ', token);
112
+ if (clusterWorkerSize > 1) {
113
+ if (cluster.isMaster) {
114
+ for (let i = 0; i < clusterWorkerSize; i++) {
115
+ cluster.fork();
116
+ }
117
+ } else {
118
+ start_server();
119
+ }
120
+ } else {
121
+ start_server();
122
+ }
123
+ },
124
+ (err) => {
125
+ debug('Error found after [%d] attempts: %s', 20, err);
126
+ process.exit(1);
127
+ }
128
+ );
0 commit comments