This commit is contained in:
newtbot
2024-01-14 23:31:00 +08:00
parent d9e028d300
commit 935709c2eb
101 changed files with 6 additions and 5 deletions

View File

@ -0,0 +1,18 @@
function apiKeyMiddleware(req, res, next) {
const apiKey = req.headers['x-api-key'];
if (!apiKey) {
return res.status(401).json({ error: 'API key is missing' });
}
//logic to check db?
if (apiKey !== 'YOUR_API_KEY') {
return res.status(403).json({ error: 'Invalid API key' });
}
// API key is valid, continue to the next middleware or route handler
next();
}
module.exports = { apiKeyMiddleware }

View File

@ -0,0 +1,36 @@
const { insertLogData } = require("../functions/database.js");
const APIlogger = (req, res, next) => {
try {
const log = {
ip: req.ip,
time: new Date().toUTCString(),
method: req.method,
//https://stackoverflow.com/questions/10183291/how-to-get-the-full-url-in-express
host: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
statusCode: res.statusCode,
Responsesize: res.get('Content-Length') ? res.get('Content-Length') : 0,
referrer: res.get('content-type') ? res.get('content-type') : "none",
userAgent: req.headers["user-agent"],
};
//upload to db logic here for api logs
insertLogData(log);
next();
}
catch (error) {
console.error(error);
}
};
module.exports = { APIlogger };
/*
method: req.method,
statusCode: res.statusCode,
protocol: req.protocol,
//formatted in nice utc format
time: new Date().toUTCString(),
ip: req.ip,
userAgent: req.headers["user-agent"],
host: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
*/