-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.js
98 lines (83 loc) · 2.22 KB
/
route.js
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
/**
* Portfolio
* Copyright (C) 2024 Maxim (https://github.com/max1mde/portfolio)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation.
*/
import { NextResponse } from "next/server";
const requestCounts = new Map();
const RATE_LIMIT = 5;
const RATE_LIMIT_WINDOW = 30 * 60 * 1000; // 30 Minutes
function getRateLimitKey(ip) {
return `ratelimit:${ip}`;
}
function checkRateLimit(ip) {
const key = getRateLimitKey(ip);
const now = Date.now();
const requestData = requestCounts.get(key) || {
count: 0,
resetTime: now + RATE_LIMIT_WINDOW,
};
if (now >= requestData.resetTime) {
requestData.count = 0;
requestData.resetTime = now + RATE_LIMIT_WINDOW;
}
if (requestData.count >= RATE_LIMIT) {
return false;
}
requestData.count++;
requestCounts.set(key, requestData);
return true;
}
export async function POST(req) {
const ip =
req.ip ||
req.headers.get("x-forwarded-for") ||
req.headers.get("x-real-ip") ||
"127.0.0.1";
if (!checkRateLimit(ip)) {
return NextResponse.json(
{
success: false,
message: "Too many requests. Please try again later.",
},
{ status: 429 },
);
}
try {
const body = await req.json();
const { name, email, message } = body;
if (!name || !email || !message) {
return NextResponse.json(
{
success: false,
message: "All fields are required.",
},
{ status: 400 },
);
}
/*
Here you can add your own implementation to process the information from the contact form
You can use the name, email and message variable from the top
*/
return NextResponse.json(
{
success: true,
message:
"Your message has been sent, but there is no implementation in place to process it, so it will not be read.",
},
{ status: 200 },
);
} catch (error) {
console.error("Error handling form submission:", error);
return NextResponse.json(
{
success: false,
message: "An error occurred. Please try again later.",
},
{ status: 500 },
);
}
}