const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const session = require('koa-session-minimal');
const MysqlSession = require('koa-mysql-session');
const static = require('koa-static');
const path = require('path');
const sys = require('./config/sys.json');
const router = require('./router');

const app = new Koa();
const store = new MysqlSession({
  connectTimeout  : 60* 60 * 60 * 1000,
  host: sys.session.host,
  user: sys.session.user,
  password: sys.session.pwd,
  database: sys.session.db
});
const cookie = {
  maxAge: sys.session.max_age,
  path: sys.session.path,
  domain: sys.session.domain,
  httpOnly: sys.session.http_only
}


app.use(bodyParser({
  "formLimit": "50mb",
  "jsonLimit": "50mb",
  "textLimit": "50mb"
}));
app.use(session({
  key: sys.session.key,
  store: store,
  cookie: cookie
}));
app.use(router.routes());
app.use(router.allowedMethods());
app.use(static(path.join(__dirname, sys.static_dir), {handle: true}));

app.listen(sys.port, () => {
    console.log(`${sys.pro_name} is starting at port ${sys.port}`);
});
