-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: admin resource 구현 * feat: 관리자 페이지 추가 * feat: cors 설정 추가 * feat: 임시로 관리자 페이지 권한 제거 * feat: admin sse api 구현 * refactor: redis sub 서비스 구현 * feat: 어드민 페이지 api 기능 추가 * feat: redis pub 서비스 추가 * refactor: admin controller에 redis service로 교체 * feat: log interceptor에 redis pub 추가 * refactor: channels const로 분리 * style: 불필요한 주석 제거 * feat: ws 로그 redis pub 추가 * feat: admin page 박스 누르면 펼치기/접기 기능 추가 * feat: admin 페이지 색깔 변경 * feat: 관리자 페이지 로그인 기능 구현
- Loading branch information
1 parent
0014655
commit 5c2f20a
Showing
18 changed files
with
577 additions
and
33 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { RedisClientType } from 'redis'; | ||
|
||
@Injectable() | ||
export class RedisService { | ||
constructor( | ||
@Inject('REDIS_SUB_CLIENT') | ||
private readonly redisSubscriber: RedisClientType, | ||
@Inject('REDIS_PUB_CLIENT') | ||
private readonly redisPublisher: RedisClientType, | ||
) {} | ||
|
||
subscribeToChannel(channel: string, callback: Function) { | ||
this.redisSubscriber.subscribe(channel, (message) => { | ||
callback(message); | ||
}); | ||
} | ||
publishToChannel(channel: string, message: string) { | ||
this.redisPublisher.publish(channel, message); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AdminController } from './admin.controller'; | ||
import { AdminService } from './admin.service'; | ||
|
||
describe('AdminController', () => { | ||
let controller: AdminController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AdminController], | ||
providers: [AdminService], | ||
}).compile(); | ||
|
||
controller = module.get<AdminController>(AdminController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Req, | ||
Res, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Response } from 'express'; | ||
import { RedisService } from 'redis/redis.service'; | ||
import { UserId } from 'src/users/decorator/userId.decorator'; | ||
import { channels } from './const/channels.const'; | ||
|
||
@Controller('admin') | ||
export class AdminController { | ||
constructor(private readonly redisService: RedisService) {} | ||
|
||
@Get('events') | ||
sse(@Req() req, @Res() res: Response, @UserId() userId: number) { | ||
res.setHeader('Content-Type', 'text/event-stream'); | ||
res.setHeader('Cache-Control', 'no-cache'); | ||
res.setHeader('Connection', 'keep-alive'); | ||
res.flushHeaders(); | ||
|
||
if (userId !== 1) { | ||
throw new UnauthorizedException('관리자가 아닙니다.'); | ||
} | ||
|
||
const changeFormat = (channel, message) => { | ||
const result = { channel, message }; | ||
return JSON.stringify(result); | ||
}; | ||
|
||
res.write(`data: ${changeFormat('notice', 'Server connected')}\n\n`); | ||
channels.forEach((channel) => { | ||
this.redisService.subscribeToChannel(channel, (message) => { | ||
res.write(`data: ${changeFormat(channel, message)}\n\n`); | ||
}); | ||
}); | ||
|
||
req.on('close', () => { | ||
res.end(); | ||
}); | ||
} | ||
@Get('generate') | ||
generate(@UserId() userId: number) { | ||
if (userId !== 1) { | ||
throw new UnauthorizedException('관리자가 아닙니다.'); | ||
} | ||
this.redisService.publishToChannel('channel', 'processAiResult'); | ||
} | ||
|
||
@Get('category') | ||
category(@UserId() userId: number) { | ||
if (userId !== 1) { | ||
throw new UnauthorizedException('관리자가 아닙니다.'); | ||
} | ||
this.redisService.publishToChannel('channel', 'processCategory'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AdminController } from './admin.controller'; | ||
import { AdminService } from './admin.service'; | ||
|
||
@Module({ | ||
controllers: [AdminController], | ||
providers: [AdminService], | ||
}) | ||
export class AdminModule {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AdminService } from './admin.service'; | ||
|
||
describe('AdminService', () => { | ||
let service: AdminService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AdminService], | ||
}).compile(); | ||
|
||
service = module.get<AdminService>(AdminService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AdminService {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const channels = [ | ||
'channel', | ||
'sharedChecklist', | ||
'ai_result', | ||
'httpLog', | ||
'wsLog', | ||
]; |
Oops, something went wrong.