feat: add lighthouse html report endpoint

This commit is contained in:
moonrailgun 2024-09-14 16:52:49 +08:00
parent 28d982e497
commit 943f7f594b
3 changed files with 53 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import {
import { env } from './utils/env.js'; import { env } from './utils/env.js';
import cors from 'cors'; import cors from 'cors';
import { serverStatusRouter } from './router/serverStatus.js'; import { serverStatusRouter } from './router/serverStatus.js';
import { lighthouseRouter } from './router/lighthouse.js';
import { logger } from './utils/logger.js'; import { logger } from './utils/logger.js';
import { monitorRouter } from './router/monitor.js'; import { monitorRouter } from './router/monitor.js';
import { healthRouter } from './router/health.js'; import { healthRouter } from './router/health.js';
@ -53,6 +54,7 @@ app.use('/api/workspace', workspaceRouter);
app.use('/monitor', monitorRouter); app.use('/monitor', monitorRouter);
app.use('/telemetry', telemetryRouter); app.use('/telemetry', telemetryRouter);
app.use('/serverStatus', serverStatusRouter); app.use('/serverStatus', serverStatusRouter);
app.use('/lh', lighthouseRouter);
app.use('/trpc', trpcExpressMiddleware); app.use('/trpc', trpcExpressMiddleware);

View File

@ -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)));
}
);

View File

@ -1,5 +1,5 @@
import puppeteer from 'puppeteer'; import puppeteer from 'puppeteer';
import lighthouse, { Result } from 'lighthouse'; import lighthouse, { Result, generateReport } from 'lighthouse';
export async function generateLighthouse(url: string): Promise<Result> { export async function generateLighthouse(url: string): Promise<Result> {
// Use Puppeteer to launch headless Chrome // Use Puppeteer to launch headless Chrome
@ -33,3 +33,7 @@ export async function generateLighthouse(url: string): Promise<Result> {
return lhr; return lhr;
} }
export function getLighthouseReport(lhr: Result): string {
return generateReport(lhr);
}