var path = require("path");
var express = require("express");
var session = require("express-session");
var cookieParser = require("cookie-parser");
var bodyParser = require('body-parser');

var config = require("./data/config.json");
var Ajax = require("./ajax.js");
var Router = require("./router.js");
var debug = require("./debug.js");
var schedule = require("./scheduler.js");

var BASE_DIR = path.join(__dirname + "/../../../");
var DATA_DIR = path.join(BASE_DIR + "data/");
var HTML_REQ = /\.html(\?(.\=.\&)+(.\=.)?)?$/;

/**
 * Class Keeling, the
 * @type {[Keeling]}
 */
class Keeling {
    
    /**
     * [constructor description]
     * @param  {[type]} opt [description]
     * @return {[type]}     [description]
     */
    constructor(option) {
        
        if (!option) {
            try {
                option = require(DATA_DIR + "config.json");
            }
            catch (err) {
                console.error("User config not found. Initiating default config");
            }
        }
        for (var i in option) {
            if (config[i]) {
                config[i] = option[i];
            }
        }
        debug.log("User config initiated");
        this.config = config;
        
        var PUBLIC_DIR = path.join(BASE_DIR + config["public_directory"] + "/");
        var AJAX_DIR = path.join(BASE_DIR + config["ajax_directory"] + "/");
        var ROUTER_DIR = path.join(BASE_DIR + config["router_directory"] + "/");
        var SCHEDULE_DIR = path.join(BASE_DIR + config["schedule_directory"] + "/");
        var AJAX_REQ = "/" + config["ajax_directory"] + "/*";
        
        // Set the debug mode
        debug.set(config.debug ? config.debug : false);
        
        // Initiate the scheduler
        schedule.init();
        debug.log("Initiated Scheduler");
        
        // Initiate the express server
        var server = express();
        debug.log("Created Express Server");
        
        // Initialize session
        server.set('trust proxy', 1);
        server.use(session({ secret: config["session_secret"], cookie: { maxAge: 60000 }}));
        debug.log("Initiated Session");
        
        // Initialize cookie
        server.use(cookieParser());
        debug.log("Initialized Cookie Parser");
        
        // Use BodyParser
        server.use(bodyParser.urlencoded({ extended: false }));
        server.use(bodyParser.json());
        
        // Set Render Engine to EJS
        server.set("views", PUBLIC_DIR);
        server.engine(".html", require("ejs").__express);
        server.set('view engine', "html");
        debug.log("Initiated Render Engine");
        
        // Set the router
        var router = new Router(ROUTER_DIR, config.error_page);
        var bindRouter = router.router;
        bindRouter = bindRouter.bind(router);
        server.get(HTML_REQ, bindRouter);
        server.post(HTML_REQ, bindRouter);
        debug.log("Initiated Router");
        
        // Set the ajax
        var ajax = new Ajax(AJAX_DIR);
        var bindPreprocess = ajax.preprocess;
        var bindAjax = ajax.ajax;
        bindPreprocess = bindPreprocess.bind(ajax);
        bindAjax = bindAjax.bind(ajax);
        server.use(AJAX_REQ, bindPreprocess);
        server.get(AJAX_REQ, bindAjax);
        server.post(AJAX_REQ, bindAjax);
        debug.log("Initiated AJAX");
        
        // Set the default page
        server.get("/", function (req, res) {
            res.redirect("/" + config["default_page"] + ".html");
        });
        debug.log("Initiated Default Page");
        
        // Set the static
        server.use("/", express.static(PUBLIC_DIR));
        debug.log("Initiated Static Field");
        
        this.server = server;
    }
    
    /**
     * Start the server, make the server listen to the port in config
     */
    start() {
        
        // Precache self
        var self = this;
        
        schedule.run();
        
        // Listen to the port specified in the
        self.server.listen(self.config.port, function (err) {
            if (err) {
                console.error("Error starting server " + self.config.name + ": \n" + err);
            }
            else {
                console.log("Successfully started server " + self.config.name + " in port " + self.config.port);
            }
        });
    }
}

module.exports = {
    createServer: function (option) {
        
        // Return the newly created server
        return new Keeling(option);
    }
}

@@ -754,32 +754,66 @@
 ion) %7B%0A        %0A
+        var self = this;%0A        %0A
         if (!opt
@@ -1066,22 +1066,22 @@
 ar i in 
-option
+config
 ) %7B%0A    
@@ -1092,22 +1092,23 @@
     if (
-config
+!option
 %5Bi%5D) %7B%0A 
@@ -1126,26 +1126,26 @@
     
-config%5Bi%5D = option
+option%5Bi%5D = config
 %5Bi%5D;
@@ -1235,22 +1235,22 @@
 onfig = 
-config
+option
 ;%0A      
@@ -1290,32 +1290,37 @@
 join(BASE_DIR + 
+this.
 config%5B%22public_d
@@ -1374,32 +1374,37 @@
 join(BASE_DIR + 
+this.
 config%5B%22ajax_dir
@@ -1458,32 +1458,37 @@
 join(BASE_DIR + 
+this.
 config%5B%22router_d
@@ -1554,16 +1554,21 @@
 E_DIR + 
+this.
 config%5B%22
@@ -1621,24 +1621,29 @@
 REQ = %22/%22 + 
+this.
 config%5B%22ajax
@@ -1720,16 +1720,21 @@
 bug.set(
+this.
 config.d
@@ -1740,16 +1740,21 @@
 debug ? 
+this.
 config.d
@@ -2117,16 +2117,21 @@
 secret: 
+this.
 config%5B%22
@@ -2790,16 +2790,21 @@
 ER_DIR, 
+this.
 config.e
@@ -3551,16 +3551,21 @@
 t(%22/%22 + 
+self.
 config%5B%22

Allow multiple server creation