"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const koa_router_1 = __importDefault(require("koa-router"));
const koa_bodyparser_1 = __importDefault(require("koa-bodyparser"));
const send = require('koa-send');
const screenshot_1 = require("./controllers/screenshot");
const thumbnail_1 = require("./controllers/thumbnail");
const headless_1 = require("./controllers/headless");
const screencast_1 = require("./controllers/screencast");
const auth_1 = require("./controllers/auth");
// import { authMiddleware } from './middlewares/auth-middleware'
const MAX_AGE = 7 * 24 * 3600 * 1000;
exports.routes = new koa_router_1.default();
const bodyParserOption = {
    formLimit: '10mb',
    jsonLimit: '10mb',
    textLimit: '10mb'
};
// Send index.html when the user access the web
const path = require('path');
const root = path.join(__dirname, '..');
// routes.use('/', authMiddleware)
// for authentication
exports.routes.post('/signup', koa_bodyparser_1.default(bodyParserOption), async (context, next) => {
    try {
        let user = context.request.body;
        let token = await auth_1.signup(user);
        context.body = {
            message: 'registered successfully',
            token
        };
        context.cookies.set('access_token', token, {
            httpOnly: false,
            maxAge: MAX_AGE
        });
    }
    catch (e) {
        context.status = 401;
        context.body = {
            message: e.message
        };
    }
});
exports.routes.post('/signin', koa_bodyparser_1.default(bodyParserOption), async (context, next) => {
    try {
        let user = context.request.body;
        let token = await auth_1.signin(user);
        context.body = {
            message: 'signin successfully',
            token
        };
        context.cookies.set('access_token', token, {
            httpOnly: false,
            maxAge: MAX_AGE
        });
    }
    catch (e) {
        context.status = 401;
        context.body = {
            message: e.message
        };
    }
});
exports.routes.get('/authcheck', async (context, next) => {
    try {
        // 새로운 토큰 발급
        var token = await auth_1.authcheck(context.state.user.email);
        context.cookies.set('access_token', token, {
            httpOnly: false,
            maxAge: MAX_AGE
        });
        context.body = {
            message: 'token checked successfully',
            token,
            user: context.state.user // jwt-koa or authMiddleware will set context.state.token, user
        };
    }
    catch (e) {
        context.status = 401;
        context.body = {
            message: e.message
        };
    }
});
// for board view only
exports.routes.post('/headless-board-view', koa_bodyparser_1.default(bodyParserOption), async (context, next) => {
    let model = context.request.body;
    await context.render('headless-board-view', { model });
});
// for board headless
exports.routes.get('/headless/:id', async (context, next) => {
    let id = context.params.id;
    let model = await headless_1.headless({ id });
    await context.render('headless-board-view', { model });
});
// for board screencast
exports.routes.get('/screencast/:id', async (context, next) => {
    let id = context.params.id;
    await screencast_1.screencast({ id });
    await send(context, 'output.webm', { root });
});
// for board thumbnail
exports.routes.get('/thumbnail/:id', async (context, next) => {
    let id = context.params.id;
    context.type = 'image/png';
    context.body = await thumbnail_1.thumbnail({ id, encoding: 'binary' });
});
// for webpage scrap
exports.routes.get('/screenshot/:id', async (context, next) => {
    let id = context.params.id;
    context.type = 'image/png';
    context.body = await screenshot_1.screenshot({ id, encoding: 'binary' });
});
// for providing resource
exports.routes.get('/file/:file', async (context, next) => {
    await send(context, context.params.file, { root: process.env.UPLOAD_DIR });
});
exports.routes
    .get('/', async (context, next) => {
    await send(context, 'index.html', { root });
})
    .get('/:page', async (context, next) => {
    await send(context, 'index.html', { root });
})
    .get('/:page/:id', async (context, next) => {
    await send(context, 'index.html', { root });
});
//# sourceMappingURL=routes.js.map