Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Commit

Permalink
Improve missing tokens display in express env
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisjanvier committed Nov 10, 2019
1 parent d3113b3 commit b17ecb5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
38 changes: 23 additions & 15 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ import express from 'express';
import proxy from 'http-proxy-middleware';

import config from './config.js';
import { MODE_RECORDER } from './modes.js';
import { myna } from './myna.js';

export const app = express();

app.get('/', (req, res) => res.json({ message: 'Hello Web Myna' }));
app.get('/', (__, res) => res.json({ message: 'Hello Web Myna' }));

for (const api of config.apis) {
const proxyOptions = {
changeOrigin: true,
proxyTimeout: 5000,
timeout: 5000,
target: api.url,
ignorePath: false,
pathRewrite: path => path.replace(`/${api.name}`, ''),
onProxyReq: proxyReq => {
if (api.requiresAuthentication) {
proxyReq.setHeader(`${api.tokenKey || 'authorization'}`, `${api.tokenPrefix || 'Bearer'} ${api.token}`);
}
},
};
app.use(`/${api.name}`, myna(api), proxy(proxyOptions));
if (config.mode === MODE_RECORDER) {
const proxyOptions = {
changeOrigin: true,
proxyTimeout: 5000,
timeout: 5000,
target: api.url,
ignorePath: false,
pathRewrite: path => path.replace(`/${api.name}`, ''),
onProxyReq: proxyReq => {
if (api.requiresAuthentication) {
proxyReq.setHeader(
`${api.tokenKey || 'authorization'}`,
`${api.tokenPrefix || 'Bearer'} ${api.token}`,
);
}
},
};
app.use(`/${api.name}`, myna(api), proxy(proxyOptions));
} else {
app.use(`/${api.name}`, myna(api));
}
}
20 changes: 18 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import signale from 'signale';
import boxen from 'boxen';

import * as appJs from './app.js';
import config, { getMissingEnvironmentTokens } from './config.js';
import config, { getApiTokenName, getMissingEnvironmentTokens } from './config.js';

const missingTokens = getMissingEnvironmentTokens();

if (missingTokens.length) {
signale.error('IL MANQUE DES TOKENS', missingTokens);
const missingApisWithAuthentication = config.apis
.filter(api => missingTokens.includes(getApiTokenName(api)))
.map(api => api.name);
const isPlural = missingApisWithAuthentication.length > 1;
const message = `
You have declared ${missingApisWithAuthentication.length} api${
isPlural ? 's' : ''
} as requiring an authentication token: ${missingApisWithAuthentication.join(', ')}.
You must therefore declare the following token${isPlural ? 's' : ''} as an environment variable: ${missingTokens
.map(
token => `
=> ${token}`,
)
.join('')}`;
signale.log(boxen(message, { padding: 1, margin: 1, borderColor: 'red', align: 'center' }));
} else {
appJs.app.listen(config.proxyPort, () => {
signale.info(`Web Myna is starded on port ${config.proxyPort} in environment ${config.env}`);
Expand Down

0 comments on commit b17ecb5

Please sign in to comment.