refactoed kafka ingestion to go via backend not web-db

This commit is contained in:
2025-11-13 17:42:04 +01:00
parent ba8c42e30e
commit 6a2c41bbdb
8 changed files with 147 additions and 38 deletions

View File

@@ -8,7 +8,6 @@
"start": "next start"
},
"dependencies": {
"kafkajs": "^2.2.4",
"next": "16.0.0",
"react": "19.2.0",
"react-dom": "19.2.0",

View File

@@ -1,7 +1,8 @@
import { NextRequest, NextResponse } from 'next/server';
import { sendEvent } from '@/lib/kafka';
import type { EventBase } from '@/lib/events';
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:5000';
export async function POST(req: NextRequest) {
try {
const body = await req.json();
@@ -16,7 +17,15 @@ export async function POST(req: NextRequest) {
ts: body.ts || new Date().toISOString(),
};
await sendEvent(event);
const res = await fetch(`${BACKEND_URL}/api/kafka/ingest`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(event),
});
if (!res.ok) {
throw new Error(`Backend returned ${res.status}`);
}
if (process.env.NEXT_PUBLIC_APP_ENV === 'dev') {
console.log('[ingest]', event);

View File

@@ -1,35 +0,0 @@
import { Kafka, Producer } from 'kafkajs';
import type { EventBase } from './events';
let producer: Producer | null = null;
const kafka = new Kafka({
clientId: 'phantom-web',
brokers: [`${process.env.KAFKA_HOST || 'localhost'}:${process.env.KAFKA_PORT || '9092'}`],
});
export const getProducer = async (): Promise<Producer> => {
if (!producer) {
producer = kafka.producer();
await producer.connect();
}
return producer;
};
export const sendEvent = async (ev: EventBase) => {
const p = await getProducer();
await p.send({
topic: 'user-interactions',
messages: [{
key: ev.sessionId,
value: JSON.stringify(ev),
}],
});
};
export const disconnect = async () => {
if (producer) {
await producer.disconnect();
producer = null;
}
};