feat(logger): streaming logs works server side not frontend for now
This commit is contained in:
29
server/services/logger/controller/streamLogs.ts
Normal file
29
server/services/logger/controller/streamLogs.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import type {Context} from "hono";
|
||||||
|
import {db} from "../../../../database/dbclient.js";
|
||||||
|
import {and, eq, gt} from "drizzle-orm";
|
||||||
|
import {streamSSE} from "hono/streaming";
|
||||||
|
import {logs} from "../../../../database/schema/logs.js";
|
||||||
|
|
||||||
|
export async function streamLogs(c: Context) {
|
||||||
|
let id = 0;
|
||||||
|
let running = true;
|
||||||
|
// c.header("Content-Type", "text/event-stream");
|
||||||
|
// c.header("Cache-Control", "no-cache");
|
||||||
|
// c.header("Connection", "keep-alive");
|
||||||
|
|
||||||
|
const getLogs = async () => {};
|
||||||
|
return streamSSE(c, async (stream) => {
|
||||||
|
while (running) {
|
||||||
|
const message = `It is ${new Date().toISOString()}`;
|
||||||
|
await stream.writeSSE({
|
||||||
|
data: message,
|
||||||
|
event: "time-update",
|
||||||
|
id: String(id++),
|
||||||
|
});
|
||||||
|
await stream.sleep(1000);
|
||||||
|
if (id === 5) {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -7,10 +7,11 @@ import {settings} from "../../../database/schema/settings.js";
|
|||||||
import {logCleanup} from "./controller/logCleanup.js";
|
import {logCleanup} from "./controller/logCleanup.js";
|
||||||
import createNewLog from "./routes/createLog.js";
|
import createNewLog from "./routes/createLog.js";
|
||||||
import getLogs from "./routes/getLogs.js";
|
import getLogs from "./routes/getLogs.js";
|
||||||
|
import stream from "./routes/streamLogs.js";
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
const routes = [clearLog, createNewLog, getLogs] as const;
|
const routes = [clearLog, createNewLog, getLogs, stream] as const;
|
||||||
const setting = await db.select().from(settings);
|
const setting = await db.select().from(settings);
|
||||||
|
|
||||||
const appRoutes = routes.forEach((route) => {
|
const appRoutes = routes.forEach((route) => {
|
||||||
|
|||||||
35
server/services/logger/routes/streamLogs.ts
Normal file
35
server/services/logger/routes/streamLogs.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
// an external way to creating logs
|
||||||
|
//@ts-nocheck
|
||||||
|
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||||
|
import {apiHit} from "../../../globalUtils/apiHits.js";
|
||||||
|
import {responses} from "../../../globalUtils/routeDefs/responses.js";
|
||||||
|
import {createLog} from "../logger.js";
|
||||||
|
import {getLogs} from "../controller/getLogs.js";
|
||||||
|
import {streamLogs} from "../controller/streamLogs.js";
|
||||||
|
import {streamSSE} from "hono/streaming";
|
||||||
|
|
||||||
|
const app = new OpenAPIHono({strict: false});
|
||||||
|
|
||||||
|
app.openapi(
|
||||||
|
createRoute({
|
||||||
|
tags: ["server:logger"],
|
||||||
|
summary: "Streams the logs to the frontend.",
|
||||||
|
method: "get",
|
||||||
|
path: "/logs/stream",
|
||||||
|
description: "This should only be used on the event you need to monitor logs.",
|
||||||
|
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
content: {
|
||||||
|
"application/json": {schema: z.object({message: z.string().optional()})},
|
||||||
|
},
|
||||||
|
description: "Response message",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
async (c) => {
|
||||||
|
apiHit(c, {endpoint: `api/logger/logs`});
|
||||||
|
return streamLogs(c);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
export default app;
|
||||||
Reference in New Issue
Block a user