mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add logger
This commit is contained in:
7
dashboard/server/middleware/00-performance-start.ts
Normal file
7
dashboard/server/middleware/00-performance-start.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { logger } from "../Logger"
|
||||
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const start = Date.now();
|
||||
event.context['performance-start'] = start.toString();
|
||||
});
|
||||
@@ -24,29 +24,21 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
|
||||
const authorization = event.headers.get('Authorization');
|
||||
|
||||
if (!authorization) {
|
||||
event.context.auth = { logged: false, }
|
||||
|
||||
event.context.auth = { logged: false }
|
||||
|
||||
} else {
|
||||
|
||||
const [type, token] = authorization.split(' ');
|
||||
|
||||
const valid = readUserJwt(token);
|
||||
|
||||
if (!valid) return event.context.auth = { logged: false }
|
||||
|
||||
const user = await UserModel.findOne({ email: valid.email })
|
||||
|
||||
if (!user) return event.context.auth = { logged: false };
|
||||
|
||||
const premium: any = null;//await PremiumModel.findOne({ user_id: user.id });
|
||||
|
||||
const roles: string[] = [];
|
||||
|
||||
if (premium && premium.ends_at.getTime() < Date.now()) {
|
||||
// await PremiumModel.deleteOne({ user_id: user.id });
|
||||
} else if (premium) {
|
||||
roles.push('PREMIUM');
|
||||
roles.push('PREMIUM_' + premium.type);
|
||||
}
|
||||
|
||||
|
||||
if (ADMIN_EMAILS.includes(user.email)) {
|
||||
roles.push('ADMIN');
|
||||
}
|
||||
@@ -61,6 +53,7 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
|
||||
},
|
||||
id: user._id.toString()
|
||||
}
|
||||
|
||||
event.context.auth = authContext;
|
||||
|
||||
}
|
||||
|
||||
28
dashboard/server/middleware/02-logging.ts
Normal file
28
dashboard/server/middleware/02-logging.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { logger } from "../Logger"
|
||||
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
|
||||
const ip = getRequestAddress(event);
|
||||
const user = getRequestUser(event);
|
||||
|
||||
event.node.res.on('finish', () => {
|
||||
if (!event.context['performance-start']) return;
|
||||
const start = parseInt(event.context['performance-start']);
|
||||
if (isNaN(start)) return;
|
||||
|
||||
const end = Date.now();
|
||||
const duration = (end - start);
|
||||
|
||||
if (!user) {
|
||||
logger.debug('Request without user', { path: event.path, method: event.method, ip, duration });
|
||||
} else if (!user.logged) {
|
||||
logger.debug('Request as guest', { path: event.path, method: event.method, ip, duration });
|
||||
} else {
|
||||
logger.debug(`(${duration}ms) [${event.method}] ${event.path} { ${user.user.email} }`, { ip });
|
||||
}
|
||||
|
||||
// event.node.res.setHeader('X-Total-Response-Time', `${duration.toFixed(2)} ms`);
|
||||
});
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user