Skip to content

Commit a936f86

Browse files
committed
Prettifying code
1 parent 0b1808d commit a936f86

File tree

13 files changed

+1004
-1168
lines changed

13 files changed

+1004
-1168
lines changed

forwarder/bin/healthcheck.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,27 @@ const path = process.env.HEALTHCHECK_PATH || '/health';
66
const httpCode = process.env.HEALTHCHECK_CODE || 200;
77

88
const options = {
9-
host: 'localhost',
10-
port,
11-
timeout: 2000,
12-
method: 'GET',
13-
path
9+
host: 'localhost',
10+
port,
11+
timeout: 2000,
12+
method: 'GET',
13+
path
1414
};
1515

16-
const request = http.request(options, result => {
17-
// eslint-disable-next-line no-console
18-
console.info(`Performed health check, result ${result.statusCode}`);
19-
if (result.statusCode === httpCode) {
20-
process.exit(0);
21-
} else {
22-
process.exit(1);
23-
}
16+
const request = http.request(options, (result) => {
17+
// eslint-disable-next-line no-console
18+
console.info(`Performed health check, result ${result.statusCode}`);
19+
if (result.statusCode === httpCode) {
20+
process.exit(0);
21+
} else {
22+
process.exit(1);
23+
}
2424
});
2525

26-
request.on('error', err => {
27-
// eslint-disable-next-line no-console
28-
console.error(
29-
`An error occurred while performing health check, error: ${err}`
30-
);
31-
process.exit(1);
26+
request.on('error', (err) => {
27+
// eslint-disable-next-line no-console
28+
console.error(`An error occurred while performing health check, error: ${err}`);
29+
process.exit(1);
3230
});
3331

3432
request.end();

forwarder/bin/proxy.js

Lines changed: 80 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,119 +21,107 @@ const verify = process.env.VERIFY_CREDENTIALS || false;
2121
// create the proxy
2222
/** @type {import('http-proxy-middleware/dist/types').RequestHandler<express.Request, express.Response>} */
2323
const proxy = createProxyMiddleware({
24-
target, // target host with the same base path
25-
changeOrigin: true, // needed for virtual hosted sites
26-
on: {
27-
proxyReq: (proxyReq, req, res) => {
28-
debug(req.originalUrl);
29-
if (tenant) {
30-
proxyReq.setHeader('NGSILD-Tenant', tenant);
31-
}
32-
if (walletType) {
33-
proxyReq.setHeader('Wallet-type', walletType);
34-
}
35-
if (walletToken) {
36-
proxyReq.setHeader('Wallet-Token', walletToken);
37-
}
38-
if (walletAddress) {
39-
proxyReq.setHeader('Wallet-address', walletAddress);
40-
}
41-
if (acceptEncoding) {
42-
proxyReq.setHeader('accept-encoding', acceptEncoding);
43-
}
24+
target, // target host with the same base path
25+
changeOrigin: true, // needed for virtual hosted sites
26+
on: {
27+
proxyReq: (proxyReq, req, res) => {
28+
debug(req.originalUrl);
29+
if (tenant) {
30+
proxyReq.setHeader('NGSILD-Tenant', tenant);
31+
}
32+
if (walletType) {
33+
proxyReq.setHeader('Wallet-type', walletType);
34+
}
35+
if (walletToken) {
36+
proxyReq.setHeader('Wallet-Token', walletToken);
37+
}
38+
if (walletAddress) {
39+
proxyReq.setHeader('Wallet-address', walletAddress);
40+
}
41+
if (acceptEncoding) {
42+
proxyReq.setHeader('accept-encoding', acceptEncoding);
43+
}
44+
}
4445
}
45-
}
4646
});
4747

4848
function initForwarder(config, text) {
49-
const app = express();
50-
app.use('/health', require('express-healthcheck')());
49+
const app = express();
50+
app.use('/health', require('express-healthcheck')());
5151

52-
if (verify) {
53-
app.use(bearerToken());
54-
verifier = new Verifier.Verifier(config);
55-
app.use('/', verifier.verify);
56-
}
52+
if (verify) {
53+
app.use(bearerToken());
54+
verifier = new Verifier.Verifier(config);
55+
app.use('/', verifier.verify);
56+
}
5757

58-
app.use('/', proxy);
59-
app.listen(PORT, function() {
60-
debug(text);
61-
});
58+
app.use('/', proxy);
59+
app.listen(PORT, function () {
60+
debug(text);
61+
});
6262
}
6363

6464
/**
6565
* Check that the IDM is responding and the PEP is recognized within the IDM
6666
* @return an auth token representing the PEP itself to be used in subsequent requests
6767
*/
6868
function connect() {
69-
let retry = 20;
70-
return new Promise((resolve, reject) => {
71-
const connect_with_retry = async () => {
72-
try {
73-
await ConfigService.checkConnectivity();
74-
debug(
75-
`Credentials Config Service is now available - requesting config for ${tenant}`
76-
);
69+
let retry = 20;
70+
return new Promise((resolve, reject) => {
71+
const connect_with_retry = async () => {
72+
try {
73+
await ConfigService.checkConnectivity();
74+
debug(`Credentials Config Service is now available - requesting config for ${tenant}`);
7775

78-
ConfigService.getConfig(tenant)
79-
.then(response => {
80-
return resolve(response);
81-
})
82-
.catch(error => {
83-
return reject(
84-
'Credentials Config Service rejected config: ' + error.message
85-
);
86-
});
87-
} catch (e) {
88-
debug(e.message);
89-
retry--;
90-
if (retry === 0) {
91-
return reject(
92-
'Credentials Config Service is not available. Giving up after 20 attempts'
93-
);
94-
}
95-
debug('retry after 5 seconds.');
96-
//eslint-disable-next-line snakecase/snakecase
97-
setTimeout(connect_with_retry, 5000);
98-
}
99-
};
100-
connect_with_retry();
101-
});
76+
ConfigService.getConfig(tenant)
77+
.then((response) => {
78+
return resolve(response);
79+
})
80+
.catch((error) => {
81+
return reject('Credentials Config Service rejected config: ' + error.message);
82+
});
83+
} catch (e) {
84+
debug(e.message);
85+
retry--;
86+
if (retry === 0) {
87+
return reject('Credentials Config Service is not available. Giving up after 20 attempts');
88+
}
89+
debug('retry after 5 seconds.');
90+
//eslint-disable-next-line snakecase/snakecase
91+
setTimeout(connect_with_retry, 5000);
92+
}
93+
};
94+
connect_with_retry();
95+
});
10296
}
10397

10498
function startServer(config) {
105-
if (clusterWorkerSize > 1) {
106-
if (cluster.isMaster) {
107-
for (let i = 0; i < clusterWorkerSize; i++) {
108-
cluster.fork();
109-
}
110-
cluster.on('exit', function(worker) {
111-
debug('Worker', worker.id, ' has exited.');
112-
});
99+
if (clusterWorkerSize > 1) {
100+
if (cluster.isMaster) {
101+
for (let i = 0; i < clusterWorkerSize; i++) {
102+
cluster.fork();
103+
}
104+
cluster.on('exit', function (worker) {
105+
debug('Worker', worker.id, ' has exited.');
106+
});
107+
} else {
108+
initForwarder(config, `Server listening on port ${PORT} and worker ${process.pid}`);
109+
}
113110
} else {
114-
initForwarder(
115-
config,
116-
`Server listening on port ${PORT} and worker ${process.pid}`
117-
);
111+
initForwarder(config, `Server listening on port ${PORT} with the single worker ${process.pid}`);
118112
}
119-
} else {
120-
initForwarder(
121-
config,
122-
`Server listening on port ${PORT} with the single worker ${process.pid}`
123-
);
124-
}
125113
}
126114

127115
if (verify) {
128-
connect().then(
129-
config => {
130-
startServer(config);
131-
},
132-
err => {
133-
debug(err);
134-
process.exit(1);
135-
}
136-
);
116+
connect().then(
117+
(config) => {
118+
startServer(config);
119+
},
120+
(err) => {
121+
debug(err);
122+
process.exit(1);
123+
}
124+
);
137125
} else {
138-
startServer(null);
126+
startServer(null);
139127
}

forwarder/lib/configService.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,27 @@ const debug = require('debug')('broker:config');
55
/**
66
* Check that Keyrock is responding to requests
77
*/
8-
exports.checkConnectivity = function() {
9-
debug(
10-
`Connecting to Configuration Service at http://${configServiceURL}/service`
11-
);
12-
return fetch(`http://${configServiceURL}/service`);
8+
exports.checkConnectivity = function () {
9+
debug(`Connecting to Configuration Service at http://${configServiceURL}/service`);
10+
return fetch(`http://${configServiceURL}/service`);
1311
};
1412

15-
exports.getConfig = function(tenant) {
16-
return new Promise(function(resolve, reject) {
17-
debug(
18-
`Retrieving config from http://${configServiceURL}/service/${tenant}`
19-
);
20-
const fetchPromise = fetch(`http://${configServiceURL}/service/${tenant}`);
21-
fetchPromise
22-
.then(response => {
23-
if (response.status === StatusCodes.NOT_FOUND) {
24-
resolve({});
25-
}
26-
return response.json();
27-
})
28-
.then(config => {
29-
resolve(config.oidcScopes[config.defaultOidcScope]);
30-
})
31-
.catch(error => {
32-
return reject(error);
33-
});
34-
});
13+
exports.getConfig = function (tenant) {
14+
return new Promise(function (resolve, reject) {
15+
debug(`Retrieving config from http://${configServiceURL}/service/${tenant}`);
16+
const fetchPromise = fetch(`http://${configServiceURL}/service/${tenant}`);
17+
fetchPromise
18+
.then((response) => {
19+
if (response.status === StatusCodes.NOT_FOUND) {
20+
resolve({});
21+
}
22+
return response.json();
23+
})
24+
.then((config) => {
25+
resolve(config.oidcScopes[config.defaultOidcScope]);
26+
})
27+
.catch((error) => {
28+
return reject(error);
29+
});
30+
});
3531
};

forwarder/lib/emitter.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
const debug = require('debug')('broker:emitter');
2-
const WEB_APP_URL =
3-
'http://' +
4-
(process.env.WEB_APP_HOST || 'localhost') +
5-
':' +
6-
(process.env.WEB_APP_PORT || 3000);
2+
const WEB_APP_URL = 'http://' + (process.env.WEB_APP_HOST || 'localhost') + ':' + (process.env.WEB_APP_PORT || 3000);
73

8-
exports.emit = function(subject, data) {
9-
return fetch(`${WEB_APP_URL}/message/${subject}`, {
10-
method: 'POST',
11-
headers: {
12-
'Content-Type': 'application/json'
13-
},
14-
body: JSON.stringify({ data })
15-
}).catch(e => {
16-
debug(e);
17-
});
4+
exports.emit = function (subject, data) {
5+
return fetch(`${WEB_APP_URL}/message/${subject}`, {
6+
method: 'POST',
7+
headers: {
8+
'Content-Type': 'application/json'
9+
},
10+
body: JSON.stringify({ data })
11+
}).catch((e) => {
12+
debug(e);
13+
});
1814
};

0 commit comments

Comments
 (0)