view mock-api/mockapi.server.js @ 237:68d7308271aa

Fix kill_vm mockapi command endpoint Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025267.html
author Andrew Azores <aazores@redhat.com>
date Wed, 04 Oct 2017 10:23:50 -0400
parents 78c7d1c23616
children
line wrap: on
line source

var express = require('express'),
  expressWs = require('express-ws'),
  basicAuth = require('express-basic-auth'),
  cors = require('cors'),
  path = require('path'),
  fs = require('fs'),
  _ = require('lodash');

var port = process.env.MOCKAPI_PORT || 8888;
var host = process.env.MOCKAPI_HOST || '0.0.0.0';

var app = express();
expressWs(app);
app.use(cors());

// express-ws does not work when basicAuth is enabled. If testing command channel
// (or other websocket) functionality, temporarily remove the configuration below.
app.use(basicAuth({
  users: {
    'client': 'client-pwd'
  },
  challenge: true,
  unauthorizedResponse: fs.readFileSync(path.resolve(__dirname, '401.html'), 'utf8')
}));

app.set('port', port);
app.set('host', host);

var endpoints = path.resolve(__dirname, 'endpoints');
fs.readdir(endpoints, function (err, files) {
  var server = {
    app: app,
    init: function (svc) {
      console.info('mock ' + svc + ' up');
    },
    logRequest: function (svc, req) {
      console.info('[' + svc + '] requested');
      console.info('params: ' + JSON.stringify(req.params));
      console.info('query: ' + JSON.stringify(req.query));
      console.info('authorization:' + JSON.stringify(req.headers.authorization));
      console.info('~~~~\n');
    }
  };
  for (var i = 0; i < files.length; i++) {
    var file = files[i];
    if (_.endsWith(file, '.endpoint.js')) {
      require(path.resolve(endpoints, file))(server);
    }
  }
});

app.listen(app.get('port'), app.get('host'), function () {
  console.info('Mock-API server started on http://' + app.get('host') + ':' + app.get('port'));
});