feat: add public stats dashboard and request/token tracking
This commit is contained in:
parent
9850334202
commit
f984a7e8e2
5 changed files with 1273 additions and 1 deletions
|
|
@ -1,11 +1,13 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { config } from './config';
|
||||
import { requestLogger } from './logger';
|
||||
import { statsTracker } from './stats';
|
||||
|
||||
export const authenticateApiKey = (req: Request, res: Response, next: NextFunction): void => {
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
statsTracker.recordDirectFailure('missing_auth_header', req.path);
|
||||
const requestId = requestLogger.generateRequestId();
|
||||
requestLogger.logError(requestId, { message: 'Missing Authorization header', path: req.path });
|
||||
res.status(401).json({
|
||||
|
|
@ -21,6 +23,7 @@ export const authenticateApiKey = (req: Request, res: Response, next: NextFuncti
|
|||
// OpenAI format: "Bearer <api_key>"
|
||||
const parts = authHeader.split(' ');
|
||||
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
||||
statsTracker.recordDirectFailure('invalid_auth_format', req.path);
|
||||
const requestId = requestLogger.generateRequestId();
|
||||
requestLogger.logError(requestId, { message: 'Invalid Authorization format', path: req.path });
|
||||
res.status(401).json({
|
||||
|
|
@ -36,6 +39,7 @@ export const authenticateApiKey = (req: Request, res: Response, next: NextFuncti
|
|||
const providedKey = parts[1];
|
||||
|
||||
if (providedKey !== config.apiPassword) {
|
||||
statsTracker.recordDirectFailure('invalid_api_key', req.path);
|
||||
const requestId = requestLogger.generateRequestId();
|
||||
requestLogger.logError(requestId, { message: 'Invalid API key', path: req.path });
|
||||
res.status(401).json({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue