2024-12-12 21:17:19 +03:00
|
|
|
const logger = require("../shared/logger/src/main.js");
|
|
|
|
const config = require("../shared/config/src/main.js")();
|
2024-10-15 17:40:37 +03:00
|
|
|
|
2024-12-12 21:17:19 +03:00
|
|
|
const fs = require("fs");
|
|
|
|
const axios = require("axios");
|
|
|
|
const UAParser = require("ua-parser-js");
|
2024-08-02 21:51:54 +03:00
|
|
|
|
2024-12-12 21:17:19 +03:00
|
|
|
require("../shared/database/src/create_table.js")();
|
2024-10-08 20:15:20 +03:00
|
|
|
|
2024-12-12 21:17:19 +03:00
|
|
|
const fastify = require("fastify")({
|
2024-12-23 18:53:56 +03:00
|
|
|
logger: config["server"]["log"]["print"] ? logger : false,
|
|
|
|
...(config["server"]["ssl"]["work"]
|
|
|
|
? {
|
|
|
|
https: {
|
|
|
|
key: fs.readFileSync(config["server"]["ssl"]["private_key"], "utf8"),
|
|
|
|
cert: fs.readFileSync(config["server"]["ssl"]["cert"], "utf8"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: false),
|
2024-08-02 21:51:54 +03:00
|
|
|
});
|
|
|
|
|
2024-12-12 21:17:19 +03:00
|
|
|
const getRateRoute = require("./routes/getRate.js");
|
|
|
|
const HomeRoute = require("./routes/home.js");
|
2025-01-17 15:11:29 +03:00
|
|
|
const getMetadata = require("./routes/metadata.js");
|
2024-07-30 18:06:21 +03:00
|
|
|
|
2024-10-02 22:35:49 +03:00
|
|
|
fastify.register(getRateRoute);
|
2024-10-12 21:13:41 +03:00
|
|
|
fastify.register(HomeRoute);
|
2025-01-17 15:11:29 +03:00
|
|
|
fastify.register(getMetadata);
|
2024-08-02 15:42:35 +03:00
|
|
|
|
2024-11-08 15:22:52 +03:00
|
|
|
fastify.setNotFoundHandler(function (res, reply) {
|
2024-12-23 18:53:56 +03:00
|
|
|
return reply.status(404).send({
|
|
|
|
status: 404,
|
|
|
|
message: "Page not found!",
|
|
|
|
documentation: "https://kekkai-docs.redume.su/",
|
|
|
|
});
|
2024-11-08 15:22:52 +03:00
|
|
|
});
|
|
|
|
|
2024-12-12 21:17:19 +03:00
|
|
|
fastify.addHook("onResponse", async (request, reply) => {
|
2024-12-23 18:53:56 +03:00
|
|
|
const routePart = request.raw.url.split("/");
|
|
|
|
const routePartFiltered = routePart
|
|
|
|
.filter((part) => part !== "")
|
|
|
|
.map((part) => `${part}/`);
|
2024-10-21 19:45:53 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
routePartFiltered.unshift("/");
|
2024-10-21 19:45:53 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
if (!config?.["analytics"]["work"] ? config?.["analytics"]["work"] : false)
|
|
|
|
return;
|
|
|
|
else if (!fastify.printRoutes().includes(routePartFiltered.at(-1))) return;
|
2024-10-15 17:40:37 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
const userAgent = request.headers["user-agent"];
|
|
|
|
const parser = new UAParser(userAgent);
|
|
|
|
const browser = parser.getBrowser();
|
|
|
|
const os = parser.getOS();
|
2024-10-15 17:40:37 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
const formattedOS =
|
|
|
|
os.name && os.version ? `${os.name} ${os.version}` : "N/A";
|
|
|
|
const formattedBrowser =
|
|
|
|
browser.name && browser.version
|
|
|
|
? `${browser.name} ${browser.version}`
|
|
|
|
: "N/A";
|
2024-10-21 19:45:53 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
const event = {
|
|
|
|
domain: config["analytics"]["plausible_domain"],
|
|
|
|
name: request.routeOptions.url
|
|
|
|
? request.routeOptions.url
|
|
|
|
: "404 - Not Found",
|
|
|
|
url: request.raw.url,
|
|
|
|
props: {
|
|
|
|
method: request.method,
|
|
|
|
statusCode: reply.statusCode,
|
|
|
|
browser: formattedBrowser,
|
|
|
|
os: formattedOS,
|
|
|
|
source: request.headers["referer"]
|
|
|
|
? request.headers["referer"]
|
|
|
|
: "direct",
|
|
|
|
},
|
|
|
|
};
|
2024-10-15 17:40:37 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
try {
|
|
|
|
await axios.post(config["analytics"]["plausible_api"], event, {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${config["analytics"]["plausible_token"]}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"User-Agent": userAgent,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
fastify.log.error("Error sending event to Plausible:", error.message);
|
|
|
|
}
|
2024-10-15 17:40:37 +03:00
|
|
|
});
|
|
|
|
|
2024-08-17 16:40:20 +03:00
|
|
|
fastify.listen(
|
2024-12-23 18:53:56 +03:00
|
|
|
{
|
|
|
|
port: 3000,
|
|
|
|
host: config["server"]["host"] ? config["server"]["host"] : "localhost",
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
fastify.log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
},
|
2024-08-17 16:40:20 +03:00
|
|
|
);
|