Skip to content

Commit

Permalink
✨ feat: 메인 페이지 - Status API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kms0219kms committed Apr 27, 2024
1 parent fe61c2a commit b281fe6
Show file tree
Hide file tree
Showing 7 changed files with 629 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nguard_security_api_renewal",
"version": "3.1.1",
"version": "3.1.2",
"description": "",
"author": "",
"private": true,
Expand Down Expand Up @@ -33,6 +33,7 @@
"cache-manager-redis-yet": "^5.0.0",
"discord-api-types": "^0.37.82",
"fastify": "^4.26.2",
"koreanbots": "^3.2.1",
"mongoose": "^8.3.2",
"reflect-metadata": "^0.1.14",
"rxjs": "^7.8.1"
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { redisStore } from 'cache-manager-redis-yet';
import { AppController } from './app.controller';
import { RepositoryModule } from './repository/repository.module';
import { DashboardModule } from './dashboard/dashboard.module';
import { WwwModule } from './www/www.module';

@Module({
imports: [
Expand All @@ -26,6 +27,7 @@ import { DashboardModule } from './dashboard/dashboard.module';
}),
RepositoryModule,
DashboardModule,
WwwModule,
],
controllers: [AppController],
})
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async function bootstrap() {
} else {
app.enableCors({
origin: [
'https://nguard.xyz',
'https://home-v2test.nguard.dev',
'https://console.nguard.xyz',
'https://console-v2test.nguard.dev',
'https://checkout.nguard.xyz',
Expand Down
25 changes: 25 additions & 0 deletions src/www/status/status.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller, Get, Logger } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import { StatusService } from './status.service';

@ApiTags('Main - Bot Status API')
@Controller('www/status')
export class StatusController {
private readonly logger = new Logger(StatusController.name);

constructor(private readonly statusService: StatusService) {}

@Get()
@ApiOperation({
summary: 'Bot Status',
description:
'한디리 (https://koreanbots.dev)에서 봇의 상태(서버수, 하트수)를 가져옵니다.',
})
async getStatus(): Promise<{
votes?: number;
servers?: number;
}> {
return await this.statusService.getStatus();
}
}
54 changes: 54 additions & 0 deletions src/www/status/status.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';

import { Cache } from 'cache-manager';
import { Bot, Koreanbots } from 'koreanbots';

@Injectable()
export class StatusService {
private readonly logger = new Logger(StatusService.name);

private readonly koreanbotsClient = new Koreanbots({
clientID: process.env.DISCORD_CLIENT_ID,
api: {
token: process.env.KOREANBOTS_TOKEN,
},
});

constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {}

async getStatus(): Promise<{
votes?: number;
servers?: number;
}> {
let koreanbots: Bot;

const redisCachedKoreanbots =
await this.cacheManager.get<Bot>('koreanbots');

if (redisCachedKoreanbots) {
koreanbots = redisCachedKoreanbots;
} else {
const locallyCachedKoreanbots = this.koreanbotsClient.bots.cache.get(
process.env.DISCORD_CLIENT_ID,
);

if (locallyCachedKoreanbots) {
koreanbots = locallyCachedKoreanbots;
} else {
koreanbots = await this.koreanbotsClient.bots.fetch(
process.env.DISCORD_CLIENT_ID,
);

await this.cacheManager.set('koreanbots', koreanbots, 600000);
}
}

this.logger.debug(koreanbots);

return {
servers: koreanbots.servers,
votes: koreanbots.votes,
};
}
}
11 changes: 11 additions & 0 deletions src/www/www.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';

import { StatusController } from './status/status.controller';
import { StatusService } from './status/status.service';

@Module({
imports: [],
controllers: [StatusController],
providers: [StatusService],
})
export class WwwModule {}
Loading

0 comments on commit b281fe6

Please sign in to comment.