var path = require("path");
var express = require("express");

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

var BASE_DIR = path.join(__dirname + "/../../../");
var DATA_DIR = path.join(BASE_DIR + "data/");
var PUBLIC_DIR = path.join(BASE_DIR + "public/");

var HTML_REQ = /\.html(\?(.\=.\&)+)?$/;
var AJAX_REQ = "/ajax/*";

/**
 * Class Keeling, the
 * @type {[Keeling]}
 */
class Keeling {
    
    /**
     * [constructor description]
     * @param  {[type]} opt [description]
     * @return {[type]}     [description]
     */
    constructor(opt) {
        
        // Save the options
        this.opt = opt;
        
        // Set the debug mode
        debug.set(opt.debug ? opt.debug : false);
        
        // Initiate the scheduler
        schedule.init();
        
        debug.log("Initiated Scheduler");
        
        // Initiate the express server
        var server = express();
        
        debug.log("Created Express Server");
        
        // 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
        server.use(HTML_REQ, router.preprocess);
        server.get(HTML_REQ, router.router);
        server.post(HTML_REQ, router.router);
        
        debug.log("Initiated Router");
        
        // Set the ajax
        server.use(AJAX_REQ, ajax.preprocess);
        server.get(AJAX_REQ, ajax.ajax);
        server.post(AJAX_REQ, ajax.ajax);
        
        debug.log("Initiated AJAX");
        
        // Set the static
        server.use("/", express.static(RES_DIR));
        
        debug.log("Initiated Static Field");
        
        // Set the default page
        server.get("/", function (req, res) {
            res.redirect("/" + config["default_page"] + ".html");
        });
        
        debug.log("Initiated Default Page");
        
        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.opt.port, function (err) {
            if (err) {
                debug.log("Error starting server " + self.opt.name + ": \n" + err);
            }
            else {
                debug.log("Successfully started server " + self.opt.name + " in port " + self.opt.port);
            }
        });
    }
}

module.exports = {
    debug: debug,
    createServer: function (opt) {
        
        // Merge the option to config
        for (var i in opt) {
            if (config[i]) {
                config[i] = opt[i];
            }
        }
        
        // Return the newly created server
        return new Keeling(config);
    }
}

@@ -50,24 +50,111 @@
 (%22express%22);
+%0Avar session = require(%22express-session%22);%0Avar cookieParser = require(%22cookie-parser%22);
 %0A%0Avar config
@@ -992,33 +992,24 @@
 ule.init();%0A
-        %0A
         debu
@@ -1114,33 +1114,24 @@
  express();%0A
-        %0A
         debu
@@ -1164,32 +1164,450 @@
 ver%22);%0A        %0A
+        // Initialize session%0A        server.set('trust proxy', 1);%0A        server.use(session(%7B%0A            secret: 'keyboard cat',%0A            resave: false,%0A            saveUninitialized: true,%0A            cookie: %7B secure: true %7D%0A        %7D));%0A        debug.log(%22Initiated Session%22);%0A        %0A        // Initialize cookie%0A        server.use(cookieParser());%0A        debug.log(%22Initialized Cookie Parser%22);%0A        %0A
         // Set R
@@ -1764,33 +1764,24 @@
 ', %22html%22);%0A
-        %0A
         debu
@@ -1989,25 +1989,16 @@
 outer);%0A
-        %0A
         
@@ -2191,25 +2191,16 @@
 .ajax);%0A
-        %0A
         
@@ -2313,25 +2313,16 @@
 _DIR));%0A
-        %0A
         
@@ -2523,25 +2523,16 @@
     %7D);%0A
-        %0A
         

Integrate Cookie and Session