mirror of
https://github.com/ente-io/ente.git
synced 2025-08-08 07:28:26 +00:00
Cleanup
This commit is contained in:
parent
07f0cc9342
commit
2b490fe131
@ -2,7 +2,6 @@
|
||||
|
||||
export default {
|
||||
async fetch(request: Request) {
|
||||
console.log("This message should appear in the logs.");
|
||||
switch (request.method) {
|
||||
case "OPTIONS":
|
||||
return handleOPTIONS(request);
|
||||
@ -10,7 +9,6 @@ export default {
|
||||
return handleGET(request);
|
||||
default:
|
||||
console.log(`Unsupported HTTP method ${request.method}`);
|
||||
throw new Error("Unsupported HTTP method");
|
||||
return new Response(null, { status: 405 });
|
||||
}
|
||||
},
|
||||
@ -30,7 +28,6 @@ const handleOPTIONS = (request: Request) => {
|
||||
};
|
||||
|
||||
const isAllowedOrigin = (origin: string | null) => {
|
||||
console.log(origin);
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const url = new URL(origin);
|
||||
|
@ -4,29 +4,38 @@
|
||||
* https://developers.cloudflare.com/workers/observability/logging/tail-workers/
|
||||
*/
|
||||
export default {
|
||||
async tail(events: TraceItem[], env: Env, ctx: ExecutionContext) {
|
||||
console.log("Length of LOKI_PUSH_URL", env.LOKI_PUSH_URL.length);
|
||||
|
||||
async tail(events: TraceItem[], env: Env) {
|
||||
// If the tail worker itself throws an exception (it shouldn't, unless
|
||||
// Loki is down), we don't catch it so that it counts as an "error" in
|
||||
// the worker stats.
|
||||
ctx.waitUntil(handleTail(events, env.LOKI_PUSH_URL, env.LOKI_AUTH));
|
||||
await handleTail(events, env);
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
||||
|
||||
interface Env {
|
||||
/** The URL of the Loki instance to push logs to. */
|
||||
LOKI_PUSH_URL: string;
|
||||
/**
|
||||
* The value of the "Basic" authorization.
|
||||
*
|
||||
* [Note: HTTP basic authorization in worker fetch]
|
||||
*
|
||||
* Usually a Loki push URL is specified with the credentials inline, say
|
||||
* `http://user:pass@loki/path`. However, I cannot get that to work with the
|
||||
* `fetch` inside a Cloudflare worker. Instead, the credentials need to be
|
||||
* separately provided as the Authorization header of the form:
|
||||
*
|
||||
* Authorization: Basic ${btoa(user:pass)}
|
||||
*
|
||||
* The LOKI_AUTH secret is the "${btoa(user:pass)}" value.
|
||||
*/
|
||||
LOKI_AUTH: string;
|
||||
}
|
||||
|
||||
const handleTail = async (events: TraceItem[], lokiPushURL: string, lokiAuth: string) =>
|
||||
await pushLogLine(Date.now(), JSON.stringify(events), lokiPushURL, lokiAuth);
|
||||
|
||||
// const handleTail1 = async (events: TraceItem[], lokiPushURL: string) => {
|
||||
// for (const event of events.filter(hasLogOrException)) {
|
||||
// await pushLogLine(Date.now(), JSON.stringify(event), lokiPushURL);
|
||||
// }
|
||||
// };
|
||||
const handleTail = async (events: TraceItem[], env: Env) => {
|
||||
for (const event of events.filter(hasLogOrException))
|
||||
await pushLogLine(Date.now(), JSON.stringify(event), env);
|
||||
};
|
||||
|
||||
/** Return true if the {@link event} has at least one log or exception. */
|
||||
const hasLogOrException = (event: TraceItem) =>
|
||||
@ -42,45 +51,22 @@ const hasLogOrException = (event: TraceItem) =>
|
||||
*
|
||||
* @param logLine The message to log.
|
||||
*
|
||||
* @param lokiPushURL The URL of the Loki instance to push logs to. The
|
||||
* credentials are part of the URL.
|
||||
* @param env The worker environment; we need it for the Loki URL and
|
||||
* credentials.
|
||||
*/
|
||||
const pushLogLine = async (
|
||||
timestampMs: number,
|
||||
logLine: string,
|
||||
lokiPushURL: string,
|
||||
lokiAuth: string,
|
||||
) => {
|
||||
console.log(
|
||||
"Pushing",
|
||||
JSON.stringify({
|
||||
const pushLogLine = async (timestampMs: number, logLine: string, env: Env) =>
|
||||
await fetch(env.LOKI_PUSH_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${env.LOKI_AUTH}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
streams: [
|
||||
{
|
||||
stream: { job: "worker" },
|
||||
values: [[`${timestampMs * 1e6}`, logLine]],
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
try {
|
||||
const res = await fetch(lokiPushURL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Basic ${lokiAuth}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
streams: [
|
||||
{
|
||||
stream: { job: "worker" },
|
||||
values: [[`${timestampMs * 1e6}`, logLine]],
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
console.log(res.status, await res.text()); //JSON.stringify(res));
|
||||
return res;
|
||||
} catch (e) {
|
||||
console.log("Failed", e);
|
||||
}
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
@ -4,4 +4,5 @@ compatibility_date = "2024-06-14"
|
||||
|
||||
[vars]
|
||||
# Added as a secret via the Cloudflare dashboard
|
||||
# LOKI_PUSH_URL = "https://{loki_base_url}>/loki/api/v1/push"
|
||||
# LOKI_PUSH_URL = "https://${loki_base_url}>/loki/api/v1/push"
|
||||
# LOKI_AUTH = "${btoa(user:pass)}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user