Merge branch 'develop'

This commit is contained in:
kittapath 2024-12-18 19:02:20 +07:00
commit 1ed7fa4a45
2 changed files with 54 additions and 44 deletions

View file

@ -189,12 +189,12 @@ class CheckAuth {
port: process.env.REDIS_PORT,
});
const getAsync = promisify(redisClient.get).bind(redisClient);
let reply = await getAsync("org_" + keycloakId);
if (reply != null) {
reply = JSON.parse(reply);
} else {
if (!keycloakId) throw "Error calling API No KeycloakId";
try {
try {
let reply = await getAsync("org_" + keycloakId);
if (reply != null) {
reply = JSON.parse(reply);
} else {
if (!keycloakId) throw new Error("No KeycloakId provided");
const x = await new CallAPI().GetData(
{
headers: { authorization: token },
@ -212,10 +212,10 @@ class CheckAuth {
};
return data;
} catch (error) {
console.error("Error calling API:", error);
throw error;
}
} catch (error) {
console.error("Error calling API:", error);
throw error;
}
}
public async PermissionCreate(req: RequestWithUser, system: string) {

View file

@ -19,12 +19,10 @@ const LOG_LEVEL_MAP: Record<string, number> = {
const elasticsearch = new Client({
node: `${process.env.ELASTICSEARCH_PROTOCOL}://${process.env.ELASTICSEARCH_HOST}:${process.env.ELASTICSEARCH_PORT}`,
});
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
if (!req.url.startsWith("/api/")) return next();
let data: any;
let data: any;
const originalJson = res.json;
res.json = function (v: any) {
@ -37,41 +35,53 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
req.app.locals.logData = {};
res.on("finish", async() => {
if (!req.url.startsWith("/api/")) return;
res.on("finish", async () => {
try {
if (!req.url.startsWith("/api/")) return;
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
if (level === 1 && res.statusCode < 500) return;
if (level === 2 && res.statusCode < 400) return;
if (level === 3 && res.statusCode < 200) return;
let token: any;
token = req.headers['authorization']
const rootId = await new permission().checkOrg(token,req.app.locals.logData.userId);
const obj = {
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
ip: req.ip,
rootId: rootId?rootId.orgRootId:null,
systemName: "development",
startTimeStamp: timestamp,
endTimeStamp: new Date().toISOString(),
processTime: performance.now() - start,
host: req.hostname,
method: req.method,
endpoint: req.url,
responseCode: String(res.statusCode === 304 ? 200 : res.statusCode),
responseDescription: data?.message,
input: (level === 4 && JSON.stringify(req.body, null, 2)) || undefined,
output: (level === 4 && JSON.stringify(data, null, 2)) || undefined,
...req.app.locals.logData,
};
if (level === 1 && res.statusCode < 500) return;
if (level === 2 && res.statusCode < 400) return;
if (level === 3 && res.statusCode < 200) return;
elasticsearch.index({
index: ELASTICSEARCH_INDEX,
document: obj,
});
const token = req.headers["authorization"];
let rootId = null;
try {
rootId = token
? await new permission().checkOrg(token, req.app.locals.logData.userId)
: null;
} catch (err) {
console.warn("Error fetching rootId:", err);
}
const obj = {
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
ip: req.ip,
rootId: rootId?.orgRootId ?? null,
systemName: "evaluation",
startTimeStamp: timestamp,
endTimeStamp: new Date().toISOString(),
processTime: performance.now() - start,
host: req.hostname,
method: req.method,
endpoint: req.url,
responseCode: String(res.statusCode === 304 ? 200 : res.statusCode),
responseDescription: data?.message,
input: level === 4 ? JSON.stringify(req.body, null, 2) : undefined,
output: level === 4 ? JSON.stringify(data, null, 2) : undefined,
...req.app.locals.logData,
};
// Send log to Elasticsearch
await elasticsearch.index({
index: ELASTICSEARCH_INDEX,
document: obj,
});
} catch (err) {
console.error("Error in logMiddleware:", err);
}
});
return next();