-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
89 lines (78 loc) · 2.42 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
// Import dependencies
const SupabaseSIRS = require('./src/infrastructure/database/vercel/SupabaseSIRS');
const SIRSService = require('./src/application/services/SIRSService');
const SIRSController = require('./src/presentation/controllers/SIRSController');
const createSIRSRouter = require('./src/presentation/routes/sirsRoutes');
const { exportToHL7, exportToFHIR } = require('./src/utils/dataExport');
// Initialize application
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'client')));
// Initialize repository and service instances
let sirsRepository;
let sirsService;
let sirsController;
// Initialize repository
async function initialize() {
try {
if (!sirsRepository) {
sirsRepository = new SupabaseSIRS();
await sirsRepository.initialize();
sirsService = new SIRSService(sirsRepository);
sirsController = new SIRSController(sirsService);
console.log('Repository initialized successfully');
}
} catch (error) {
console.error('Initialization error:', error);
throw error;
}
}
// Middleware to ensure initialization
app.use(async (req, res, next) => {
try {
if (!sirsRepository) {
await initialize();
}
next();
} catch (error) {
console.error('Middleware initialization error:', error);
res.status(500).json({
success: false,
error: 'Server initialization failed'
});
}
});
// Routes
app.use('/api/sirs', (req, res, next) => {
if (!sirsController) {
return res.status(500).json({
success: false,
error: 'Server not initialized'
});
}
return createSIRSRouter(sirsController)(req, res, next);
});
// Serve static files
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'index.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Global error handler:', err);
res.status(500).json({
success: false,
error: 'Internal server error'
});
});
// Initialize on startup for development
if (process.env.NODE_ENV !== 'production') {
initialize().catch(console.error);
}
// Export for serverless
module.exports = app;