#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const Path = tslib_1.__importStar(require("path"));
require("villa/platform/node");
const hapi_1 = require("@hapi/hapi");
const inert_1 = tslib_1.__importDefault(require("@hapi/inert"));
const boring_cache_1 = require("boring-cache");
const main_function_1 = require("main-function");
const shared_1 = require("../../bld/shared");
const _core_1 = require("./@core");
// tslint:disable-next-line: no-var-requires no-require-imports
const { version } = require('../../package.json');
const LOG_AUTO_REFRESH_LENGTH_LIMIT = 10000;
const config = new _core_1.Config('remote-workspace.config.json');
const storage = new boring_cache_1.BoringCache('.remote-workspace.json');
const daemon = new _core_1.Daemon(config, storage);
main_function_1.main(async () => {
    const apiServer = new hapi_1.Server({
        host: config.host,
        port: config.port,
        routes: {
            files: {
                relativeTo: Path.join(__dirname, '../../bld/client'),
            },
        },
    });
    await apiServer.register(inert_1.default);
    apiServer.route({
        method: 'GET',
        path: '/api/server-version',
        handler() {
            return {
                data: version,
            };
        },
    });
    apiServer.route({
        method: 'GET',
        path: '/workspaces/{id}/log',
        async handler({ params: { id } }, toolkit) {
            try {
                let ready = await daemon.isWorkspaceReady(id);
                let log = await daemon.retrieveWorkspaceLog(id);
                let refreshHTML = ready
                    ? ''
                    : log.length < LOG_AUTO_REFRESH_LENGTH_LIMIT
                        ? '<meta http-equiv="refresh" content="10" />'
                        : '<div>Log too long, auto refresh disabled.</div>';
                return `${refreshHTML}<pre>${log
                    .replace(/</g, '&lt;')
                    .replace(/>/g, '&gt;')}</pre>`;
            }
            catch (_a) {
                return toolkit.response('Page not found').code(404);
            }
        },
    });
    apiServer.route({
        method: 'GET',
        path: '/api/templates',
        handler() {
            return {
                data: config.templates,
            };
        },
    });
    apiServer.route({
        method: 'GET',
        path: '/api/workspaces',
        async handler() {
            return {
                data: await daemon.getWorkspaceStatuses(),
            };
        },
    });
    apiServer.route({
        method: 'POST',
        path: '/api/workspaces',
        async handler({ payload }) {
            let id = await daemon.createWorkspace(payload);
            return {
                data: {
                    id,
                },
            };
        },
    });
    apiServer.route({
        method: 'PUT',
        path: '/api/workspaces/{id}',
        async handler({ params: { id }, payload }) {
            await daemon.updateWorkspace({
                id,
                ...payload,
            });
            return {};
        },
    });
    apiServer.route({
        method: 'DELETE',
        path: '/api/workspaces/{id}',
        async handler({ params: { id } }) {
            await daemon.deleteWorkspace(id);
            return {};
        },
    });
    apiServer.route({
        method: 'GET',
        path: '/static/{param*}',
        handler: {
            directory: {
                path: '.',
            },
        },
    });
    apiServer.route({
        method: 'GET',
        path: '/{param*}',
        handler({}, toolkit) {
            return toolkit.file(Path.join(__dirname, '../../bld/client/index.html'));
        },
    });
    await apiServer.start();
    console.info(`Listening on "${config.host}:${config.port}"...`);
    return shared_1.NEVER;
});
//# sourceMappingURL=main.js.map