2025-10-24 20:27:18 +02:00
|
|
|
import express from 'express';
|
|
|
|
|
import cors from 'cors';
|
2026-08-01 12:51:02 +02:00
|
|
|
import cookieParser from 'cookie-parser';
|
2025-10-24 20:27:18 +02:00
|
|
|
import dotenv from 'dotenv';
|
2025-10-25 17:02:42 +02:00
|
|
|
import { initializeDatabase } from './db';
|
2026-08-01 12:51:02 +02:00
|
|
|
import { requireApiToken } from './middleware/auth';
|
2025-11-13 18:10:50 +01:00
|
|
|
import { deviceIdMiddleware } from './middleware/device';
|
2026-08-01 12:51:02 +02:00
|
|
|
import authRoutes from './routes/authRoutes';
|
2025-10-24 20:27:18 +02:00
|
|
|
import listRoutes from './routes/listRoutes';
|
|
|
|
|
import fieldRoutes from './routes/fieldRoutes';
|
|
|
|
|
import itemRoutes from './routes/itemRoutes';
|
|
|
|
|
import syncRoutes from './routes/syncRoutes';
|
|
|
|
|
import webRoutes from './routes/webRoutes';
|
2025-11-13 18:10:50 +01:00
|
|
|
import notificationRoutes from './routes/notificationRoutes';
|
|
|
|
|
import { startNotificationCleanup } from './notifications';
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
const PORT = process.env.PORT || 3000;
|
2026-06-03 17:09:31 +02:00
|
|
|
const DB_INIT_RETRY_INTERVAL_MS = Math.max(250, Number(process.env.DB_INIT_RETRY_INTERVAL_MS || 5000));
|
|
|
|
|
const DB_INIT_RETRY_MAX_ATTEMPTS = Math.max(0, Number(process.env.DB_INIT_RETRY_MAX_ATTEMPTS || 0));
|
|
|
|
|
|
|
|
|
|
function sleep(ms: number) {
|
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function initializeDatabaseWithRetry() {
|
|
|
|
|
let attempt = 0;
|
|
|
|
|
|
2026-06-03 17:30:24 +02:00
|
|
|
for (;;) {
|
2026-06-03 17:09:31 +02:00
|
|
|
attempt += 1;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await initializeDatabase();
|
|
|
|
|
if (attempt > 1) {
|
|
|
|
|
console.log(`[DB] Connected after ${attempt} attempt(s)`);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const reachedLimit = DB_INIT_RETRY_MAX_ATTEMPTS > 0 && attempt >= DB_INIT_RETRY_MAX_ATTEMPTS;
|
|
|
|
|
|
|
|
|
|
console.error(`[DB] Initialization attempt ${attempt} failed`, error);
|
|
|
|
|
|
|
|
|
|
if (reachedLimit) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`[DB] Retrying in ${DB_INIT_RETRY_INTERVAL_MS}ms...`);
|
|
|
|
|
await sleep(DB_INIT_RETRY_INTERVAL_MS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
// Middleware
|
2026-08-01 12:51:02 +02:00
|
|
|
app.use(cors({ credentials: true, origin: true }));
|
2025-10-24 20:27:18 +02:00
|
|
|
app.use(express.json());
|
2026-08-01 12:51:02 +02:00
|
|
|
app.use(cookieParser());
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
// Health check (no auth required)
|
|
|
|
|
app.get('/health', (req, res) => {
|
|
|
|
|
res.json({ status: 'ok', timestamp: Date.now() });
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-01 12:51:02 +02:00
|
|
|
// Auth bootstrap routes (status/setup/login/logout/credentials/token) - must stay
|
|
|
|
|
// reachable without a token or session, mounted before the API-token gate below.
|
|
|
|
|
app.use('/api', authRoutes);
|
|
|
|
|
|
|
|
|
|
// Sync/collaboration API: gated by a long-lived API token shared by every collaborating
|
|
|
|
|
// device (see middleware/auth.ts) - this replaces the old optional SERVER_PASSWORD scheme,
|
|
|
|
|
// which silently disabled auth entirely when unset.
|
|
|
|
|
app.use('/api', requireApiToken);
|
2025-11-13 18:10:50 +01:00
|
|
|
// Attach device id for downstream routes
|
|
|
|
|
app.use('/api', deviceIdMiddleware);
|
2025-10-24 20:27:18 +02:00
|
|
|
|
2026-08-01 12:51:02 +02:00
|
|
|
// Routes (protected by the API token above)
|
2025-10-24 20:27:18 +02:00
|
|
|
app.use('/api/lists', listRoutes);
|
|
|
|
|
app.use('/api/fields', fieldRoutes);
|
|
|
|
|
app.use('/api/items', itemRoutes);
|
|
|
|
|
app.use('/api', syncRoutes);
|
2025-11-13 18:10:50 +01:00
|
|
|
app.use('/api', notificationRoutes);
|
2025-10-24 20:27:18 +02:00
|
|
|
|
2026-08-01 12:51:02 +02:00
|
|
|
// Web UI (static SPA shell is public; its /web/info and /web/data JSON endpoints require
|
|
|
|
|
// the admin session - see webRoutes.ts. Mounted last to not interfere with API routes.)
|
2025-10-24 20:27:18 +02:00
|
|
|
app.use('/', webRoutes);
|
|
|
|
|
|
|
|
|
|
// Initialize database and start server
|
2025-10-25 17:02:42 +02:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
2026-06-03 17:09:31 +02:00
|
|
|
await initializeDatabaseWithRetry();
|
2025-10-25 17:02:42 +02:00
|
|
|
console.log('Database synchronized successfully');
|
|
|
|
|
console.log('Database models loaded:');
|
|
|
|
|
console.log('- Lists');
|
|
|
|
|
console.log('- Fields');
|
|
|
|
|
console.log('- Items');
|
|
|
|
|
console.log('- ItemValues');
|
|
|
|
|
|
2025-11-13 18:10:50 +01:00
|
|
|
// Start retention cleanup for notifications
|
|
|
|
|
startNotificationCleanup();
|
|
|
|
|
|
2025-10-26 15:12:45 +01:00
|
|
|
// Start HTTP server (WebSocket removed)
|
|
|
|
|
app.listen(PORT, () => {
|
2025-10-25 17:02:42 +02:00
|
|
|
console.log(`Server is running on port ${PORT}`);
|
|
|
|
|
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
|
2026-08-01 12:51:02 +02:00
|
|
|
const source = process.env.DATABASE_URL ? 'DATABASE_URL' : 'PGHOST/PGDATABASE';
|
|
|
|
|
console.log(`Database: PostgreSQL (${source})`);
|
2025-10-25 17:02:42 +02:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Database initialization error:', error);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
})();
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
export default app;
|