hutatools / app.js
fullpwerr's picture
Update app.js
a8838d0 verified
const express = require("express");
const cors = require("cors");
const path = require("path");
const fs = require("fs");
require("dotenv").config();
const bodyParser = require("body-parser");
const apis = require("./fnc/apis");
const downloadf = require("./fnc/download");
const app = express();
const port = process.env.PORT || 7860;
app.use(express.json({
limit: "500mb"
}));
app.use(express.urlencoded({
limit: "500mb",
extended: true
}));
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use("/cdn", express.static(path.join(__dirname, "cdn")));
app.use("/tmp", express.static(path.join(__dirname, "tmp")));
app.use(async (req, res, next) => {
const _req = Date.now()
const ip = req?.ip?.replace("::ffff:", "") || req?.ip;
req._ip = ip;
res.on("finish", () => {
const _end = Date.now();
const total = _end - _req;
const _date = formatDate(new Date(_req));
const date = `${_date.YYYY}-${_date.MM}-${_date.DD}`;
const time = `${_date.hh}:${_date.mm}:${_date.ss}`;
console.log(
"\x1b[36;1m[\x1b[32;1m" + req.method + "\x1b[36;1m]\x1b[0;0m\n",
"Path:", req.originalUrl + "\n",
"Host:", req.get("host") + "\n",
"Status: \x1b[33m" + res.statusCode + "\x1b[0;0m\n",
"Time:", total + "ms\n",
"Length:", res.getHeaders()["content-length"] + "\n",
"User-Agent:", req.get("user-agent") + "\n",
"IP:", ip + "\n",
"Date:", date + "\n",
"Time:", time
);
});
next();
});
app.get("/", (req, res) => {
res.send({ status: 401, message: "You don't have access to this!"})
});
app.post("/api", apis);
app.get("/download", downloadf);
const apiStatusFile = path.join(__dirname, "/tmp","api.json");
// Endpoint untuk membaca api.json
app.get("/api-checker", (req, res) => {
res.send(`
<html>
<head>
<meta http-equiv="refresh" content="3;url=https://fullpwerr-hutatools.hf.space/tmp/api.json" />
</head>
<body>
</body>
</html>
`)
})
let server
app.listen(port, () => {
console.log("listening on localhost:", port);
})
function formatDate(date) {
const YYYY = String(date.getFullYear());
const MM = String(date.getMonth() + 1).padStart(2, "0");
const DD = String(date.getDate()).padStart(2, "0");
const hh = String(date.getHours()).padStart(2, "0");
const mm = String(date.getMinutes()).padStart(2, "0");
const ss = String(date.getSeconds()).padStart(2, "0");
return {
YYYY,
MM,
DD,
hh,
mm,
ss
}
}