Files
itsc/pages/api/time/limit.js
2022-03-31 17:20:26 +08:00

38 lines
976 B
JavaScript

import { authenticate, setLimit, getLimit } from "../../../libs/db";
import { addAPIQPS } from "../../../libs/stats";
export default function handler(req, res) {
addAPIQPS();
// put method
if (req.method === "PUT") {
const { token, limit } = req.body;
// authenticate
if (!authenticate(token)) {
res.status(401).json({
error: `token ${token} 验证失败`,
});
return;
}
// check type is integer
const limitInt = parseInt(limit);
if (!limitInt) {
res.status(400).json({
error: `数量限制必须是整数,但是传入了 ${limit}`,
});
return;
}
setLimit(limitInt);
res.status(200).send({ success: true });
} else if (req.method === "GET") {
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
res.status(200).json({
limit: getLimit(),
});
} else {
res.status(405).send({ error: "方法" + req.method + "不被允许" });
}
}