github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/server/proxies/api.js (about)

     1  'use strict';
     2  
     3  const proxyPath = '/v1';
     4  
     5  module.exports = function(app, options) {
     6    // For options, see:
     7    // https://github.com/nodejitsu/node-http-proxy
     8  
     9    // This is probably not safe to do, but it works for now.
    10    let cacheKey = `${options.project.configPath()}|${options.environment}`;
    11    let config = options.project.configCache.get(cacheKey);
    12  
    13    // Disable the proxy completely when Mirage is enabled. No requests to the API
    14    // will be being made, and having the proxy attempt to connect to Nomad when it
    15    // is not running can result in socket max connections that block the livereload
    16    // server from reloading.
    17    if (config['ember-cli-mirage'].enabled !== false) {
    18      options.ui.writeInfoLine('Mirage is enabled. Not starting proxy');
    19      delete options.proxy;
    20      return;
    21    }
    22  
    23    let proxyAddress = options.proxy;
    24  
    25    let server = options.httpServer;
    26    let proxy = require('http-proxy').createProxyServer({
    27      target: proxyAddress,
    28      ws: true,
    29      changeOrigin: true,
    30    });
    31  
    32    proxy.on('error', function(err, req) {
    33      // eslint-disable-next-line
    34      console.error(err, req.url);
    35    });
    36  
    37    app.use(proxyPath, function(req, res) {
    38      // include root path in proxied request
    39      req.url = proxyPath + req.url;
    40      proxy.web(req, res, { target: proxyAddress });
    41    });
    42  
    43    server.on('upgrade', function(req, socket, head) {
    44      if (req.url.startsWith('/v1/client/allocation') && req.url.includes('exec?')) {
    45        req.headers.origin = proxyAddress;
    46        proxy.ws(req, socket, head, { target: proxyAddress });
    47      }
    48    });
    49  };