강동현

개발 환경에 맞게 서버 설정 조정

...@@ -18,4 +18,6 @@ RUN yarn build ...@@ -18,4 +18,6 @@ RUN yarn build
18 18
19 EXPOSE 3000 19 EXPOSE 3000
20 20
21 +ENV NODE_ENV production
22 +
21 CMD [ "node", "dist/server/index.js" ] 23 CMD [ "node", "dist/server/index.js" ]
......
1 import express from "express"; 1 import express from "express";
2 import socketIo, { Server as IoServer } from "socket.io"; 2 import socketIo, { Server as IoServer } from "socket.io";
3 -import { createServer } from "https"; 3 +import { createServer as createServerHttps } from "https";
4 +import { createServer as createServerHttp } from "http";
4 import { RoomManager } from "./room/RoomManager"; 5 import { RoomManager } from "./room/RoomManager";
5 import { Connection } from "./connection/Connection"; 6 import { Connection } from "./connection/Connection";
6 import { SocketIoWrapper } from "./connection/SocketWrapper"; 7 import { SocketIoWrapper } from "./connection/SocketWrapper";
...@@ -13,8 +14,14 @@ export class Server { ...@@ -13,8 +14,14 @@ export class Server {
13 constructor(port: number) { 14 constructor(port: number) {
14 this.port = port; 15 this.port = port;
15 16
17 + console.log(`Running in ${process.env.NODE_ENV} mode.`);
18 +
16 const app = express(); 19 const app = express();
17 - const server = createServer( 20 + let server;
21 + if (process.env.NODE_ENV === "development") {
22 + server = createServerHttp(app);
23 + } else {
24 + server = createServerHttps(
18 { 25 {
19 cert: readFileSync( 26 cert: readFileSync(
20 "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem" 27 "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem"
...@@ -28,6 +35,7 @@ export class Server { ...@@ -28,6 +35,7 @@ export class Server {
28 }, 35 },
29 app 36 app
30 ); 37 );
38 + }
31 this.io = new socketIo.Server(server, { 39 this.io = new socketIo.Server(server, {
32 cors: { 40 cors: {
33 origin: "*", 41 origin: "*",
......
1 import { Server } from "./Server"; 1 import { Server } from "./Server";
2 2
3 +process.env.NODE_ENV =
4 + process.env.NODE_ENV &&
5 + process.env.NODE_ENV.trim().toLowerCase() == "production"
6 + ? "production"
7 + : "development";
8 +
3 new Server(3000); 9 new Server(3000);
......
1 import React from "react"; 1 import React from "react";
2 import { io } from "socket.io-client"; 2 import { io } from "socket.io-client";
3 3
4 -export const socket = io(`https://${window.location.hostname}:3000/`); 4 +export const socket = io(
5 + `${window.location.protocol}//${window.location.hostname}:3000/`
6 +);
5 const SocketContext = React.createContext(socket); 7 const SocketContext = React.createContext(socket);
6 8
7 export const SocketProvider = SocketContext.Provider; 9 export const SocketProvider = SocketContext.Provider;
......