-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
127 lines (105 loc) · 3.24 KB
/
server.js
File metadata and controls
127 lines (105 loc) · 3.24 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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { createClient } from "@supabase/supabase-js";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
// Load environment variables
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3005;
// Middleware
app.use(cors());
app.use(express.json());
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY,
);
const rateLimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, "10 s"),
analytics: true,
prefix: "@upstash/ratelimit",
});
function getClientIP(req) {
return (
req.headers["x-real-ip"] ||
req.headers["x-forwarded-for"]?.split(",")[0]?.trim() ||
req.socket?.remoteAddress ||
"unknown"
);
}
// API route for sending emails
app.post("/api/send-email", async (req, res) => {
try {
const { name, email, message } = req.body;
if (!name || !email || !message) {
return res.status(400).json({ error: "Missing fields" });
}
const apiKey = process.env.RESEND_API_KEY;
if (!apiKey) {
return res.status(500).json({ error: "Something went wrong" });
}
const from = process.env.CONTACT_FROM_EMAIL || "onboarding@resend.dev";
const to = process.env.CONTACT_TO_EMAIL || "tusharsachan06@gmail.com";
const response = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
from,
to: [to],
subject: `ISA Contact: ${name}`,
html: `<p><strong>Name:</strong> ${name}</p><p><strong>Email:</strong> ${email}</p><p>${message}</p>`,
reply_to: email,
}),
});
if (!response.ok) {
let detail;
try {
detail = await response.json();
} catch {
detail = await response.text();
}
return res.status(400).json({ error: "Resend error", detail });
}
return res.status(200).json({ ok: true });
} catch (error) {
return res.status(500).json({ error: "Server error" });
}
});
app.get("/api/leasing-listings", async (req, res) => {
const ip = getClientIP(req);
const { success } = await rateLimit.limit(ip);
if (success) {
try {
const { data, error } = await supabase
.from("sublease_listings")
.select("*")
.order("created_at", { ascending: false });
if (error) {
console.error(error);
return res.status(500).json({ error: error.message });
}
res.json(data);
} catch (err) {
res.status(500).json({ error: "Server error" });
}
} else return res.status(429).json({ error: "Request timed out" });
});
// Serve static files in production
if (process.env.NODE_ENV === "production") {
app.use(express.static(join(__dirname, "dist")));
app.get("*", (req, res) => {
res.sendFile(join(__dirname, "dist", "index.html"));
});
}
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});