- Add authMiddleware to all AI/transcribe routes (were unauthenticated) - Add full admin panel: user management, registration toggle, stats - Fix XSS in email verification (escape user.name in HTML) - Fix missing APP_URL fallback in password reset email - Add per-tab model selector (respects OpenRouter/Bedrock/Azure lists) - Fix transcribeAudio to send Authorization header - Fix labs input: textarea instead of single-line input - Add structured logging: audit_log, api_log, access_log tables - Add admin CLI (admin-cli.js) for Docker exec management - Fix duplicate var duration declaration in ai.js catch block - Fix RETURNING check case-sensitivity in database.js
46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
// ============================================================
|
|
// LOGGING MIDDLEWARE — Logs all API calls
|
|
// ============================================================
|
|
|
|
var logger = require('../utils/logger');
|
|
|
|
function loggingMiddleware(req, res, next) {
|
|
var startTime = Date.now();
|
|
|
|
// Capture original json method
|
|
var originalJson = res.json.bind(res);
|
|
|
|
res.json = function(data) {
|
|
var duration = Date.now() - startTime;
|
|
|
|
// Log API calls (skip static, health, models)
|
|
if (req.path.startsWith('/api/') &&
|
|
req.path !== '/api/health' &&
|
|
req.path !== '/api/models' &&
|
|
req.method !== 'GET') {
|
|
|
|
var userId = req.user ? req.user.id : null;
|
|
var responseStr = '';
|
|
try { responseStr = JSON.stringify(data); } catch(e) { responseStr = ''; }
|
|
|
|
logger.apiCall(userId, req.path, {
|
|
method: req.method,
|
|
statusCode: res.statusCode,
|
|
requestSize: parseInt(req.headers['content-length'] || 0),
|
|
responseSize: responseStr.length,
|
|
model: (data && data.model) || (req.body && req.body.model) || null,
|
|
tokensInput: (data && data.usage && data.usage.prompt_tokens) ? data.usage.prompt_tokens : 0,
|
|
tokensOutput: (data && data.usage && data.usage.completion_tokens) ? data.usage.completion_tokens : 0,
|
|
duration: duration,
|
|
ip: req.ip || (req.connection && req.connection.remoteAddress) || null,
|
|
error: (data && data.error) || null
|
|
});
|
|
}
|
|
|
|
return originalJson(data);
|
|
};
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = loggingMiddleware;
|