sdfsd
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
import express from 'express';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import path from 'node:path';
|
||||
import cors from 'cors';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { config } from './config.js';
|
||||
import { initDb } from './db.js';
|
||||
import { authRouter } from './routes/auth.js';
|
||||
import { domainsRouter } from './routes/domains.js';
|
||||
import { mailboxesRouter } from './routes/mailboxes.js';
|
||||
import { auditRouter } from './routes/audit.js';
|
||||
import { SyncService } from './services/sync.js';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const app = express();
|
||||
|
||||
app.use(express.json({ limit: '2mb' }));
|
||||
app.use(cors({ origin: true, credentials: true }));
|
||||
app.use(express.json({ limit: '1mb' }));
|
||||
app.use(cookieParser());
|
||||
|
||||
app.use((req, res, next) => {
|
||||
@@ -24,36 +29,50 @@ app.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
app.get('/api/health', (_req, res) => {
|
||||
res.json({
|
||||
ok: true,
|
||||
node: config.nodeName,
|
||||
hostname: config.nodeHostname,
|
||||
});
|
||||
});
|
||||
|
||||
app.use('/api/auth', authRouter);
|
||||
app.use('/api/domains', domainsRouter);
|
||||
app.use('/api/mailboxes', mailboxesRouter);
|
||||
app.use('/api/audit', auditRouter);
|
||||
|
||||
const currentFile = fileURLToPath(import.meta.url);
|
||||
const currentDir = path.dirname(currentFile);
|
||||
const publicDir = path.resolve(currentDir, '../public');
|
||||
|
||||
app.use(express.static(publicDir));
|
||||
|
||||
app.get('*', (_req, res) => {
|
||||
res.sendFile(path.join(publicDir, 'index.html'));
|
||||
});
|
||||
|
||||
app.use((err: any, req: express.Request, res: express.Response, _next: express.NextFunction) => {
|
||||
const status = err.status || err.statusCode || 500;
|
||||
const status = err.status ?? err.statusCode ?? 500;
|
||||
|
||||
console.error(`[error] ${req.method} ${req.originalUrl}`);
|
||||
console.error(err?.stack || err?.message || err);
|
||||
|
||||
res.status(status).json({
|
||||
error: err?.message || 'Internal server error',
|
||||
error: err.message ?? 'Internal server error',
|
||||
});
|
||||
});
|
||||
|
||||
const publicDir = config.publicDir.startsWith('/')
|
||||
? config.publicDir
|
||||
: resolve(__dirname, config.publicDir);
|
||||
|
||||
console.log(`Serving frontend from: ${publicDir}`);
|
||||
|
||||
app.use(express.static(publicDir));
|
||||
|
||||
app.get('*', (_req, res) => {
|
||||
res.sendFile(resolve(publicDir, 'index.html'));
|
||||
});
|
||||
|
||||
await initDb();
|
||||
|
||||
const port = Number(process.env.PORT || 3000);
|
||||
try {
|
||||
await new SyncService().syncFromDms();
|
||||
} catch (err) {
|
||||
console.warn('Initial DMS sync failed. The app still starts:', err);
|
||||
}
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`mailadmin listening on ${port} for ${process.env.NODE_NAME || 'unknown-node'}`);
|
||||
});
|
||||
app.listen(config.port, () => {
|
||||
console.log(`mailadmin listening on ${config.port} for ${config.nodeName}`);
|
||||
});
|
||||
Reference in New Issue
Block a user