/**
 * lark.js - lib/application.js
 * Copyright(c) 2014 larkjs-team
 * MIT Licensed
 */

'use strict';

/**
 * Module dependencies.
 */

var util = require('util');
var koa = require('koa');
var config = require('lark-config');
var http = require('http');
var middlewares = require('./middlewares');

/**
 * Application constructor
 *
 */

function Application(options) {
    if (!(this instanceof Application)) return new Application(options);
    koa.call(this);
    this.config = config(options);
}

util.inherits(Application, koa);

var app = Application.prototype;
/**
 * @param port {number}
 * @param callback {function}
 * @returns {*|Server}
 */
app.listen = function () {
    if (enable(this.config, 'mvc')) {
        this.use(middlewares.mvc.middleware(this.config.mvc));
    }
    if (enable(this.config, 'router')) {
        this.use(middlewares.router(this.config.router));
    }
    if (enable(this.config, 'bootstrap')) {
        this.use(middlewares.bootstrap(this, this.config.bootstrap));
    }
    var server = http.createServer(this.callback());
    return server.listen.apply(server, arguments);
};
/**
 * @param callback {function}
 * @param callback
 */
app.run = function (callback) {
    if (typeof this.config.port === 'number') {
        this.listen(this.config.port,callback(this.config.port));
    }else{
        console.error('If you want use listen, you need config port');
    }
};
/**
 * enable middleware
 * @param config {object}
 * @param key {string}
 * @returns {boolean}
 * default return true
 */
function enable(config, key) {
    return !(typeof config === 'object' && typeof key === 'string' && config[key] && config[key]['enable'] === false);
}

/**
 * Module exports.
 */
module.exports = Application;

@@ -672,24 +672,69 @@
 nction () %7B%0A
+    this.use(configMiddleware(this.config));%0A
     if (enab
@@ -1334,16 +1334,17 @@
 ig.port,
+ 
 callback
@@ -1373,12 +1373,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -1416,21 +1416,22 @@
 ant use 
-liste
+app.ru
 n, you n
@@ -1734,16 +1734,139 @@
 se);%0A%7D%0A%0A
+function configMiddleware(config)%7B%0A    return function *(next)%7B%0A        this.config = config;%0A        yield next;%0A    %7D%0A%7D%0A%0A
 /**%0A * M

add config middleware