Newer
Older
const express = require('express');
const app = express();
const port = 9000;
const cookieParser = require('cookie-parser');
const printError = require('./helpers/logs.helpers.js').printError;
const printLog = require('./helpers/logs.helpers.js').printLog;
// Parse the request headers in order to populate req.cookies
app.use(cookieParser());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
app.locals.config = {
redisSentinelHost: process.env.REDIS_SENTINEL_HOST,
redisSentinelPort: process.env.REDIS_SENTINEL_PORT,
redisGroupName: process.env.REDIS_GROUP_NAME,
redisAuthorizedTTL: process.env.REDIS_AUTHORIZED_TTL, // In seconds
redisUnauthorizedTTL: process.env.REDIS_UNAUTHORIZED_TTL, // In seconds
elasticsearchUrl: process.env.ELASTICSEARCH_URL,
legacyAuthMiddlewareUrl: process.env.LEGACY_AUTH_MIDDLEWARE_URL,
technicalAccountUsername: process.env.TECHNICAL_ACCOUNT_USERNAME,
technicalAccountPassword: process.env.TECHNICAL_ACCOUNT_PASSWORD,
proxyHostTarget: process.env.PROXY_HOST_TARGET,
ignKey: process.env.IGN_KEY,
}
FORESTIER Fabien
committed
// Proxies configuration
FORESTIER Fabien
committed
// IGN Ortho
const IGNProxy = httpProxy.createProxyServer({
changeOrigin: true,
target: 'https://wxs.ign.fr/' + app.locals.config.ignKey + '/geoportail/r/wms/',
}).on('proxyReq', (proxyReq, req) => {
// keep a reference of the proxyRequest in the req object
FORESTIER Fabien
committed
req._proxyReq = proxyReq;
}).on('error', (err, req, res) => {
FORESTIER Fabien
committed
// If the client cancelled the request, abort the request to the upstream server
if (req.socket.destroyed && err.code === 'ECONNRESET') {
req._proxyReq.abort();
}
printError(`ING proxy error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`);
return;
FORESTIER Fabien
committed
});
const AuthenticatedProxy = httpProxy.createProxyServer({
changeOrigin: true,
target: app.locals.config.proxyHostTarget,
auth: `${app.locals.config.technicalAccountUsername}:${app.locals.config.technicalAccountPassword}`,
}).on('proxyReq', (proxyReq, req) => {
// keep a reference of the proxyRequest in the req object
req._proxyReq = proxyReq;
}).on('error', (err, req, res) => {
FORESTIER Fabien
committed
// If the client cancelled the request, abort the request to the upstream server
if (req.socket.destroyed && err.code === 'ECONNRESET') {
req._proxyReq.abort();
}
printError(`Authenticated proxy error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`);
return;
FORESTIER Fabien
committed
});
var UnauthenticatedProxy = httpProxy.createProxyServer({
changeOrigin: true,
target: app.locals.config.proxyHostTarget,
}).on('proxyReq', (proxyReq, req) => {
// keep a reference of the proxyRequest in the req object
req._proxyReq = proxyReq;
}).on('error', (err, req, res) => {
FORESTIER Fabien
committed
// If the client cancelled the request, abort the request to the upstream server
if (req.socket.destroyed && err.code === 'ECONNRESET') {
req._proxyReq.abort();
}
printError(`Unauthenticated proxy Error, req.socket.destroyed: ${req.socket.destroyed}, ${err}`);
return;
FORESTIER Fabien
committed
});
app.locals.proxies = {
ign: IGNProxy,
authenticated: AuthenticatedProxy,
unauthenticated: UnauthenticatedProxy,
};
// ROUTES
app.use(require('./routes/index.js'));
// STARTING SERVER
app.listen(port, () => printLog('index.js', `Proxy listening on port: ${port}`));