diff --git a/docker-compose.yml b/docker-compose.yml index 5e5baf4..0ccd399 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -45,6 +45,8 @@ services: SESSION_SECRET: ${SESSION_SECRET} CLIENT_ORIGIN: ${CLIENT_ORIGIN} ENCODING_URL: rtmp://172.20.0.10/stream + PORT: 3000 + NODE_ENV: development rtmp-server: build: ./server/rtmp-server diff --git a/server/api-server/package.json b/server/api-server/package.json index 853e677..2acbe62 100644 --- a/server/api-server/package.json +++ b/server/api-server/package.json @@ -8,7 +8,7 @@ "scripts": { "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", - "start": "NODE_ENV=production nest start", + "start": "nest start", "start:dev": "NODE_ENV=development nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "NODE_ENV=production node dist/main", diff --git a/server/api-server/src/chat/chat.gateway.ts b/server/api-server/src/chat/chat.gateway.ts index 2f16c50..66f41ad 100644 --- a/server/api-server/src/chat/chat.gateway.ts +++ b/server/api-server/src/chat/chat.gateway.ts @@ -11,7 +11,9 @@ import { Server, Socket } from 'socket.io'; import { JoinPayload } from './dto/join-payload.dto'; import { ChatPayload } from './dto/chat-payload'; import { Logger } from '@nestjs/common'; -import { UsersService } from 'src/users/users.service'; +import { User } from 'src/users/entities/user.entity'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; @WebSocketGateway({ cors: { @@ -20,15 +22,21 @@ import { UsersService } from 'src/users/users.service'; }, }) export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { - constructor(private readonly userService: UsersService) {} + constructor(@InjectRepository(User) private userRepo: Repository) {} @WebSocketServer() server: Server = new Server({ cookie: true }); private readonly logger = new Logger(ChatGateway.name); async handleConnection(client: Socket) { - const id = client.handshake['session'].userId || ''; + const userId = client.handshake['session'].userId || ''; + + let user: User; + try { + user = await this.userRepo.findOne({ where: { userId } }); + } catch (e) { + this.logger.error(e); + } - const user = await this.userService.findOne(id); client.data.userId = user?.userId || 'anonymous'; client.data.nickname = user?.nickname || '익명'; diff --git a/server/api-server/src/main.ts b/server/api-server/src/main.ts index f1f0292..51ef4ff 100644 --- a/server/api-server/src/main.ts +++ b/server/api-server/src/main.ts @@ -8,22 +8,24 @@ import * as http from 'http'; import * as express from 'express'; import { ExpressAdapter } from '@nestjs/platform-express'; import * as process from 'process'; +import { INestApplication } from '@nestjs/common'; async function bootstrap() { - const httpsOptions = { - key: fs.readFileSync(process.env.KEY_PATH), - cert: fs.readFileSync(process.env.CERT_PATH), - }; + let app: INestApplication; const server = express(); - const app = await NestFactory.create(AppModule, new ExpressAdapter(server), { - httpsOptions, - }); - app.enableCors({ - origin: process.env.CLIENT_ORIGIN, - methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', - credentials: true, - }); + if (process.env.NODE_ENV === 'production') { + const httpsOptions = { + key: fs.readFileSync(process.env.KEY_PATH), + cert: fs.readFileSync(process.env.CERT_PATH), + }; + + app = await NestFactory.create(AppModule, new ExpressAdapter(server), { + httpsOptions, + }); + } else { + app = await NestFactory.create(AppModule); + } app.enableCors({ origin: process.env.CLIENT_ORIGIN, @@ -40,10 +42,12 @@ async function bootstrap() { await redisIoAdapter.connectToRedis(); app.useWebSocketAdapter(redisIoAdapter); - await app.init(); + if (process.env.NOD_ENV === 'production') { + await app.init(); + http.createServer(server).listen(3000); + } - app.listen(443); - http.createServer(server).listen(3000); + app.listen(process.env.PORT); } bootstrap();