2025-10-24 20:27:18 +02:00
|
|
|
import express from 'express';
|
|
|
|
|
import cors from 'cors';
|
|
|
|
|
import dotenv from 'dotenv';
|
2025-10-25 17:02:42 +02:00
|
|
|
import { initializeDatabase } from './db';
|
2025-10-24 20:27:18 +02:00
|
|
|
import { authenticatePassword } from './middleware/auth';
|
2025-11-13 18:10:50 +01:00
|
|
|
import { deviceIdMiddleware } from './middleware/device';
|
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;
|
|
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
|
app.use(cors());
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
|
|
// Health check (no auth required)
|
|
|
|
|
app.get('/health', (req, res) => {
|
|
|
|
|
res.json({ status: 'ok', timestamp: Date.now() });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Apply authentication middleware to all API routes
|
|
|
|
|
app.use('/api', authenticatePassword);
|
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
|
|
|
|
|
|
|
|
// Routes (protected by auth)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Web UI (no auth required, must be last to not interfere with API routes)
|
|
|
|
|
app.use('/', webRoutes);
|
|
|
|
|
|
|
|
|
|
// Initialize database and start server
|
2025-10-25 17:02:42 +02:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
await initializeDatabase();
|
|
|
|
|
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'}`);
|
|
|
|
|
const dbClient = (process.env.DB_CLIENT || process.env.DB_TYPE || 'sqlite').toLowerCase();
|
|
|
|
|
if (dbClient === 'postgres' || dbClient === 'postgresql') {
|
|
|
|
|
const source = process.env.DATABASE_URL ? 'DATABASE_URL' : 'PGHOST/PGDATABASE';
|
|
|
|
|
console.log(`Database: PostgreSQL (${source})`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Database: ${process.env.DB_PATH || './data/collabtable.db'}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Database initialization error:', error);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
})();
|
2025-10-24 20:27:18 +02:00
|
|
|
|
|
|
|
|
export default app;
|