"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startServer = startServer;
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const integration_foundation_1 = require("@dbis/integration-foundation");
const exchange_1 = require("./api/routes/exchange");
const rate_limit_1 = require("./middleware/rate-limit");
function corsOptions() {
    const policy = (0, integration_foundation_1.loadCustomerSecurityPolicy)();
    const origins = policy.corsOrigins.filter(Boolean);
    if (!origins.length)
        return {};
    return {
        origin(origin, cb) {
            if (!origin || origins.includes(origin))
                cb(null, true);
            else
                cb(new Error('CORS not allowed'));
        },
    };
}
function startServer() {
    const app = (0, express_1.default)();
    app.use((0, cors_1.default)(corsOptions()));
    app.use(express_1.default.json());
    app.use('/api/v1/exchange', rate_limit_1.exchangeRateLimiter, (0, exchange_1.createExchangeRouter)());
    const p = (0, exchange_1.exchangePort)();
    app.listen(p, () => {
        console.log(`DBIS Exchange (Pancake-style) on http://localhost:${p}/api/v1/exchange`);
    });
}
