From 943f7f594ba90aa037ab5cb1b057f496e8a5fcb2 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 14 Sep 2024 16:52:49 +0800 Subject: [PATCH] feat: add lighthouse html report endpoint --- src/server/app.ts | 2 + src/server/router/lighthouse.ts | 46 +++++++++++++++++++++++ src/server/utils/screenshot/lighthouse.ts | 6 ++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/server/router/lighthouse.ts diff --git a/src/server/app.ts b/src/server/app.ts index 39b54a2..37ec314 100644 --- a/src/server/app.ts +++ b/src/server/app.ts @@ -15,6 +15,7 @@ import { import { env } from './utils/env.js'; import cors from 'cors'; import { serverStatusRouter } from './router/serverStatus.js'; +import { lighthouseRouter } from './router/lighthouse.js'; import { logger } from './utils/logger.js'; import { monitorRouter } from './router/monitor.js'; import { healthRouter } from './router/health.js'; @@ -53,6 +54,7 @@ app.use('/api/workspace', workspaceRouter); app.use('/monitor', monitorRouter); app.use('/telemetry', telemetryRouter); app.use('/serverStatus', serverStatusRouter); +app.use('/lh', lighthouseRouter); app.use('/trpc', trpcExpressMiddleware); diff --git a/src/server/router/lighthouse.ts b/src/server/router/lighthouse.ts new file mode 100644 index 0000000..a007e5b --- /dev/null +++ b/src/server/router/lighthouse.ts @@ -0,0 +1,46 @@ +import { Router } from 'express'; +import { param, validate } from '../middleware/validate.js'; +import { prisma } from '../model/_client.js'; +import { getLighthouseReport } from '../utils/screenshot/lighthouse.js'; + +export const lighthouseRouter = Router(); + +lighthouseRouter.get( + '/:lighthouseId', + validate(param('lighthouseId').isString()), + async (req, res) => { + const { lighthouseId } = req.params; + + const { result } = await prisma.websiteLighthouseReport.findUniqueOrThrow({ + where: { + id: lighthouseId, + }, + select: { + result: true, + }, + }); + + res.setHeader('Content-Type', 'application/json').send(JSON.parse(result)); + } +); + +lighthouseRouter.get( + '/:lighthouseId/html', + validate(param('lighthouseId').isString()), + async (req, res) => { + const { lighthouseId } = req.params; + + const { result } = await prisma.websiteLighthouseReport.findUniqueOrThrow({ + where: { + id: lighthouseId, + }, + select: { + result: true, + }, + }); + + res + .setHeader('Content-Type', 'text/html') + .send(getLighthouseReport(JSON.parse(result))); + } +); diff --git a/src/server/utils/screenshot/lighthouse.ts b/src/server/utils/screenshot/lighthouse.ts index cec40a6..f633222 100644 --- a/src/server/utils/screenshot/lighthouse.ts +++ b/src/server/utils/screenshot/lighthouse.ts @@ -1,5 +1,5 @@ import puppeteer from 'puppeteer'; -import lighthouse, { Result } from 'lighthouse'; +import lighthouse, { Result, generateReport } from 'lighthouse'; export async function generateLighthouse(url: string): Promise { // Use Puppeteer to launch headless Chrome @@ -33,3 +33,7 @@ export async function generateLighthouse(url: string): Promise { return lhr; } + +export function getLighthouseReport(lhr: Result): string { + return generateReport(lhr); +}