mirror of
https://github.com/tiennm99/miti-loki.git
synced 2026-10-11 03:13:40 +00:00
Update index.js
This commit is contained in:
1 parent
7b3ca13268
commit
1089d1f4e2
1 file changed
+16
-29
@@ -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
|
||||||
|
|||||||
Reference in new issue
Block a user