Compare commits

...

2 Commits

Author SHA1 Message Date
mozzie
ac998b9cc7 feat: 很奇怪,请求会挂 2023-10-10 22:10:22 +08:00
mozzie
3ba9704741 fear: nats log promihous 2023-10-10 21:38:59 +08:00
23 changed files with 136 additions and 94 deletions

View File

@ -27,11 +27,13 @@ import { ForbiddenExceptionFilter } from './filter/forbid.filter';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -8,11 +8,13 @@ import { ClientsModule, Transport } from '@nestjs/microservices';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -1,12 +1,12 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { getFileExt } from './app.util'; import { getFileExt } from './app.util';
import { clientConfig } from './app.config'; import { clientConfig } from './app.config';
import { MinioOSS } from '@tavi/oss'; import { MinioOSS } from '@tavi/oss';
@Controller() @Controller()
export class AppController { export class AppController {
@EventPattern('report-upload-image') @MessagePattern('report-upload-image')
async uploadImageHandler({ buffer, originalName }) { async uploadImageHandler({ buffer, originalName }) {
const objectName = Date.now() + '.' + getFileExt(originalName); const objectName = Date.now() + '.' + getFileExt(originalName);
const { bucketName, ...config } = clientConfig; const { bucketName, ...config } = clientConfig;

View File

@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>( const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule, AppModule,
{ {
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], // 可以指定链接到多个nats的消息队列 urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
); );

View File

@ -1,4 +1,4 @@
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { RedisService } from './redis/redis.service'; import { RedisService } from './redis/redis.service';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
@ -16,7 +16,7 @@ export class AppController {
* token签发, * token签发,
* @description redis的EXcookie的maxAge(ms)jwt的expiresIn * @description redis的EXcookie的maxAge(ms)jwt的expiresIn
*/ */
@EventPattern('cert.token.create') @MessagePattern('cert.token.create')
async createToken(payload) { async createToken(payload) {
const { username } = payload; const { username } = payload;
const token = await this.jwtService.sign(payload); const token = await this.jwtService.sign(payload);
@ -28,7 +28,7 @@ export class AppController {
* token有效性 && token * token有效性 && token
* @description guard对请求token进行拦截token是否合法&redis中 * @description guard对请求token进行拦截token是否合法&redis中
*/ */
@EventPattern('cert.token.decode') @MessagePattern('cert.token.decode')
async decodeToken( async decodeToken(
token: string, token: string,
): Promise<{ tokenValid: boolean; payload: unknown; error?: unknown }> { ): Promise<{ tokenValid: boolean; payload: unknown; error?: unknown }> {
@ -47,7 +47,7 @@ export class AppController {
* token在cookie中存储的key * token在cookie中存储的key
* @description cookie的maxAge单位是毫秒cookie使用expires需要 x1000 * @description cookie的maxAge单位是毫秒cookie使用expires需要 x1000
*/ */
@EventPattern('cert.token.config') @MessagePattern('cert.token.config')
async tokenConfig() { async tokenConfig() {
return { return {
tokenResignIn: this.configService.get('TOKEN_RESIGN_IN'), tokenResignIn: this.configService.get('TOKEN_RESIGN_IN'),
@ -56,7 +56,7 @@ export class AppController {
}; };
} }
@EventPattern('cert.token.deprecated') @MessagePattern('cert.token.deprecated')
async removeToken(payload) { async removeToken(payload) {
const { username } = payload; const { username } = payload;
console.log('username', username); console.log('username', username);

View File

@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>( const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule, AppModule,
{ {
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], // 可以指定链接到多个nats的消息队列 urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
); );

View File

@ -1,6 +1,6 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { RbacService } from './rbac/rbac.service'; import { RbacService } from './rbac/rbac.service';
@Controller() @Controller()
@ -10,59 +10,59 @@ export class AppController {
private readonly rbacService: RbacService, private readonly rbacService: RbacService,
) {} ) {}
@EventPattern('cert.authorize') @MessagePattern('cert.authorize')
async authorize(payload) { async authorize(payload) {
console.log('获取user的token进行payload权限的验证', payload); console.log('获取user的token进行payload权限的验证', payload);
return false; return false;
} }
@EventPattern('cert.init.role.admin') @MessagePattern('cert.init.role.admin')
async initRole() { async initRole() {
await this.rbacService.initSuperAdminRole(); await this.rbacService.initSuperAdminRole();
await this.rbacService.initSuperAdminAccount(); await this.rbacService.initSuperAdminAccount();
} }
@EventPattern('cert.create.role') @MessagePattern('cert.create.role')
async createRole(payload) { async createRole(payload) {
return await this.rbacService.createRole(payload); return await this.rbacService.createRole(payload);
} }
@EventPattern('cert.remove.role') @MessagePattern('cert.remove.role')
async removeRole(payload) { async removeRole(payload) {
return await this.rbacService.removeRole(payload); return await this.rbacService.removeRole(payload);
} }
@EventPattern('cert.find.role.all') @MessagePattern('cert.find.role.all')
async findAllRole() { async findAllRole() {
return await this.rbacService.findAllRole(); return await this.rbacService.findAllRole();
} }
@EventPattern('cert.init.permission.resource') @MessagePattern('cert.init.permission.resource')
async initPermission(payload) { async initPermission(payload) {
await this.rbacService.initPermission(payload); await this.rbacService.initPermission(payload);
} }
@EventPattern('cert.find.role.permission') @MessagePattern('cert.find.role.permission')
async findRolePermission(payload) { async findRolePermission(payload) {
return await this.rbacService.findRolePermission(payload); return await this.rbacService.findRolePermission(payload);
} }
@EventPattern('cert.update.role.permissions') @MessagePattern('cert.update.role.permissions')
async updateRolePermissions(payload) { async updateRolePermissions(payload) {
return await this.rbacService.updateRolePermissions(payload); return await this.rbacService.updateRolePermissions(payload);
} }
@EventPattern('cert.update.role') @MessagePattern('cert.update.role')
async updateRole(payload) { async updateRole(payload) {
return await this.rbacService.updateRole(payload); return await this.rbacService.updateRole(payload);
} }
@EventPattern('cert.find.all.user') @MessagePattern('cert.find.all.user')
async findAllUser() { async findAllUser() {
return await this.rbacService.findAllUser(); return await this.rbacService.findAllUser();
} }
@EventPattern('cert.role.authorize') @MessagePattern('cert.role.authorize')
async roleAuthorize({ user, url }) { async roleAuthorize({ user, url }) {
const allow = await this.rbacService.roleAuthorize(user, url); const allow = await this.rbacService.roleAuthorize(user, url);
return { allow }; return { allow };

View File

@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>( const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule, AppModule,
{ {
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], // 可以指定链接到多个nats的消息队列 urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
); );

View File

@ -1,5 +1,5 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { UserService } from './user.service'; import { UserService } from './user.service';
import { BcryptService } from '../bcrypt/bcrypt.service'; import { BcryptService } from '../bcrypt/bcrypt.service';
@ -12,7 +12,7 @@ export class UserController {
/** /**
* *
*/ */
@EventPattern('cert.user.account') @MessagePattern('cert.user.account')
async findUser( async findUser(
payload, payload,
): Promise<{ isLegal: boolean; msg?: string; data?: any }> { ): Promise<{ isLegal: boolean; msg?: string; data?: any }> {
@ -28,22 +28,22 @@ export class UserController {
return { isLegal: true, data: user }; return { isLegal: true, data: user };
} }
@EventPattern('cert.user.encrypt') @MessagePattern('cert.user.encrypt')
async encrypt(plainText: string) { async encrypt(plainText: string) {
return await this.bcryptService.hashPassword(plainText); return await this.bcryptService.hashPassword(plainText);
} }
@EventPattern('cert.encrypt.compare') @MessagePattern('cert.encrypt.compare')
async compare(plainText: string, hash: string) { async compare(plainText: string, hash: string) {
return await this.bcryptService.comparePassword(plainText, hash); return await this.bcryptService.comparePassword(plainText, hash);
} }
@EventPattern('cert.user.create') @MessagePattern('cert.user.create')
async createUser(payload) { async createUser(payload) {
return await this.userService.create(payload); return await this.userService.create(payload);
} }
@EventPattern('cert.user.update') @MessagePattern('cert.user.update')
async updateUser(payload) { async updateUser(payload) {
const { password, ...rest } = payload; const { password, ...rest } = payload;
const updateFields = password const updateFields = password
@ -52,12 +52,12 @@ export class UserController {
return await this.userService.update(updateFields); return await this.userService.update(updateFields);
} }
@EventPattern('cert.user.delete') @MessagePattern('cert.user.delete')
async deleteUser(userIds: number[]) { async deleteUser(userIds: number[]) {
return await this.userService.delete(userIds); return await this.userService.delete(userIds);
} }
@EventPattern('cert.user.find.annotator') @MessagePattern('cert.user.find.annotator')
async findAnnotators() { async findAnnotators() {
const annotators = await this.userService.findAnnotators(); const annotators = await this.userService.findAnnotators();
return { data: annotators }; return { data: annotators };

View File

@ -8,11 +8,13 @@ import { ClientsModule, Transport } from '@nestjs/microservices';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -21,11 +21,14 @@ import { AdminModule } from './admin/admin.module';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
]), ]),

View File

@ -1,6 +1,6 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import axios from 'axios'; import axios from 'axios';
@ -75,7 +75,7 @@ export class AppController {
private readonly configService: ConfigService, private readonly configService: ConfigService,
) {} ) {}
@EventPattern('dicom.find') @MessagePattern('dicom.find')
async findDicoms() { async findDicoms() {
const pacsUrl = this.configService.get('PACS_URL'); const pacsUrl = this.configService.get('PACS_URL');
try { try {
@ -118,7 +118,7 @@ export class AppController {
} }
} }
@EventPattern('dicom.archive.url') @MessagePattern('dicom.archive.url')
async archiveUrl({ ID, Type }: { ID: string; Type: string }) { async archiveUrl({ ID, Type }: { ID: string; Type: string }) {
const pacsUrl = this.configService.get('PACS_URL'); const pacsUrl = this.configService.get('PACS_URL');
const mapping = { const mapping = {

View File

@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>( const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule, AppModule,
{ {
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], // 可以指定链接到多个nats的消息队列 urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
); );

View File

@ -1,17 +1,17 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) {}
@EventPattern('archive.task.create') @MessagePattern('archive.task.create')
async createArchiveTask(payload) { async createArchiveTask(payload) {
return await this.appService.createArchiveTask(payload); return await this.appService.createArchiveTask(payload);
} }
@EventPattern('archive.task.find') @MessagePattern('archive.task.find')
async findArchiveTask(payload) { async findArchiveTask(payload) {
return await this.appService.findArchiveTask(payload); return await this.appService.findArchiveTask(payload);
} }

View File

@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>( const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule, AppModule,
{ {
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], // 可以指定链接到多个nats的消息队列 urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
); );

View File

@ -7,11 +7,13 @@ import { ClientsModule, Transport } from '@nestjs/microservices';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -7,11 +7,13 @@ import { ClientsModule, Transport } from '@nestjs/microservices';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -21,11 +21,13 @@ import * as cookieParser from 'cookie-parser';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -7,11 +7,13 @@ import { ClientsModule, Transport } from '@nestjs/microservices';
ClientsModule.register([ ClientsModule.register([
{ {
name: 'Client', name: 'Client',
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
}, },
}, },
]), ]),

View File

@ -1,6 +1,6 @@
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { EventPattern } from '@nestjs/microservices'; import { EventPattern, MessagePattern } from '@nestjs/microservices';
import * as dayjs from 'dayjs'; import * as dayjs from 'dayjs';
import { SymmetricCrypto } from '@tavi/util'; import { SymmetricCrypto } from '@tavi/util';
@ -16,7 +16,7 @@ interface UserSignLoggerDto {
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) {}
@EventPattern('logger.user.signIn') @MessagePattern('logger.user.signIn')
async userSignIn(payload: UserSignLoggerDto) { async userSignIn(payload: UserSignLoggerDto) {
const dateTime = dayjs().format('YYYY-MM-DD HH:mm:ss'); const dateTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
const { finger2, ...rest } = payload; const { finger2, ...rest } = payload;
@ -25,12 +25,12 @@ export class AppController {
return 1; return 1;
} }
@EventPattern('logger.save') @MessagePattern('logger.save')
async saveLogger(payload: UserSignLoggerDto) { async saveLogger(payload: UserSignLoggerDto) {
return 'ok'; return 'ok';
} }
@EventPattern('alg.test') @MessagePattern('alg.test')
async algTest(payload) { async algTest(payload) {
console.log('来自: alg.test', payload); console.log('来自: alg.test', payload);
return payload; return payload;

View File

@ -6,11 +6,14 @@ async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>( const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule, AppModule,
{ {
transport: Transport.NATS, transport: Transport.RMQ,
options: { options: {
servers: ['nats://localhost:4222'], // 可以指定链接到多个nats的消息队列 urls: ['amqp://localhost:5672'],
maxReconnectAttempts: 5, queue: 'cats_queue',
reconnectTimeWait: 1000, queueOptions: {
durable: false,
},
maxConnectionAttempts: 10,
}, },
}, },
); );

View File

@ -14,7 +14,7 @@ async getHello(): Promise<any> {
} }
// 服务 // 服务
@EventPattern('sum') @MessagePattern('sum')
sum(data: number[]): number { sum(data: number[]): number {
return (data || []).reduce((a, b) => a + b); return (data || []).reduce((a, b) => a + b);
} }

View File

@ -11,6 +11,13 @@ services:
environment: environment:
- RABBITMQ_DEFAULT_USER=guest - RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest - RABBITMQ_DEFAULT_PASS=guest
- TZ=Asia/Shanghai # 将此值设置为您所在的时区
# volumes:
# - rabbitmq-data:/var/lib/rabbitmq
command: >
bash -c "rabbitmq-plugins enable rabbitmq_management &&
rabbitmq-plugins enable rabbitmq_tracing &&
rabbitmq-server"
mongodb: mongodb:
image: mongo:5.0 image: mongo:5.0