diff --git a/src/server/main.ts b/src/server/main.ts index d387f3e..64cc44f 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -73,14 +73,14 @@ if (env.allowOpenapi) { } // fallback -app.get('/*', (req, res) => { - if (req.accepts('html')) { +app.use('/*', (req, res) => { + if (req.method === 'GET' && req.accepts('html')) { res.sendFile(path.join(process.cwd(), 'public', 'index.html')); } }); app.use((err: any, req: any, res: any, next: any) => { - logger.error(err); + logger.error('[express]', err); res.status(500).json({ message: err.message }); }); diff --git a/src/server/model/telemetry.ts b/src/server/model/telemetry.ts index 3e151b5..d4baaec 100644 --- a/src/server/model/telemetry.ts +++ b/src/server/model/telemetry.ts @@ -22,6 +22,8 @@ export async function recordTelemetryEvent(req: Request) { return; } + const telemetryId = req.params.telemetryId; + const { origin, pathname } = new URL(url); const payload = Object.keys(others).length > 0 ? others : undefined; @@ -29,6 +31,7 @@ export async function recordTelemetryEvent(req: Request) { data: { sessionId: session.id, workspaceId, + telemetryId, eventName, urlOrigin: origin, urlPath: pathname, diff --git a/src/server/router/telemetry.ts b/src/server/router/telemetry.ts index a2adc06..10db74b 100644 --- a/src/server/router/telemetry.ts +++ b/src/server/router/telemetry.ts @@ -6,6 +6,11 @@ import { makeBadge } from 'badge-maker'; export const telemetryRouter = Router(); +const blankGifBuffer = Buffer.from( + 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', + 'base64' +); + telemetryRouter.get( '/:workspaceId/blank.gif', validate( @@ -13,14 +18,9 @@ telemetryRouter.get( query('url').optional().isURL() ), async (req, res) => { - const buffer = Buffer.from( - 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', - 'base64' - ); - recordTelemetryEvent(req); - res.header('Content-Type', 'image/gif').send(buffer); + res.header('Content-Type', 'image/gif').send(blankGifBuffer); } ); @@ -46,3 +46,41 @@ telemetryRouter.get( res.header('Content-Type', 'image/svg+xml').send(svg); } ); + +telemetryRouter.get( + '/:workspaceId/:telemetryId.gif', + validate( + query('name').optional().isString(), + query('url').optional().isURL() + ), + async (req, res) => { + recordTelemetryEvent(req); + + res.header('Content-Type', 'image/gif').status(200).send(blankGifBuffer); + } +); + +telemetryRouter.get( + '/:workspaceId/:telemetryId/badge.svg', + validate( + query('name').optional().isString(), + query('url').optional().isURL(), + query('title').optional().isString(), + query('start').optional().isNumeric() + ), + async (req, res) => { + const title = req.query.title || 'visitor'; + const start = req.query.start ? Number(req.query.start) : 0; + + recordTelemetryEvent(req); + const num = await sumTelemetryEvent(req); + + const svg = makeBadge({ + label: String(title), + message: numify(num + start), + color: 'green', + }); + + res.header('Content-Type', 'image/svg+xml').status(200).send(svg); + } +);