mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-09 23:48:36 +01:00
add deprecated endpoints (v1)
This commit is contained in:
48
producer/src/deprecated.ts
Normal file
48
producer/src/deprecated.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Router, json } from "express";
|
||||
import { createSessionHash, getIPFromRequest } from "./utils";
|
||||
import { requireEnv } from "../../shared/utilts/requireEnv";
|
||||
import { RedisStreamService } from "@services/RedisStreamService";
|
||||
|
||||
const router = Router();
|
||||
|
||||
const allowAnyType = () => true;
|
||||
const jsonOptions = { limit: '5mb', type: allowAnyType }
|
||||
|
||||
const streamName = requireEnv('STREAM_NAME');
|
||||
|
||||
router.post('/keep_alive', json(jsonOptions), async (req, res) => {
|
||||
try {
|
||||
const ip = getIPFromRequest(req);
|
||||
const sessionHash = createSessionHash(req.body.website, ip, req.body.userAgent);
|
||||
await RedisStreamService.addToStream(streamName, {
|
||||
...req.body, _type: 'keep_alive', sessionHash, ip,
|
||||
instant: req.body.instant + ''
|
||||
});
|
||||
return res.sendStatus(200);
|
||||
} catch (ex: any) {
|
||||
return res.status(500).json({ error: ex.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
router.post('/metrics/push', json(jsonOptions), async (req, res) => {
|
||||
try {
|
||||
const ip = getIPFromRequest(req);
|
||||
const sessionHash = createSessionHash(req.body.website, ip, req.body.userAgent);
|
||||
|
||||
const { type } = req.body;
|
||||
|
||||
if (type === 0) {
|
||||
await RedisStreamService.addToStream(streamName, { ...req.body, _type: 'visit', sessionHash, ip });
|
||||
} else {
|
||||
await RedisStreamService.addToStream(streamName, { ...req.body, _type: 'event', sessionHash, ip });
|
||||
}
|
||||
|
||||
return res.sendStatus(200);
|
||||
} catch (ex: any) {
|
||||
return res.status(500).json({ error: ex.message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,9 +1,9 @@
|
||||
import { requireEnv } from "../../shared/utilts/requireEnv";
|
||||
import { RedisStreamService } from "@services/RedisStreamService";
|
||||
|
||||
import crypto from 'crypto';
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import { createSessionHash, getIPFromRequest } from "./utils";
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -13,19 +13,8 @@ const jsonOptions = { limit: '5mb', type: allowAnyType }
|
||||
|
||||
const streamName = requireEnv('STREAM_NAME');
|
||||
|
||||
|
||||
function getIPFromRequest(req: express.Request) {
|
||||
const ip = req.header('X-Real-IP') || req.header('X-Forwarded-For') || '0.0.0.0';
|
||||
return ip;
|
||||
}
|
||||
|
||||
|
||||
export function createSessionHash(website: string, ip: string, userAgent: string) {
|
||||
const dailySalt = new Date().toLocaleDateString('it-IT');
|
||||
const sessionClean = dailySalt + website + ip + userAgent;
|
||||
const sessionHash = crypto.createHash('md5').update(sessionClean).digest("hex");
|
||||
return sessionHash;
|
||||
}
|
||||
import DeprecatedRouter from "./deprecated";
|
||||
app.use('/v1', DeprecatedRouter);
|
||||
|
||||
app.post('/event', express.json(jsonOptions), async (req, res) => {
|
||||
try {
|
||||
|
||||
16
producer/src/utils.ts
Normal file
16
producer/src/utils.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
import crypto from 'crypto';
|
||||
import type { Request } from "express";
|
||||
|
||||
export function getIPFromRequest(req: Request) {
|
||||
const ip = req.header('X-Real-IP') || req.header('X-Forwarded-For') || '0.0.0.0';
|
||||
return ip;
|
||||
}
|
||||
|
||||
|
||||
export function createSessionHash(website: string, ip: string, userAgent: string) {
|
||||
const dailySalt = new Date().toLocaleDateString('it-IT');
|
||||
const sessionClean = dailySalt + website + ip + userAgent;
|
||||
const sessionHash = crypto.createHash('md5').update(sessionClean).digest("hex");
|
||||
return sessionHash;
|
||||
}
|
||||
Reference in New Issue
Block a user