Skip to content

Commit 1c357da

Browse files
committed
fix: fix access log
1 parent 9ecef63 commit 1c357da

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/routes/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import { sendAccessLog } from "../services/log.ts";
66
const app = new Hono();
77

88
app.use(async (c, next) => {
9-
if (!c.req.path.startsWith("/api")) await sendAccessLog(c);
10-
119
await next();
10+
11+
if (!c.req.path.startsWith("/api")) {
12+
// 200-299 or 300-399
13+
if (c.res.ok || (c.res.status >= 300 && c.res.status < 400)) {
14+
await sendAccessLog(c, false);
15+
} else {
16+
await sendAccessLog(c, true);
17+
}
18+
}
1219
});
1320

1421
app.get("/", home);

src/services/log.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ const DISCORD_WEBHOOK_URL_ACCESS_LOG = Deno.env.get(
99
const DISCORD_WEBHOOK_URL_API_ACCESS_LOG = Deno.env.get(
1010
"DISCORD_WEBHOOK_URL_API_ACCESS_LOG",
1111
);
12+
const DISCORD_WEBHOOK_URL_OTHERS_LOG = Deno.env.get(
13+
"DISCORD_WEBHOOK_URL_OTHERS_LOG",
14+
);
1215

13-
export const sendAccessLog = async (c: Context) => {
14-
if (!DISCORD_WEBHOOK_URL_ACCESS_LOG) return;
16+
export const sendAccessLog = async (c: Context, others: boolean) => {
17+
const webhookURL = others
18+
? DISCORD_WEBHOOK_URL_OTHERS_LOG
19+
: DISCORD_WEBHOOK_URL_ACCESS_LOG;
20+
if (!webhookURL) return;
1521

1622
const info = getConnInfo(c);
1723
const time = new Date().toLocaleString("en-US", {
@@ -24,18 +30,21 @@ export const sendAccessLog = async (c: Context) => {
2430
second: "numeric",
2531
});
2632

27-
await sendDiscordWebhook(DISCORD_WEBHOOK_URL_ACCESS_LOG, {
28-
content: c.req.path,
29-
embeds: [
30-
{
31-
description: `[${c.req.method}] ${c.req.url}`,
32-
color: 0x008000,
33-
footer: {
34-
text: `${time} - ${info.remote.address}`,
33+
await sendDiscordWebhook(
34+
webhookURL,
35+
{
36+
content: c.req.path,
37+
embeds: [
38+
{
39+
description: `[${c.req.method}] ${c.req.url} - ${c.res.status}`,
40+
color: others ? 0xffff00 : 0x008000,
41+
footer: {
42+
text: `${time} - ${info.remote.address}`,
43+
},
3544
},
36-
},
37-
],
38-
});
45+
],
46+
},
47+
);
3948
};
4049

4150
export const sendAPIAccessLog = async (
@@ -51,9 +60,9 @@ export const sendAPIAccessLog = async (
5160
const encoder = new TextEncoder();
5261
const data = encoder.encode(apiKey);
5362
const hash = await crypto.subtle.digest("SHA-256", data);
54-
hashedApiKey = Array.from(new Uint8Array(hash)).map((b) =>
55-
b.toString(16).padStart(2, "0")
56-
).join("");
63+
hashedApiKey = Array.from(new Uint8Array(hash))
64+
.map((b) => b.toString(16).padStart(2, "0"))
65+
.join("");
5766
}
5867

5968
const info = getConnInfo(c);
@@ -73,10 +82,12 @@ export const sendAPIAccessLog = async (
7382
title,
7483
description: `[${c.req.method}] ${c.req.url}`,
7584
color: error ? 0xff0000 : 0x008000,
76-
fields: [{
77-
name: "Hashed API Key",
78-
value: hashedApiKey,
79-
}],
85+
fields: [
86+
{
87+
name: "Hashed API Key",
88+
value: hashedApiKey,
89+
},
90+
],
8091
footer: {
8192
text: `${time} - ${info.remote.address}`,
8293
},

0 commit comments

Comments
 (0)