'use strict';

var path = require('path');
var fs = require('fs');
var koa = require('koa');
var serve = require('koa-serve');
var serveStatic = require('koa-serve-static');
var websockify = require('koa-websocket');
var app = websockify(koa());
var wsRouter = require('./router/websocket');
var httpRouter = require('./router/http');
var rootpath = path.join(__dirname, '../frontend/');
var Config = require('./components/Config');
/* 
 ===================================
 WebSocket Router
 ===================================
 */
module.exports = function (port) {

    app.ws.use(wsRouter.routes());
    app.on('error', function (err, ctx) {
        if (Config.verbose) {
            if (err.status == 404) {
                console.log('404');
            } else {
                console.error(err);
            }
        }
    });
    /*
     ===================================
     Http Router
     ===================================
     */

    app.use(httpRouter.routes());
    app.use(serveStatic(rootpath));

    app.listen(port);
};