강동현

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

...@@ -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,21 +14,28 @@ export class Server { ...@@ -13,21 +14,28 @@ 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;
18 - { 21 + if (process.env.NODE_ENV === "development") {
19 - cert: readFileSync( 22 + server = createServerHttp(app);
20 - "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem" 23 + } else {
21 - ), 24 + server = createServerHttps(
22 - key: readFileSync( 25 + {
23 - "/etc/letsencrypt/live/2020105578.oss2021.tk/privkey.pem" 26 + cert: readFileSync(
24 - ), 27 + "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem"
25 - ca: readFileSync( 28 + ),
26 - "/etc/letsencrypt/live/2020105578.oss2021.tk/fullchain.pem" 29 + key: readFileSync(
27 - ), 30 + "/etc/letsencrypt/live/2020105578.oss2021.tk/privkey.pem"
28 - }, 31 + ),
29 - app 32 + ca: readFileSync(
30 - ); 33 + "/etc/letsencrypt/live/2020105578.oss2021.tk/fullchain.pem"
34 + ),
35 + },
36 + app
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;
......