Skip to content
Snippets Groups Projects
index.js 3.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • const express = require('express');
    const app = express();
    const port = 9000;
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    const httpProxy = require('http-proxy');
    
    const cookieParser = require('cookie-parser');
    const printError = require('./helpers/logs.helpers.js').printError;
    
    const printLog = require('./helpers/logs.helpers.js').printLog;
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    
    
    // Parse the request headers in order to populate req.cookies
    app.use(cookieParser());
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    
    
    app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      next();
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    });
    
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    
    
    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,
    }
    
    // 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
    
    }).on('error', (err, req, res) => {
    
      // 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;
    
    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) => {
    
      // 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;
    
    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) => {
    
      // 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;
    
    app.locals.proxies = {
      ign: IGNProxy,
      authenticated: AuthenticatedProxy,
      unauthenticated: UnauthenticatedProxy,
    };
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    
    
    // ROUTES
    app.use(require('./routes/index.js'));
    
    FORESTIER Fabien's avatar
    FORESTIER Fabien committed
    
    
    app.listen(port, () => printLog('index.js', `Proxy listening on port: ${port}`));