const Koa = require("koa");
const app = new Koa();
const path = require("path");
const json = require("koa-json");
const bodyparser = require("koa-bodyparser");
const render = require("koa-art-template");
const core = require("@serverless-devs/core");
const index = require("./routes/index");
app.use(require("koa-static")(__dirname + "/public"));

const argv = core.minimist(process.argv.slice(2));
const port = argv.port || 3001;

// middlewares
app.use(
  bodyparser({
    enableTypes: ["json", "form", "text"],
  })
);
app.use(json());

app.use(async (ctx, next) => {
  ctx.body = ctx.request.body;
  await next();
});

render(app, {
  root: path.join(__dirname, "views"),
  extname: ".art",
});
// routes
app.use(index.routes(), index.allowedMethods());

// app.listen(port, () => {
//   console.log(`server start at http://localhost:${port}`);
//   open(`http://localhost:${port}`);
// });

module.exports = app;
