-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-server.mjs
More file actions
99 lines (84 loc) · 2.68 KB
/
Copy pathapi-server.mjs
File metadata and controls
99 lines (84 loc) · 2.68 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
/**
* Standalone API server for the contact form.
* Runs alongside the static Astro site on the VM.
*
* Usage:
* RESEND_API_KEY=re_xxx node api-server.mjs
*
* In production, configure nginx to proxy /api/* to http://localhost:3001
*/
import { createServer } from "http";
import { Resend } from "resend";
const PORT = process.env.API_PORT ?? 3001;
const TOPIC_LABELS = {
platform: "Platform access & accounts",
technical: "Technical support",
ecosystem: "Joining the ecosystem",
research: "Research collaboration",
press: "Press & communications",
other: "Other",
};
function parseFormData(body) {
const params = new URLSearchParams(body);
return Object.fromEntries(params.entries());
}
async function handleContact(req, res) {
let body = "";
for await (const chunk of req) body += chunk;
const fields = parseFormData(body);
const { name, email, topic, message } = fields;
if (!name?.trim() || !email?.trim() || !topic || !message?.trim()) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ok: false, error: "Missing fields" }));
return;
}
const apiKey = process.env.RESEND_API_KEY;
if (!apiKey) {
console.error("RESEND_API_KEY is not set");
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ok: false, error: "Server misconfiguration" }));
return;
}
try {
const resend = new Resend(apiKey);
await resend.emails.send({
from: "AI4EOSC Contact Form <noreply@ai4eosc.eu>",
to: "ai4eosc@csic.es",
replyTo: email.trim(),
subject: `[Contact] ${TOPIC_LABELS[topic] ?? topic} — ${name.trim()}`,
text: [
`Name: ${name.trim()}`,
`Email: ${email.trim()}`,
`Topic: ${TOPIC_LABELS[topic] ?? topic}`,
"",
message.trim(),
].join("\n"),
});
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ok: true }));
} catch (err) {
console.error("Resend error:", err);
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ok: false, error: "Failed to send email" }));
}
}
const server = createServer((req, res) => {
// CORS for local dev
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
if (req.method === "OPTIONS") {
res.writeHead(204);
res.end();
return;
}
if (req.method === "POST" && req.url === "/api/contact") {
handleContact(req, res);
return;
}
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Not found" }));
});
server.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}`);
});