58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import dotenv from 'dotenv';
|
|
import authRoutes from './routes/auth.js';
|
|
import userRoutes from './routes/users.js';
|
|
import departmentRoutes from './routes/departments.js';
|
|
import workAllocationRoutes from './routes/work-allocations.js';
|
|
import attendanceRoutes from './routes/attendance.js';
|
|
import contractorRateRoutes from './routes/contractor-rates.js';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Request logging
|
|
app.use((req, res, next) => {
|
|
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
|
|
next();
|
|
});
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
// API Routes
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/users', userRoutes);
|
|
app.use('/api/departments', departmentRoutes);
|
|
app.use('/api/work-allocations', workAllocationRoutes);
|
|
app.use('/api/attendance', attendanceRoutes);
|
|
app.use('/api/contractor-rates', contractorRateRoutes);
|
|
|
|
// Error handling middleware
|
|
app.use((err, req, res, next) => {
|
|
console.error('Error:', err);
|
|
res.status(err.status || 500).json({
|
|
error: err.message || 'Internal server error'
|
|
});
|
|
});
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
res.status(404).json({ error: 'Route not found' });
|
|
});
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 Server running on http://localhost:${PORT}`);
|
|
console.log(`📊 Health check: http://localhost:${PORT}/health`);
|
|
});
|