From 1a85c2571690ba592ac5183d5eadaf9846fe532b Mon Sep 17 00:00:00 2001 From: Factiven Date: Mon, 25 Sep 2023 00:44:40 +0700 Subject: Update v4.1.0 (#79) * Update v4.1.0 * Update pages/_app.js --- pages/api/v2/admin/broadcast/index.js | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 pages/api/v2/admin/broadcast/index.js (limited to 'pages/api/v2/admin/broadcast') diff --git a/pages/api/v2/admin/broadcast/index.js b/pages/api/v2/admin/broadcast/index.js new file mode 100644 index 0000000..d3d3af0 --- /dev/null +++ b/pages/api/v2/admin/broadcast/index.js @@ -0,0 +1,40 @@ +import { rateLimitStrict, redis } from "@/lib/redis"; +// import { getServerSession } from "next-auth"; +// import { authOptions } from "pages/api/auth/[...nextauth]"; + +export default async function handler(req, res) { + // Check if the custom header "X-Your-Custom-Header" is present and has a specific value + const customHeaderValue = req.headers["x-broadcast-key"]; + + if (customHeaderValue !== "get-broadcast") { + return res.status(401).json({ message: "Unauthorized" }); + } + + try { + if (redis) { + try { + const ipAddress = req.socket.remoteAddress; + await rateLimitStrict.consume(ipAddress); + } catch (error) { + return res.status(429).json({ + error: `Too Many Requests, retry after ${error.msBeforeNext / 1000}`, + }); + } + + const getId = await redis.get(`broadcast`); + if (getId) { + const broadcast = JSON.parse(getId); + return res + .status(200) + .json({ message: broadcast.message, startAt: broadcast.startAt }); + } else { + return res.status(200).json({ message: "No broadcast" }); + } + } + + return res.status(200).json({ message: "redis is not defined" }); + } catch (err) { + console.error(err); + res.status(500).json({ error: err.message }); + } +} -- cgit v1.2.3