forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
204 lines (165 loc) · 5.52 KB
/
Copy pathserver.js
File metadata and controls
204 lines (165 loc) · 5.52 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const path = require('path');
// Import configuration
const {
REQUIRED_ENV_VARS,
MAX_REQUESTS_PER_MINUTE,
RATE_LIMIT_WINDOW_MS,
PERFORMANCE_MONITORING_ENABLED,
PERFORMANCE_LOG_INTERVAL
} = require('./email/src/config/constants');
// Import services
const { checkSupabaseConnection, getAuthenticatedUser } = require('./email/src/services/authService');
const { initializeDatabase } = require('./email/src/utils/dbUtils');
const { closeRedisConnection } = require('./email/src/utils/redisUtils');
const { draftEmail: sendEmailWithGmail } = require('./email/src/utils/emailUtils');
// Import routes
const authRouter = require('./email/src/routes/auth');
const emailRouter = require('./email/src/routes/email');
const deckRouter = require('./deck/src/routes/deck');
const app = express();
const PORT = process.env.PORT || 8080;
// Validate required environment variables
for (const envVar of REQUIRED_ENV_VARS) {
if (!process.env[envVar]) {
console.error(`Error: ${envVar} environment variable is required`);
process.exit(1);
}
}
// Enable trust proxy
app.set('trust proxy', 1);
// Middleware
app.use(helmet());
app.use(cors());
app.use(compression());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Shared services used by route modules.
app.locals.getAuthenticatedUser = getAuthenticatedUser;
app.locals.sendEmail = sendEmailWithGmail;
// Rate limiting middleware
const requestCounts = new Map();
app.use((req, res, next) => {
const now = Date.now();
const clientIp = req.ip;
// Clean up expired entries
for (const [ip, data] of requestCounts.entries()) {
if (now - data.windowStart > RATE_LIMIT_WINDOW_MS) {
requestCounts.delete(ip);
}
}
// Initialize or get client data
if (!requestCounts.has(clientIp)) {
requestCounts.set(clientIp, {
count: 0,
windowStart: now
});
}
const clientData = requestCounts.get(clientIp);
// Reset window if needed
if (now - clientData.windowStart > RATE_LIMIT_WINDOW_MS) {
clientData.count = 0;
clientData.windowStart = now;
}
// Increment count and check limits
clientData.count++;
if (clientData.count > MAX_REQUESTS_PER_MINUTE) {
console.warn(`Rate limit exceeded for IP: ${clientIp}`);
return res.status(429).json({
error: 'rate_limit_exceeded',
message: 'Too many requests, please try again later.'
});
}
next();
});
// Routes
app.use('/api/auth', authRouter);
app.use('/api/email', emailRouter);
app.use('/api/deck', deckRouter);
// App routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/email', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'email.html'));
});
app.get('/deck', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'deck.html'));
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
// Initialize database and start server
async function startServer() {
try {
// Initialize Supabase tables
await initializeDatabase();
// Check Supabase connection
await checkSupabaseConnection();
// Ensure Redis is connected
try {
const { initializeRedis } = require('./email/src/utils/redisUtils');
const redisConnected = await initializeRedis();
if (redisConnected) {
console.log('Redis initialized successfully');
} else {
console.warn('Redis initialization failed, some features may not work properly');
}
} catch (redisError) {
console.error('Failed to initialize Redis:', redisError);
console.warn('Continuing without Redis, some features may not work properly');
}
// Start HTTP server
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
}).on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Please try a different port or kill the process using this port.`);
process.exit(1);
} else {
console.error('Failed to start server:', err);
process.exit(1);
}
});
// Graceful shutdown handler
const shutdown = async () => {
console.log('\nGracefully shutting down...');
// Close server first to stop accepting new connections
server.close(() => {
console.log('Server closed');
});
try {
// Close Redis connections
await closeRedisConnection();
console.log('Redis connection closed');
// Exit process
process.exit(0);
} catch (error) {
console.error('Error during shutdown:', error);
process.exit(1);
}
};
// Handle shutdown signals
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
return server;
} catch (error) {
console.error('Failed to start server:', error);
process.exit(1);
}
}
// Start the server
startServer().catch(error => {
console.error('Unhandled error during server startup:', error);
process.exit(1);
});
module.exports = app;