mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-23 20:18:42 +08:00
fix(server): set reqeust-id on ClsModule setup (#9829)
This commit is contained in:
@@ -62,6 +62,8 @@ test('should init app', async t => {
|
|||||||
|
|
||||||
t.is(config.type, 'Affine');
|
t.is(config.type, 'Affine');
|
||||||
t.true(Array.isArray(config.features));
|
t.true(Array.isArray(config.features));
|
||||||
|
// make sure the request id is set
|
||||||
|
t.truthy(response.headers['x-request-id']);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return 404 for unknown path', async t => {
|
test('should return 404 for unknown path', async t => {
|
||||||
@@ -76,4 +78,6 @@ test('should be able to call apis', async t => {
|
|||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
t.is(res.body.flavor, 'graphql');
|
t.is(res.body.flavor, 'graphql');
|
||||||
|
// make sure the request id is set
|
||||||
|
t.truthy(res.headers['x-request-id']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { ScheduleModule } from '@nestjs/schedule';
|
|||||||
import { ClsPluginTransactional } from '@nestjs-cls/transactional';
|
import { ClsPluginTransactional } from '@nestjs-cls/transactional';
|
||||||
import { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
|
import { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
import { Response } from 'express';
|
||||||
import { get } from 'lodash-es';
|
import { get } from 'lodash-es';
|
||||||
import { ClsModule } from 'nestjs-cls';
|
import { ClsModule } from 'nestjs-cls';
|
||||||
|
|
||||||
@@ -57,6 +58,9 @@ export const FunctionalityModules = [
|
|||||||
// make every request has a unique id to tracing
|
// make every request has a unique id to tracing
|
||||||
return randomUUID();
|
return randomUUID();
|
||||||
},
|
},
|
||||||
|
setup(cls, _req, res: Response) {
|
||||||
|
res.setHeader('X-Request-Id', cls.getId());
|
||||||
|
},
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
// https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/prisma-adapter
|
// https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/prisma-adapter
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import {
|
|||||||
import { SocketIoAdapter } from './base/websocket';
|
import { SocketIoAdapter } from './base/websocket';
|
||||||
import { AuthGuard } from './core/auth';
|
import { AuthGuard } from './core/auth';
|
||||||
import { ENABLED_FEATURES } from './core/config/server-feature';
|
import { ENABLED_FEATURES } from './core/config/server-feature';
|
||||||
import { responseRequestIdHeader } from './middleware/request-id';
|
|
||||||
import { serverTimingAndCache } from './middleware/timing';
|
import { serverTimingAndCache } from './middleware/timing';
|
||||||
|
|
||||||
export async function createApp() {
|
export async function createApp() {
|
||||||
@@ -32,7 +31,6 @@ export async function createApp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.use(serverTimingAndCache);
|
app.use(serverTimingAndCache);
|
||||||
app.use(responseRequestIdHeader);
|
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
graphqlUploadExpress({
|
graphqlUploadExpress({
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
import { NextFunction, Request, Response } from 'express';
|
|
||||||
import { ClsServiceManager } from 'nestjs-cls';
|
|
||||||
import onHeaders from 'on-headers';
|
|
||||||
|
|
||||||
export const responseRequestIdHeader = (
|
|
||||||
_req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) => {
|
|
||||||
onHeaders(res, () => {
|
|
||||||
const requestId = ClsServiceManager.getClsService().getId();
|
|
||||||
res.setHeader('X-Request-Id', requestId);
|
|
||||||
});
|
|
||||||
|
|
||||||
next();
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user