This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller() | |
export class ChatController { | |
constructor(private readonly chatService: ChatService) { | |
} | |
@UseGuards(JwtGuard) | |
@GrpcMethod('ChatService', 'GetChat') | |
@UseFilters(RpcExceptionFilter.for('ChatService::getChat')) | |
public getChat(data: Identity<chatApiTypes.chat.Stub>): Observable<chatApi.chat.ChatList> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class ChatService { | |
constructor( | |
private readonly messageDataFinder: MessageDataFinder, | |
private readonly chatEventService: ChatEventService, | |
) { | |
} | |
public getChatStream(): Observable<api.chat.ChatList> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class MessageService implements OnModuleInit { | |
@Client(grpcUser) private readonly grpcUserClient: ClientGrpc; | |
private grpcUserService: userApi.user.UserService; | |
constructor( | |
private readonly messageDataProducer: MessageDataProducer, | |
private readonly messageDataUpdater: MessageDataUpdater, | |
private readonly messageDataRemover: MessageDataRemover, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class ChatEventService { | |
private readonly updates$ = new Subject<api.chat.Message[]>(); | |
public emit(message: api.chat.Message): void { | |
this.updates$.next([message]); | |
} | |
public broadcast(): Observable<api.chat.Message[]> { | |
return this.updates$.asObservable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrpcMethod('AuthService', 'Auth') | |
@UseFilters(RpcExceptionFilter.for('AuthController::auth')) | |
public auth(data: AuthReqDTO): Observable<authApi.auth.AuthRes> { | |
return this.grpcUserService.verifyUser(data, meta).pipe( | |
map(user => this.jwtService.addToken(user)), | |
map(token => ({ token })), | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrpcMethod('AuthService', 'GetCertStream') | |
@UseFilters(RpcExceptionFilter.for('AuthController::getCertStream')) | |
public getCertStream(data: Identity<authTypes.auth.Stub>): Observable<authApi.auth.GetCertStreamRes> { | |
return this.certSubscribeService.getCert() | |
.pipe(map(key => ({ key }))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TOKEN_HEADER_NAME = 'authorization'; | |
const DECODING_OPTIONS = { | |
algorithms: ['RS256'], | |
}; | |
export class JwtGuard implements CanActivate { | |
canActivate(context: ExecutionContext): boolean { | |
const meta = context.getArgByIndex(1); | |
const token = meta.get(TOKEN_HEADER_NAME)[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Catch(...EXCEPTION_LIST) | |
export class RpcExceptionFilter extends BaseRpcExceptionFilter { | |
private readonly exceptionHandlerFactory: IExceptionHandlerFactory; | |
public static for(label: string): RpcExceptionFilter { | |
return new RpcExceptionFilter(label); | |
} | |
protected constructor(protected readonly label: string) { | |
super(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrpcMethod('UserService', 'VerifyUser') | |
@UseFilters(RpcExceptionFilter.for('UserController::verifyUser')) | |
public verifyUser(data: VerifyUserReqDTO, meta: Metadata): Observable<userTypes.user.User> { | |
/* for example metadata */ | |
this.logger.info(meta); | |
/* for example metadata */ | |
return this.userService.verifyUser(data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrpcMethod('AuthService', 'Auth') | |
@UseFilters(RpcExceptionFilter.for('AuthController::auth')) | |
public auth(data: AuthReqDTO): Observable<authApi.auth.AuthRes> { | |
/* for example metadata */ | |
const meta = new Metadata(); | |
meta.add('X-Grpc-From', 'auth'); | |
/* for example metadata */ | |
return this.grpcUserService.verifyUser(data, meta).pipe( | |
map(user => this.jwtService.addToken(user)), |