-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-server.js
More file actions
151 lines (127 loc) · 5.13 KB
/
api-server.js
File metadata and controls
151 lines (127 loc) · 5.13 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
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const EmailService = require('./email-service');
class APIServer {
constructor(port = 3002) {
this.port = port;
this.emailService = new EmailService();
this.server = null;
}
start() {
this.server = http.createServer((req, res) => {
// Enable CORS
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
this.handleRequest(req, res);
});
this.server.listen(this.port, () => {
console.log(`🚀 API Server running on http://localhost:${this.port}`);
});
}
async handleRequest(req, res) {
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
const method = req.method;
try {
// Route handling
if (path === '/api/emails' && method === 'GET') {
await this.getEmails(req, res, parsedUrl.query);
} else if (path === '/api/emails/send' && method === 'POST') {
await this.sendEmail(req, res);
} else if (path === '/api/emails/draft' && method === 'POST') {
await this.saveDraft(req, res);
} else if (path.startsWith('/api/emails/') && method === 'PUT') {
await this.updateEmail(req, res, path);
} else if (path.startsWith('/api/emails/') && method === 'DELETE') {
await this.deleteEmail(req, res, path);
} else if (path === '/api/emails/search' && method === 'GET') {
await this.searchEmails(req, res, parsedUrl.query);
} else if (path === '/api/health' && method === 'GET') {
this.sendResponse(res, 200, { status: 'OK', timestamp: new Date().toISOString() });
} else {
this.sendResponse(res, 404, { error: 'Endpoint not found' });
}
} catch (error) {
console.error('API Error:', error);
this.sendResponse(res, 500, { error: 'Internal server error' });
}
}
async getEmails(req, res, query) {
const folder = query.folder || 'inbox';
const emails = this.emailService.getEmailsFromFolder(folder);
this.sendResponse(res, 200, { emails, folder });
}
async sendEmail(req, res) {
const body = await this.getRequestBody(req);
const emailData = JSON.parse(body);
const result = await this.emailService.sendEmail(emailData);
if (result.success) {
this.sendResponse(res, 200, { message: 'Email sent successfully', messageId: result.messageId });
} else {
this.sendResponse(res, 400, { error: result.error });
}
}
async saveDraft(req, res) {
const body = await this.getRequestBody(req);
const draftData = JSON.parse(body);
const draft = this.emailService.saveDraft(draftData);
this.sendResponse(res, 200, { message: 'Draft saved', draft });
}
async updateEmail(req, res, path) {
const emailId = parseInt(path.split('/').pop());
const body = await this.getRequestBody(req);
const updates = JSON.parse(body);
const updatedEmail = this.emailService.updateEmail(emailId, updates);
if (updatedEmail) {
this.sendResponse(res, 200, { message: 'Email updated', email: updatedEmail });
} else {
this.sendResponse(res, 404, { error: 'Email not found' });
}
}
async deleteEmail(req, res, path) {
const emailId = parseInt(path.split('/').pop());
const body = await this.getRequestBody(req);
const { folder } = JSON.parse(body || '{}');
const deleted = this.emailService.deleteEmail(emailId, folder || 'inbox');
if (deleted) {
this.sendResponse(res, 200, { message: 'Email deleted' });
} else {
this.sendResponse(res, 404, { error: 'Email not found' });
}
}
async searchEmails(req, res, query) {
const searchQuery = query.q || '';
const results = this.emailService.searchEmails(searchQuery);
this.sendResponse(res, 200, { results, query: searchQuery });
}
getRequestBody(req) {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
resolve(body);
});
req.on('error', reject);
});
}
sendResponse(res, statusCode, data) {
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
}
stop() {
if (this.server) {
this.server.close();
}
}
}
module.exports = APIServer;