// packages
import express from 'express';
import fs from 'fs';
import * as Q from 'q';
import * as domain from 'domain';

const d = domain.create();

// handle exceptions
d.on('error', function(err) {
    console.error(err);
});

// local imports
import config from './lib/config.js';
import Manager from './lib/manager.js';
import Experiment from './lib/experiment.js';

const app = express();

// fetch all this from config
const ip = config.get('ip');
const port = config.get('port');
const main_server_url = config.get('main_server_url');
const urls_url = config.get('urls_url');

const v2_url = main_server_url + 'v2';
const v1_url = main_server_url + 'v1';

function getExperiment(req) {
    let manager = new Manager(req.query, config.get('num_buckets'));
    let experiment = new Experiment(manager);
    return experiment;
}

function updateExperiments() {
    let deferred = Q.defer();
    // prevent exceptions, run in domain
    d.run(function() {
        fs.readFile(config.get('experiments_file'), 'utf8', function(err, data) {
            if (err) {
                deferred.reject(new Error(err));
            }
            deferred.resolve(data);
        })
    });
    return deferred.promise;
}

// v1 route
app.get(v1_url, function(req, res) {
    // uses promises
    updateExperiments().then(function(data) {
        res.set('Content-Type', 'application/json').json(getExperiment(req).load(data));
    });
});

// v2 route - returns the mainServerUrl as well
app.get(v2_url, function(req, res) {
    // uses promises
    updateExperiments().then(function(data) {
        res.set('Content-Type', 'application/json').json({
            'mainServerUrl': ip + ':' + port + v2_url,
            'results': getExperiment(req).load(data),
        });
    });
});

// urls route
app.get(urls_url, function(req, res) {
    res.set('Content-Type', 'application/json').json({
        // this route, where server routes are gotten
        // kept for backwards compatiability
        'updateServerUrl': ip + ':' + port + urls_url,

        // the mainServerUrl (v1)
        'mainServerUrl': ip + ':' + port + v1_url,
    });
});

app.listen(port, ip);

module.exports = app;

@@ -84,150 +84,8 @@
 'q';
-%0Aimport * as domain from 'domain';%0A%0Aconst d = domain.create();%0A%0A// handle exceptions%0Ad.on('error', function(err) %7B%0A    console.error(err);%0A%7D);
 %0A%0A//
@@ -752,16 +752,237 @@
 ();%0A    
+const experiments_file = config.get('experiments_file');%0A    if ('' === experiments_file %7C%7C typeof experiments_file === 'undefined') %7B%0A        deferred.reject(new Error('hi'));%0A        return deferred.promise;%0A    %7D;%0A    
 // preve
@@ -1014,35 +1014,8 @@
 ain%0A
-    d.run(function() %7B%0A    
     
@@ -1026,28 +1026,16 @@
 eadFile(
-config.get('
 experime
@@ -1042,18 +1042,16 @@
 nts_file
-')
 , 'utf8'
@@ -1082,20 +1082,16 @@
         
-    
 if (err)
@@ -1097,36 +1097,32 @@
 ) %7B%0A            
-   
- 
 deferred.reject(
@@ -1134,28 +1134,24 @@
 rror(err));%0A
-    
         %7D%0A  
@@ -1148,36 +1148,32 @@
       %7D%0A        
-    
 deferred.resolve
@@ -1184,19 +1184,8 @@
 a);%0A
-        %7D)%0A
     
@@ -1311,32 +1311,41 @@
 eriments().then(
+%0A        
 function(data) %7B
@@ -1337,32 +1337,36 @@
 unction(data) %7B%0A
+    
         res.set(
@@ -1438,25 +1438,95 @@
 data));%0A    
-%7D
+    %7D,%0A        function(err) %7B%0A            res.send(err)%0A        %7D%0A    
 );%0A%7D);%0A%0A// v
@@ -1656,16 +1656,25 @@
 ().then(
+%0A        
 function
@@ -1682,32 +1682,36 @@
 data) %7B%0A        
+  
+  
 res.set('Content
@@ -1749,32 +1749,36 @@
 n(%7B%0A            
+    
 'mainServerUrl':
@@ -1816,16 +1816,20 @@
         
+ 
+   
 'results
@@ -1862,16 +1862,20 @@
 (data),%0A
+    
         
@@ -1875,24 +1875,93 @@
        %7D
-);%0A    %7D
+%0A        )%7D,%0A        function(err) %7B%0A            res.send(err)%0A        %7D%0A    
 );%0A%7D);%0A%0A

fix promises when experiments_file is empty