-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
181 lines (160 loc) ยท 6.45 KB
/
server.js
File metadata and controls
181 lines (160 loc) ยท 6.45 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const APIServer = require('./api-server');
const EmailService = require('./email-service');
const config = require('./config');
const PORT = config.web.port;
const API_PORT = config.web.apiPort;
// Start API server
const apiServer = new APIServer(API_PORT);
apiServer.start();
// Start SMTP server
require('./smtp-server');
// Initialize with sample data
const emailService = new EmailService();
initializeSampleData(emailService);
// MIME types for different file extensions
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml',
'.woff': 'font/woff',
'.woff2': 'font/woff2'
};
const server = http.createServer((req, res) => {
console.log(`${req.method} ${req.url}`);
// Parse URL
let filePath = '.' + req.url;
if (filePath === './') {
filePath = './index.html';
}
// Get file extension
const extname = String(path.extname(filePath)).toLowerCase();
const mimeType = mimeTypes[extname] || 'application/octet-stream';
// Read and serve the file
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
// File not found
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - File Not Found</h1>', 'utf-8');
} else {
// Server error
res.writeHead(500);
res.end(`Server Error: ${error.code}`, 'utf-8');
}
} else {
// Success
res.writeHead(200, { 'Content-Type': mimeType });
res.end(content, 'utf-8');
}
});
});
function initializeSampleData(emailService) {
// Check if inbox already has data
const inbox = emailService.getEmailsFromFolder('inbox');
if (inbox.length === 0) {
console.log('๐ Initializing sample email data...');
// Sample emails
const sampleEmails = [
{
id: 1,
from: 'alice@example.com',
to: config.emailAddress,
subject: 'Welcome to CustomMail!',
body: `Hi there,\n\nWelcome to your new self-hosted email service for ${config.domain}! This is a test email to show how the system works.\n\nYou can now:\n- Send and receive emails\n- Organize with folders\n- Search through your messages\n- Attach files\n\nBest regards,\nAlice`,
date: new Date('2024-01-15T10:30:00'),
read: false,
starred: true,
attachments: []
},
{
id: 2,
from: 'bob@company.com',
to: config.emailAddress,
subject: 'Project Update - Q1 2024',
body: 'Hi there,\n\nHere\'s the latest update on our project. Everything is going according to plan and we\'re on track for the Q1 delivery.\n\nKey milestones completed:\n- Backend API development\n- Frontend UI implementation\n- Database schema design\n- Initial testing phase\n\nNext steps:\n- User acceptance testing\n- Performance optimization\n- Security audit\n\nThanks,\nBob',
date: new Date('2024-01-14T14:20:00'),
read: true,
starred: false,
attachments: []
},
{
id: 3,
from: 'newsletter@techblog.com',
to: config.emailAddress,
subject: 'Weekly Tech Newsletter - Serverless Trends',
body: 'This week in tech:\n\n๐ Serverless Computing Advances\n- New AWS Lambda features\n- Edge computing improvements\n- Cost optimization strategies\n\n๐ก AI & Machine Learning\n- Latest ChatGPT updates\n- Open source AI models\n- ML deployment best practices\n\n๐ง Development Tools\n- New VS Code extensions\n- Docker improvements\n- CI/CD pipeline updates\n\nRead more at techblog.com',
date: new Date('2024-01-13T09:00:00'),
read: true,
starred: false,
attachments: []
}
];
// Save sample emails to inbox
sampleEmails.forEach(email => {
emailService.saveEmailToFolder('inbox', email);
});
// Sample sent email
const sentEmail = {
id: 4,
from: config.emailAddress,
to: 'alice@example.com',
subject: 'Re: Welcome to CustomMail!',
body: 'Hi Alice,\n\nThank you for the welcome! The system looks great and I\'m excited to start using it.\n\nThe interface is very intuitive and reminds me of Gmail, which is perfect.\n\nBest regards,\nManager',
date: new Date('2024-01-15T11:00:00'),
read: true,
starred: false,
attachments: []
};
emailService.saveEmailToFolder('sent', sentEmail);
// Sample draft
const draft = {
id: 5,
from: config.emailAddress,
to: 'team@company.com',
subject: 'Meeting Notes - Weekly Standup',
body: 'Hi team,\n\nHere are the notes from today\'s meeting:\n\n## Agenda\n1. Project status updates\n2. Blockers and challenges\n3. Next week\'s priorities\n\n## Action Items\n- [ ] Complete API documentation\n- [ ] Review security requirements\n- [ ] Schedule user testing sessions\n\n[Draft - not sent yet]',
date: new Date('2024-01-15T16:30:00'),
read: true,
starred: false,
attachments: []
};
emailService.saveEmailToFolder('drafts', draft);
console.log('โ
Sample data initialized successfully!');
}
}
server.listen(PORT, () => {
console.log('๐ CustomMail Server Started!');
console.log('================================');
console.log(`๐ Web Server: http://localhost:${PORT}/`);
console.log(`๐ API Server: http://localhost:${API_PORT}/`);
console.log(`๐ฌ SMTP Server: port ${config.smtp.port} (production) and ${config.smtp.altPort} (testing)`);
console.log(`๐ง Domain: ${config.domain}`);
console.log(`๐ฎ Email: ${config.emailAddress}`);
console.log('');
if (config.dns.a.ip === 'YOUR_SERVER_IP') {
console.log('โ ๏ธ SETUP REQUIRED:');
console.log(' 1. Update YOUR_SERVER_IP in config.js');
console.log(' 2. Configure DNS records (see DNS_SETUP_GUIDE.md)');
console.log(' 3. Run: npm run setup');
console.log('');
} else {
console.log('โ
Server configured for production');
console.log(`๐ External access: http://${config.dns.a.ip}:${PORT}/`);
console.log('');
}
console.log('๐ Documentation:');
console.log(' - DNS Setup: DNS_SETUP_GUIDE.md');
console.log(' - Generate DKIM: npm run generate-dkim');
console.log(' - Production Setup: npm run setup');
console.log('');
console.log('Press Ctrl+C to stop all servers');
});