forked from marcelo-m7/open2.tech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
171 lines (146 loc) · 6.14 KB
/
Copy pathserver.js
File metadata and controls
171 lines (146 loc) · 6.14 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
/**
* Open2 - Production Server
* Express server with static file serving and API endpoints
*/
import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config({ path: '.env.local' });
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = process.env.PORT || 8080;
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Logging middleware
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
next();
});
// API Routes
app.all('/api/contact', (req, res, next) => {
if (req.method !== 'POST') {
res.setHeader('Allow', ['POST']);
return res.status(405).json({
error: `Method ${req.method} Not Allowed. Use POST to submit the contact form.`
});
}
next();
});
app.post('/api/contact', async (req, res) => {
const { name, email, tel, company, message } = req.body;
// Validate required fields
if (!name || !email || !message) {
return res.status(400).json({
error: 'Missing required fields: name, email, and message are mandatory.'
});
}
const RESEND_API_KEY = process.env.RESEND_API_KEY;
if (!RESEND_API_KEY) {
console.error('[SERVER ERROR] RESEND_API_KEY environment variable is missing.');
return res.status(500).json({
error: 'The email service is not properly configured on the server.'
});
}
try {
const resendResponse = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${RESEND_API_KEY}`,
},
body: JSON.stringify({
from: 'Open2 <hello@open2.tech>',
to: 'hello@open2.tech',
subject: `New contact form submission — ${name}`,
reply_to: email,
html: `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Inter', sans-serif; color: #05070a; line-height: 1.6; }
.container { max-width: 600px; margin: 0 auto; padding: 40px; border: 4px solid #05070a; background-color: #ffffff; }
.header { border-bottom: 4px solid #8b5cf6; margin-bottom: 30px; padding-bottom: 10px; }
.label { font-size: 10px; font-weight: 900; text-transform: uppercase; letter-spacing: 0.2em; color: #8b5cf6; margin-bottom: 4px; }
.value { font-size: 18px; font-weight: 700; margin-bottom: 24px; color: #05070a; }
.message-box { background-color: #f8fafc; border-left: 4px solid #8b5cf6; padding: 20px; margin-top: 30px; }
.footer { margin-top: 40px; font-size: 10px; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.1em; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1 style="margin: 0; font-size: 24px; text-transform: uppercase; letter-spacing: -0.02em;">New Studio Signal</h1>
</div>
<div>
<div class="label">Name / Identity</div>
<div class="value">${name}</div>
<div class="label">Email Address</div>
<div class="value">${email}</div>
<div class="label">Phone / WhatsApp</div>
<div class="value">${tel || 'Not provided'}</div>
<div class="label">Company / Project</div>
<div class="value">${company || 'Not provided'}</div>
<div class="message-box">
<div class="label" style="color: #8b5cf6;">Message Context</div>
<p style="margin: 0; white-space: pre-wrap; font-size: 16px;">${message}</p>
</div>
</div>
<div class="footer">
© Open2 · Automated Notification · ${new Date().toLocaleString()}
</div>
</div>
</body>
</html>
`,
}),
});
const result = await resendResponse.json();
if (resendResponse.ok) {
console.log(`[EMAIL SENT] Contact form submission from ${name} (${email}) - ID: ${result.id}`);
return res.status(200).json({ success: true, id: result.id });
} else {
console.error('[RESEND ERROR]', result);
return res.status(resendResponse.status).json({
error: result.message || 'The email service rejected the transmission. Please check the logs.'
});
}
} catch (error) {
console.error('[INTERNAL ERROR]', error);
return res.status(500).json({
error: 'A critical error occurred while attempting to deliver the signal.'
});
}
});
// Handle 404 for API routes
app.use('/api/*', (req, res) => {
res.status(404).json({
error: 'API endpoint not found. Check the URL and try again.'
});
});
// Serve static files from the dist directory
app.use(express.static(join(__dirname, 'dist')));
// SPA fallback - serve index.html for all other routes
app.get('*', (req, res) => {
res.sendFile(join(__dirname, 'dist', 'index.html'));
});
// Start server
app.listen(PORT, () => {
console.log(`
╔══════════════════════════════════════════════════════╗
║ Open2 Server ║
║ Running on port ${PORT} ║
║ Environment: ${process.env.NODE_ENV || 'development'} ║
║ API Endpoint: POST /api/contact ║
╚══════════════════════════════════════════════════════╝
`);
if (!process.env.RESEND_API_KEY) {
console.warn('⚠️ WARNING: RESEND_API_KEY is not set. Contact form will not work.');
} else {
console.log('✅ Email service configured');
}
});