ucs/backend/src/utils.mjs
2025-03-15 12:24:31 +02:00

49 lines
1.0 KiB
JavaScript

import RateLimit from 'express-rate-limit';
import config from './config.mjs';
// Constants
const domain = process.env.DOMAIN || config.config.domain || "localhost";
const port = process.env.PORT || config.config.port || 8080;
const db_path = process.env.DB || config.config.db_path || "ucs.db";
const cors = (function() {
let origins = new Set();
for (const site in config.config.sites) {
config.config.sites[site].cors.forEach(origin => {origins.add(origin) });
}
return Array.from(origins);
})();
// functions
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
// limiters
const limiter = limit => RateLimit({
windowMs: 1 * 60 * 1000,
max: limit,
});
export default {
domain,
port,
db_path,
cors,
escapeHtml,
get_limiter: limiter(config.config.limits_per_second.get),
post_limiter: limiter(config.config.limits_per_second.post),
};