allow configuring CORS origins

This commit is contained in:
2026-09-06 18:16:27 +03:00
parent c1e542f634
commit 089f5c36f0
2 changed files with 20 additions and 6 deletions
+14 -6
View File
@@ -73,6 +73,7 @@ const CA_BUNDLE = [...tls.rootCertificates, EXTRA_CA];
const PORT = process.env.PORT || 3001;
const ILAN_BASE = 'https://www.ilan.gov.tr';
const CORS_ORIGINS = String(process.env.CORS_ORIGINS || '').split(',').map(value => value.trim()).filter(Boolean);
function validateHealthPortfolio(value) {
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('Firma profili geçersiz.');
@@ -621,13 +622,16 @@ async function getDetail(ilanId) {
// HTTP sunucu
// ---------------------------------------------------------------------------
function json(res, code, body) {
res.writeHead(code, {
const headers = {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'no-referrer',
});
};
const origin = res.__ihaleistCorsOrigin;
if (origin) headers['Access-Control-Allow-Origin'] = origin;
else if (!CORS_ORIGINS.length) headers['Access-Control-Allow-Origin'] = '*';
res.writeHead(code, headers);
res.end(JSON.stringify(body));
}
@@ -732,13 +736,17 @@ const aiJobRunner = startAiJobRunner(async (job,{signal}) => {
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://localhost:${PORT}`);
const requestOrigin = String(req.headers.origin || '').trim();
res.__ihaleistCorsOrigin = CORS_ORIGINS.length && CORS_ORIGINS.includes(requestOrigin) ? requestOrigin : null;
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'Access-Control-Allow-Origin': '*',
const headers = {
'Access-Control-Allow-Methods': 'GET,POST,PUT,PATCH,DELETE,OPTIONS',
'Access-Control-Allow-Headers': 'Authorization,Content-Type',
'Access-Control-Max-Age': '600',
});
};
if (res.__ihaleistCorsOrigin) headers['Access-Control-Allow-Origin'] = res.__ihaleistCorsOrigin;
else if (!CORS_ORIGINS.length) headers['Access-Control-Allow-Origin'] = '*';
res.writeHead(204, headers);
res.end();
return;
}