From 1089d1f4e2cc0438bc1036cc6a167a8110fcc6df Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Sat, 15 Nov 2025 21:02:38 +0700 Subject: [PATCH] Update index.js --- index.js | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index a1e1c89..15e2a85 100644 --- a/index.js +++ b/index.js @@ -29,11 +29,11 @@ async function handleRequest(request, env) { } try { - // Read the request body - const body = await request.text(); + // Read the request body as plain text + const message = await request.text(); // Validate that body is not empty - if (!body) { + if (!message) { return new Response('Request body is required', { status: 400, headers: { @@ -42,32 +42,19 @@ async function handleRequest(request, env) { }); } - let jsonBody; - try { - jsonBody = JSON.parse(body); - if (!jsonBody.streams || !Array.isArray(jsonBody.streams)) { - return new Response('Invalid format: "streams" array is required', { - status: 400, - headers: { - 'Access-Control-Allow-Origin': '*', - } - }); - } - } catch (parseError) { - return new Response(`Invalid JSON: ${parseError.message}`, { - status: 400, - headers: { - 'Access-Control-Allow-Origin': '*', + // Create Loki payload from plain text message + const lokiPayload = { + streams: [ + { + stream: { + source: 'miti-loki' + }, + values: [ + [(Date.now() * 1000000).toString(), message] + ] } - }); - } - - jsonBody.streams.forEach(stream => { - if (!stream.stream) { - stream.stream = {}; - } - stream.stream.source = 'miti-loki'; - }); + ] + }; // Construct Loki URL const lokiPort = env.LOKI_PORT || '443'; @@ -84,7 +71,7 @@ async function handleRequest(request, env) { 'Content-Type': 'application/json', 'Authorization': `Basic ${auth}`, }, - body: JSON.stringify(jsonBody), + body: JSON.stringify(lokiPayload), }); // Get response text