import { Kafka } from "@upstash/kafka";
export interface Env {
UPSTASH_KAFKA_REST_URL: string;
UPSTASH_KAFKA_REST_USERNAME: string;
UPSTASH_KAFKA_REST_PASSWORD: string;
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
if (new URL(request.url).pathname == "/favicon.ico") {
return new Response(null, { status: 200 });
}
let message = {
country: request.cf?.country,
city: request.cf?.city,
region: request.cf?.region,
url: request.url,
ip: request.headers.get("x-real-ip"),
mobile: request.headers.get("sec-ch-ua-mobile"),
platform: request.headers.get("sec-ch-ua-platform"),
useragent: request.headers.get("user-agent"),
};
const kafka = new Kafka({
url: env.UPSTASH_KAFKA_REST_URL,
username: env.UPSTASH_KAFKA_REST_USERNAME,
password: env.UPSTASH_KAFKA_REST_PASSWORD,
});
const p = kafka.producer();
// Please update the topic according to your configuration
const topic = "mytopic";
ctx.waitUntil(p.produce(topic, JSON.stringify(message)));
// if you use CF Workers to intercept your existing site, uncomment below
// return await fetch(request);
return new Response("My website");
},
};