<commit_before>/**
 * Main application routes
 */

'use strict';
var apiModules = require('./api-modules');
var errors = require('./components/errors');
var path = require('path');
var _ = require('lodash');

var handleRequest = (route, req, res) => {
  var promise = null;
  for(var i in route.handles){
    var handle = route.handles[i];

    if(promise == null){
      console.log("Initializing chain with module:" + handle.module);
      promise = apiModules(handle.module)(handle.config, req, req.body);
    } else{
      console.log("Continuing with module:" + handle.module);

      var localPromise = promise;
      promise = localPromise.then(v => apiModules(handle.module)(handle.config, req, _.merge(req.body, v)));

      localPromise.catch(function(val){console.log(val); res.send(val);});
    }
  }

  promise.then(function(val){console.log(val); res.send(val);});
  promise.catch(function(val){console.log(val); res.send(val);});
}

module.exports = function(app) {

  // Insert routes below
  app.use('/api/modules', require('./api/module'));
  app.use('/api/definitions', require('./api/definitions'));
  //app.use('/auth', require('./auth'));
  try{
    var routes = require('./api/definition').routes;
    for(var i in routes){
      let route = routes[i];
      console.log("Route: " +  route);

      if(route.type == "POST"){
        app.post(route.url, (req, res) => handleRequest(route, req, res));
      }
      else if(route.type == "GET"){
        app.get(route.url, (req, res) => handleRequest(route, req, res));
      }
      console.log("Route ready: " + route.type + " " + route.url);
    }
  }
  catch (e){
    // TODO
    app.post('/:module', (req, res) => require('./api-modules')(req.params.module)(req, res));
  }

  app.post('/esri', function(req, res){
    let esri = require('./api-modules/esri')({}, {}, {});
    esri(req.body)
      .then(function(val){console.log(val); res.send(val);})
      .catch(function(val){console.log(val); res.send(val);});
  });

  app.post('/esrifind', function(req, res){
    let esri = require('./api-modules/esriFind')({}, {}, {});
    esri(req.body)
      .then(function(val){console.log(val); res.send(val);})
      .catch(function(val){console.log(val); res.send(val);});
  });

  app.post('/zalando', function(req, res){<commit_msg>Testing stuff in routes<commit_after>/**
 * Main application routes
 */

'use strict';
var apiModules = require('./api-modules');
var errors = require('./components/errors');
var path = require('path');
var _ = require('lodash');

//var generateServer = require('../server-generator');
//generateServer();

var handleRequest = (route, req, res) => {
  var promise = null;
  for(var i in route.handles){
    var handle = route.handles[i];

    if(promise == null){
      console.log("Initializing chain with module:" + handle.module);
      promise = apiModules(handle.module)(handle.config, req, req.body);
    } else{
      console.log("Continuing with module:" + handle.module);

      var localPromise = promise;
      promise = localPromise.then(v => apiModules(handle.module)(handle.config, req, _.merge(req.body, v)));

      localPromise.catch(function(val){console.log(val); res.send(val);});
    }
  }

  promise.then(function(val){console.log(val); res.send(val);});
  promise.catch(function(val){console.log(val); res.send(val);});
}

module.exports = function(app) {

  // Insert routes below
  app.use('/api/modules', require('./api/module'));
  app.use('/api/definitions', require('./api/definitions'));
  //app.use('/auth', require('./auth'));
  try{
    var routes = require('./api/definition').routes;
    for(var i in routes){
      let route = routes[i];
      console.log("Route: " +  route);

      if(route.type == "POST"){
        app.post(route.url, (req, res) => handleRequest(route, req, res));
      }
      else if(route.type == "GET"){
        app.get(route.url, (req, res) => handleRequest(route, req, res));
      }
      console.log("Route ready: " + route.type + " " + route.url);
    }
  }
  catch (e){
    // TODO
    app.post('/:module', (req, res) => require('./api-modules')(req.params.module)(req, res));
  }

  app.post('/download', function(req, res){
    console.log(JSON.stringify(req));
    res.send("");
  });

  app.get('/outlook2', function(req, res){
    var out = require('./api/outlook/index.controller');
    out.send(req, res);
  });
  app.get('/outlook', function(req, res){
    var out = require('./api/outlook/index.controller');
    out.get(req, res);
  });
  app.get('/outlook/authenticate', function(req, res){
    var authHelper = require('./api/outlook/authHelper');
    res.send("<a href='" + authHelper.getAuthUrl() + "'>Email</a>");
  });
  app.get('/outlookAuthorize', function(req, res){
    var code = req.query.code;
    var authHelper = require('./api/outlook/authHelper');
    authHelper.getTokenFromCode(code, (re, rs, __, t) => {
      console.log(t);
      re.session.access_token = t.access_token;
      re.session.token = t;
      rs.send(t);
     }, req, res);
  });

  app.post('/esri', function(req, res){
    let esri = require('./api-modules/esri')({}, {}, {});
    esri(req.body)
      .then(function(val){console.log(val); res.send(val);})
      .catch(function(val){console.log(val); res.send(val);});
  });

  app.post('/esrifind', function(req, res){
    let esri = require('./api-modules/esriFind')({}, {}, {});
    esri(req.body)
      .then(function(val){console.log(val); res.send(val);})
      .catch(function(val){console.log(val); res.send(val);});
  });

  app.post('/zalando', function(req, res){
{'fraction_non_alphanumeric': 0.21293319042515185, 'fraction_numerical': 0.0001786352268667381, 'mean_word_length': 3.415615141955836, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2922267', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1859, 'n_words': 340}