import { existsSync } from 'node:fs'; import path from 'node:path'; import cookieParser from 'cookie-parser'; import express from 'express'; import { aiRouter } from './ai.js'; import { authRouter, requireSameOrigin, requireUser } from './auth.js'; import { citationsRouter } from './citations.js'; import { compileRouter, sandbox } from './compile.js'; import { APP_NAME, HOST, PORT, ROOT } from './config.js'; import { historyRouter } from './history.js'; import { giydirRouter } from './giydir.js'; import { toolsRouter } from './tools.js'; import { projectsRouter } from './projects.js'; import { HttpError } from './util.js'; const app = express(); app.set('trust proxy', 1); app.use(express.json({ limit: '10mb' })); app.use(cookieParser()); // Oturum gerektirmeyen durum ucu: sunucunun ve derleme kafesinin çalışıp çalışmadığını gösterir. app.get('/api/saglik', (_req, res) => res.json({ ok: true, derlemeYalitimi: sandbox })); app.use('/api', requireSameOrigin); app.use('/api/auth', authRouter); app.use('/api', requireUser, projectsRouter, compileRouter, historyRouter, toolsRouter, aiRouter, citationsRouter, giydirRouter); app.use('/api', () => { throw new HttpError(404, 'Böyle bir API ucu yok.'); }); // Üretimde derlenmiş arayüz (web/dist) aynı sunucudan sunulur. const dist = path.join(ROOT, 'web', 'dist'); if (existsSync(dist)) { app.use(express.static(dist)); app.use((_req, res) => res.sendFile(path.join(dist, 'index.html'))); } app.use((err, _req, res, _next) => { if (res.headersSent) return res.end(); if (err instanceof HttpError) return res.status(err.status).json({ error: err.message, ...err.extra }); if (err.code === 'LIMIT_FILE_SIZE') return res.status(413).json({ error: 'Dosya çok büyük.' }); if (err.type === 'entity.too.large') return res.status(413).json({ error: 'İçerik çok büyük.' }); console.error(err); res.status(500).json({ error: 'Sunucuda beklenmeyen bir hata oluştu.' }); }); app.listen(PORT, HOST, () => { console.log(`${APP_NAME} sunucusu çalışıyor: http://${HOST}:${PORT}`); if (sandbox.mode === 'none' && !['127.0.0.1', 'localhost', '::1'].includes(HOST)) { console.warn('UYARI: Sunucu ağa açık, ancak derleme yalıtılmamış. Başkalarına açmadan önce README\'deki güvenlik uyarısını okuyun.'); } });