aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/etc/schedule/index.js
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-09-25 00:44:40 +0700
committerGitHub <[email protected]>2023-09-25 00:44:40 +0700
commit1a85c2571690ba592ac5183d5eadaf9846fe532b (patch)
tree3f3552c00cd49c0eeab5275275cf5cf5666e5027 /pages/api/v2/etc/schedule/index.js
parentDelete .github/workflows/deploy.yml (diff)
downloadmoopa-4.1.0.tar.xz
moopa-4.1.0.zip
Update v4.1.0 (#79)v4.1.0
* Update v4.1.0 * Update pages/_app.js
Diffstat (limited to 'pages/api/v2/etc/schedule/index.js')
-rw-r--r--pages/api/v2/etc/schedule/index.js14
1 files changed, 11 insertions, 3 deletions
diff --git a/pages/api/v2/etc/schedule/index.js b/pages/api/v2/etc/schedule/index.js
index 7a13fff..9b8f43d 100644
--- a/pages/api/v2/etc/schedule/index.js
+++ b/pages/api/v2/etc/schedule/index.js
@@ -1,6 +1,6 @@
import axios from "axios";
import cron from "cron";
-import redis from "../../../../../lib/redis";
+import { rateLimiterRedis, redis } from "@/lib/redis";
const API_KEY = process.env.API_KEY;
@@ -43,6 +43,14 @@ export default async function handler(req, res) {
try {
let cached;
if (redis) {
+ try {
+ const ipAddress = req.socket.remoteAddress;
+ await rateLimiterRedis.consume(ipAddress);
+ } catch (error) {
+ return res.status(429).json({
+ error: `Too Many Requests, retry after ${error.msBeforeNext / 1000}`,
+ });
+ }
cached = await redis.get("schedule");
}
if (cached) {
@@ -60,9 +68,9 @@ export default async function handler(req, res) {
60 * 60 * 24 * 7
);
}
- res.status(200).json(data);
+ return res.status(200).json(data);
} else {
- res.status(404).json({ message: "Schedule not found" });
+ return res.status(404).json({ message: "Schedule not found" });
}
}
} catch (error) {