/**
 * A proxy server with basic auth
 */

var path = require('path');
var koa = require('koa');
var addSlashes = require('koa-add-trailing-slashes');
var mount = require('koa-mount');
var serve = require('koa-static');
var auth = require('basic-auth');
var httpProxy = require('http-proxy');

// app
var app = koa();

// add trailing slashes
app.use(addSlashes());

// serve static page
app.use(mount('/dashboard', serve(path.join(__dirname, '../public'))));

// create a proxy
var proxy = httpProxy.createProxyServer();

// proxy requests
var name = process.env['kong-dashboard-name'];
var pass = process.env['kong-dashboard-pass'];
app.use(function *(next){
  var ctx = this;
  var credentials = auth(ctx.req)
  if (ctx.request.method !== 'OPTION' && name) {
    // check basic auth
    if (!credentials || credentials.name !== name || credentials.pass !== pass) {
      ctx.response.status = 401;
      ctx.response.headers['WWW-Authenticate'] = 'Basic realm="example"';
      ctx.body = 'Access denied';
      return;
    }
  }
  // proxy requests
  yield new Promise(function (resolve, reject) {
    proxy.web(ctx.req, ctx.res, {target: ctx.request.headers['kong-node-url']}, function (err) {
      resolve();
    });
  });
});

app.listen(process.env['kong-dashboard-port']);

console.log('Server is runding on port ' + process.env['kong-dashboard-port']);

@@ -1026,16 +1026,186 @@
   %7D%0A  %7D%0A
+  // check kong node url%0A  if (!ctx.request.headers%5B'kong-node-url'%5D) %7B%0A    ctx.response.status = 400;%0A    ctx.body = 'Kong-Node-URL header is required';%0A    return;%0A  %7D%0A
   // pro

handle kong node url missing exception, now respond with 400