feat(api): adopt pino request logging

This commit is contained in:
William Valentin
2025-10-17 09:54:46 -07:00
parent 37411bc890
commit 994da9a4e1
6 changed files with 69 additions and 55 deletions
@@ -1,23 +1,34 @@
import { Request, Response } from 'express';
import StreamService from '../services/streamService';
import { Request, Response } from "express";
import StreamService from "../services/streamService";
import { Logger } from "pino";
export default class StreamController {
constructor(private streamService: StreamService) { }
constructor(
private streamService: StreamService,
private logger: Logger,
) {}
// GET /api/streams/:key
async streamAudio(req: Request, res: Response) {
const { fileName: key } = req.params as { fileName: string };
const range = req.headers.range as string | undefined;
if (!key) return res.status(400).send('Missing file key');
// GET /api/streams/:key
async streamAudio(req: Request, res: Response) {
const { fileName: key } = req.params as { fileName: string };
const range = req.headers.range as string | undefined;
if (!key) return res.status(400).send("Missing file key");
try {
const result = await this.streamService.streamFromS3({ key, range });
res.writeHead(result.status, result.headers);
result.body.pipe(res);
} catch (err: any) {
const message = err?.message || 'Error streaming audio';
const status = err?.statusCode || 500;
res.status(status).send(message);
}
try {
this.logger.info(
`Attempting to stream file: ${key}, range: ${range || "none"}`,
);
const result = await this.streamService.streamFromS3({ key, range });
this.logger.info(
`Streaming successful for ${key}, status: ${result.status}, content-length: ${result.headers["Content-Length"]}`,
);
res.writeHead(result.status, result.headers);
result.body.pipe(res);
} catch (err: any) {
this.logger.error({ err, key }, `Error streaming audio file ${key}`);
const message = err?.message || "Error streaming audio";
const status = err?.statusCode || 500;
res.status(status).send(message);
}
}
}
}