2025-03-02 22:33:02 +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
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const axios = require('axios');
|
|
|
|
const UAParser = require('ua-parser-js');
|
2024-08-02 21:51:54 +03:00
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
require('../shared/database/src/create_table.js')();
|
2024-10-08 20:15:20 +03:00
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
const fastify = require('fastify')({
|
2025-03-03 01:27:32 +03:00
|
|
|
logger: config['server']['log']['level'] !== 'none' ? logger : false,
|
2025-03-02 22:33:02 +03:00
|
|
|
...(config['server']['ssl']['work']
|
2024-12-23 18:53:56 +03:00
|
|
|
? {
|
2025-03-02 22:33:02 +03:00
|
|
|
https: {
|
|
|
|
key: fs.readFileSync(
|
|
|
|
config['server']['ssl']['private_key'],
|
|
|
|
'utf8',
|
|
|
|
),
|
|
|
|
cert: fs.readFileSync(
|
|
|
|
config['server']['ssl']['cert'],
|
|
|
|
'utf8',
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
2024-12-23 18:53:56 +03:00
|
|
|
: false),
|
2024-08-02 21:51:54 +03:00
|
|
|
});
|
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
const getRateRoute = require('./routes/getRate.js');
|
|
|
|
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);
|
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,
|
2025-03-02 22:33:02 +03:00
|
|
|
message: 'Page not found!',
|
|
|
|
documentation: 'https://kekkai-docs.redume.su/',
|
2024-12-23 18:53:56 +03:00
|
|
|
});
|
2024-11-08 15:22:52 +03:00
|
|
|
});
|
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
|
|
const routePart = request.raw.url.split('/');
|
2024-12-23 18:53:56 +03:00
|
|
|
const routePartFiltered = routePart
|
2025-03-02 22:33:02 +03:00
|
|
|
.filter((part) => part !== '')
|
2024-12-23 18:53:56 +03:00
|
|
|
.map((part) => `${part}/`);
|
2024-10-21 19:45:53 +03:00
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
routePartFiltered.unshift('/');
|
2024-10-21 19:45:53 +03:00
|
|
|
|
2025-03-03 01:22:31 +03:00
|
|
|
if (
|
|
|
|
!config?.['analytics']['enabled']
|
|
|
|
? config?.['analytics']['enabled']
|
|
|
|
: false
|
|
|
|
)
|
2024-12-23 18:53:56 +03:00
|
|
|
return;
|
|
|
|
else if (!fastify.printRoutes().includes(routePartFiltered.at(-1))) return;
|
2024-10-15 17:40:37 +03:00
|
|
|
|
2025-03-02 22:33:02 +03:00
|
|
|
const userAgent = request.headers['user-agent'];
|
2024-12-23 18:53:56 +03:00
|
|
|
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 =
|
2025-03-02 22:33:02 +03:00
|
|
|
os.name && os.version ? `${os.name} ${os.version}` : 'N/A';
|
2024-12-23 18:53:56 +03:00
|
|
|
const formattedBrowser =
|
|
|
|
browser.name && browser.version
|
|
|
|
? `${browser.name} ${browser.version}`
|
2025-03-02 22:33:02 +03:00
|
|
|
: 'N/A';
|
2024-10-21 19:45:53 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
const event = {
|
2025-03-02 22:33:02 +03:00
|
|
|
domain: config['analytics']['plausible_domain'],
|
2024-12-23 18:53:56 +03:00
|
|
|
name: request.routeOptions.url
|
|
|
|
? request.routeOptions.url
|
2025-03-02 22:33:02 +03:00
|
|
|
: '404 - Not Found',
|
2024-12-23 18:53:56 +03:00
|
|
|
url: request.raw.url,
|
|
|
|
props: {
|
|
|
|
method: request.method,
|
|
|
|
statusCode: reply.statusCode,
|
|
|
|
browser: formattedBrowser,
|
|
|
|
os: formattedOS,
|
2025-03-02 22:33:02 +03:00
|
|
|
source: request.headers['referer']
|
|
|
|
? request.headers['referer']
|
|
|
|
: 'direct',
|
2024-12-23 18:53:56 +03:00
|
|
|
},
|
|
|
|
};
|
2024-10-15 17:40:37 +03:00
|
|
|
|
2024-12-23 18:53:56 +03:00
|
|
|
try {
|
2025-03-03 01:22:31 +03:00
|
|
|
await axios.post(
|
|
|
|
`https://${config['analytics']['plausible_domain']}/api/event`,
|
|
|
|
event,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${config['analytics']['plausible_token']}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'User-Agent': userAgent,
|
|
|
|
},
|
2024-12-23 18:53:56 +03:00
|
|
|
},
|
2025-03-03 01:22:31 +03:00
|
|
|
);
|
2024-12-23 18:53:56 +03:00
|
|
|
} catch (error) {
|
2025-03-02 22:33:02 +03:00
|
|
|
fastify.log.error('Error sending event to Plausible:', error.message);
|
2024-12-23 18:53:56 +03:00
|
|
|
}
|
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,
|
2025-03-02 22:33:02 +03:00
|
|
|
host: config['server']['host'] ? config['server']['host'] : 'localhost',
|
2024-12-23 18:53:56 +03:00
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
fastify.log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
},
|
2024-08-17 16:40:20 +03:00
|
|
|
);
|