-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
53 lines (45 loc) · 1.47 KB
/
server.ts
File metadata and controls
53 lines (45 loc) · 1.47 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
import express, { Request, Response } from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { EmailService } from './src/EmailService';
import { MockProviderA } from './src/providers/MockProviderA';
import { MockProviderB } from './src/providers/MockProviderB';
import path from 'path';
// Initialize email service
const providerA = new MockProviderA(0.3);
const providerB = new MockProviderB(0.1);
const emailService = new EmailService(providerA, providerB, 10, 60000, 3, 1000);
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// API Endpoint
app.post('/api/send-emails', async (req: Request, res: Response) => {
try {
const emails = req.body;
const results = [];
for (const email of emails) {
const result = await emailService.sendEmail({
...email,
idempotencyKey: `${email.to}-${Date.now()}`
});
results.push(result);
}
res.json(results);
} catch (error: unknown) {
if (error instanceof Error) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'An unknown error occurred' });
}
}
});
// Serve frontend
app.get('*', (req: Request, res: Response) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});