"use strict";

var path = require("path");
var Koa = require("koa");
var serve = require("koa-static");
var Websockify = require("koa-websocket");
var bodyParser = require("koa-bodyparser");
var WsRouter = require("./router/websocket");
var HttpRouter = require("./router/http");
var app = Websockify(new Koa());
var rootPath = path.join(__dirname, "../../frontend/");

var _require = require("../link/setup"),
    setup = _require.setup;

var _require2 = require("../util"),
    logger = _require2.logger;

exports.start = function (port, cb) {
  setup();
  app.use(bodyParser());
  app.ws.use(WsRouter.routes()).use(WsRouter.allowedMethods());
  app.use(HttpRouter.routes());
  app.use(serve(rootPath));

  app.on("error", function (err, ctx) {
    logger.error(err);
  });

  app.listen(port, cb);
};