#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const service_common_1 = require("@crosslab/service-common");
const logging_1 = require("@crosslab/service-common/logging");
const cookie_parser_1 = __importDefault(require("cookie-parser"));
const express_1 = __importDefault(require("express"));
const express_async_handler_1 = __importDefault(require("express-async-handler"));
const nunjucks_1 = __importDefault(require("nunjucks"));
const path_1 = __importDefault(require("path"));
const admin_1 = require("./admin");
const auth_1 = require("./auth");
const config_1 = require("./config");
const experiment_1 = require("./experiment");
const lti_1 = require("./lti");
const thk_1 = require("./thk");
const utils_1 = require("./utils");
service_common_1.logging.init();
const content_path = config_1.config.NODE_ENV === "development"
    ? "src/content"
    : path_1.default.dirname(__filename) + "/content";
const nunjucks_configuration = {
    autoescape: true,
    noCache: config_1.config.NODE_ENV === "development",
};
const languages = ["en", "de"];
const renderPage = (0, utils_1.renderPageInit)(content_path, config_1.config.DEFAULT_LANGUAGE);
const app = (0, express_1.default)();
app.set("etag", config_1.config.NODE_ENV !== "development");
app.use((_req, res, next) => {
    res.removeHeader("X-Powered-By");
    next();
});
app.use(express_1.default.urlencoded({ extended: true }));
app.use((0, cookie_parser_1.default)());
nunjucks_1.default.configure(content_path + "/templates", {
    ...nunjucks_configuration,
    express: app,
});
app.use("/img", express_1.default.static(path_1.default.join(content_path, "img")));
app.use("/js", express_1.default.static(path_1.default.join(content_path, "js")));
if (config_1.config.NODE_ENV === "development") {
    // When developing, we dynamically transform the css files with postcss
    const { postcss_transform } = require("./debug_utils");
    app.use("/css", postcss_transform(path_1.default.join(content_path, "css")));
}
else {
    // For production, we use precompiled css files
    app.use("/css", express_1.default.static(path_1.default.join(content_path, "css")));
}
app.use((0, auth_1.auth_router)());
for (const language of languages) {
    app.post("/" + language + "/login.html", (0, express_async_handler_1.default)(auth_1.handle_login));
    app.get("/" + language + "/logout.html", (0, express_async_handler_1.default)(auth_1.handle_logout));
    app.use("/" + language + "/admin/", (0, admin_1.admin_router)(language, renderPage, logging_1.logger));
    app.use("/" + language + "/experiment/", (0, experiment_1.experiment_router)(language, renderPage, logging_1.logger));
    app.use("/" + language + "/", (0, thk_1.thk_router)(language, renderPage, logging_1.logger));
    app.use("/" + language + "/", (0, lti_1.lti_router)(language, renderPage));
    app.get("/" + language + "/", (0, express_async_handler_1.default)(async (req, res) => {
        await renderPage("index", language, res, req.user);
    }));
    app.use("/" + language + "/", (0, express_async_handler_1.default)(async (req, res) => {
        await renderPage(req.path, language, res, req.user);
    }));
}
app.use("/", function (req, res) {
    const selected_language = req.acceptsLanguages(languages) || config_1.config.DEFAULT_LANGUAGE;
    res.redirect(307, "/" + selected_language + req.originalUrl);
});
app.use(service_common_1.error.middleware);
if (config_1.config.NODE_ENV === "development") {
    // When developing, we start a browserSync server after listen
    const { start_browserSync } = require("./debug_utils");
    const server = app.listen(() => start_browserSync(server.address().port));
}
else {
    // Just listen on the configured port
    app.listen(config_1.config.PORT);
}
//# sourceMappingURL=index.js.map