Update index.js

This commit is contained in:
tiennm99 committed 2025-11-15 21:02:38 +07:00
1 parent 7b3ca13268
commit 1089d1f4e2
1 file changed
+16 -29
+16 -29
View File
@@ -29,11 +29,11 @@ async function handleRequest(request, env) {
} }
try { try {
// Read the request body // Read the request body as plain text
const body = await request.text(); const message = await request.text();
// Validate that body is not empty // Validate that body is not empty
if (!body) { if (!message) {
return new Response('Request body is required', { return new Response('Request body is required', {
status: 400, status: 400,
headers: { headers: {
@@ -42,32 +42,19 @@ async function handleRequest(request, env) {
}); });
} }
let jsonBody; // Create Loki payload from plain text message
try { const lokiPayload = {
jsonBody = JSON.parse(body); streams: [
if (!jsonBody.streams || !Array.isArray(jsonBody.streams)) { {
return new Response('Invalid format: "streams" array is required', { stream: {
status: 400, source: 'miti-loki'
headers: { },
'Access-Control-Allow-Origin': '*', values: [
} [(Date.now() * 1000000).toString(), message]
}); ]
}
} catch (parseError) {
return new Response(`Invalid JSON: ${parseError.message}`, {
status: 400,
headers: {
'Access-Control-Allow-Origin': '*',
} }
}); ]
} };
jsonBody.streams.forEach(stream => {
if (!stream.stream) {
stream.stream = {};
}
stream.stream.source = 'miti-loki';
});
// Construct Loki URL // Construct Loki URL
const lokiPort = env.LOKI_PORT || '443'; const lokiPort = env.LOKI_PORT || '443';
@@ -84,7 +71,7 @@ async function handleRequest(request, env) {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`, 'Authorization': `Basic ${auth}`,
}, },
body: JSON.stringify(jsonBody), body: JSON.stringify(lokiPayload),
}); });
// Get response text // Get response text