{"size": 2991, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "xontab/basic-node-ci", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "xontab/basic-node-ci", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "xontab/basic-node-ci", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.1612903226, "max_line_length": 147, "alphanum_fraction": 0.6345703778}
const appsHelper = require('./app/helpers/apps.js');
const list = require('./config/apps.json');
const webConfig = require('./config/web.json');

appsHelper.loadConfig(list);  

if (webConfig['web-ui-enable']) {
  // Web UI
  const express = require('express');
  const bodyParser = require('body-parser');
  const app = express();
  app.use(bodyParser.json());

  const Immutable = require('Immutable');
  const uuid = require('node-uuid');
  const loggedUsers = [];

  // APIs
  const loggingHelper = require('./app/helpers/logging.js');

  function handleAuthenication(req, res, next) {
    const accessToken = req.headers['authorization'];
    if (accessToken && accessToken.length > 7 && loggedUsers[accessToken.substr(7)] && loggedUsers[accessToken.substr(7)] > new Date().getTime()) {
      next();
    } else {
      res.status(401).send({ error: 'Authentication failed' });
    }
  }

  app.get('/api/apps', handleAuthenication, (req, res) => {
      res.send(appsHelper.getRunningApps());
  });

  app.get('/api/logs/:id', handleAuthenication, (req, res) => {
      res.send(appsHelper.getFullLogs(req.params.id));
  });

  app.get('/api/exec/:id', handleAuthenication,  (req, res) => {
      res.send(appsHelper.executeApp(req.params.id));
  });

  app.get('/api/logout', (req, res) => {
      const accessToken = req.headers['authorization'];
      loggedUsers[accessToken.substr(7)] = undefined;
      res.send({ status: 'OK' });
  });

  app.post('/api/login', (req, res) => {
    const logins = Immutable.fromJS(require('./config/users.json'));
    if (logins.findIndex(x => x.get('username') === req.body.username && x.get('password') === req.body.password) >= 0) {
      const accessToken = uuid.v4();
      loggedUsers[accessToken] = new Date().getTime() + 3600000;
      res.send({ token: accessToken });
    } else {
      res.send({ token: '' });
    }
  });

  // Webpack
  const isDeveloping = process.env.NODE_ENV !== 'production';

  const webpack = require('webpack');
  const webpackMiddleware = require('webpack-dev-middleware');
  const webpackHotMiddleware = require('webpack-hot-middleware');
  const webpackConfig = require('./dev/webpack.config.js');

  const webPort = isDeveloping ? webConfig['web-port'] : process.env.PORT;
  const path = require('path');

  if (isDeveloping) {
    const compiler = webpack(webpackConfig);
    const middleware = webpackMiddleware(compiler, {
      publicPath: webpackConfig.output.publicPath,
      hot: true
    });

    app.use(middleware);
    app.use(webpackHotMiddleware(compiler));
    app.get('*', (req, res) => {
      res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
      res.end();
    });
  } else {
    app.use(express.static(__dirname + '/dist'));
    app.get('*', (req, res) => {
      res.sendFile(path.join(__dirname, 'dist/index.html'));
    });
  }

  app.listen(webPort, () => {
      loggingHelper.log('Initialise', `Web: http://localhost:${webPort}`);
  });
}