diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 526da54..77f2750 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -1,33 +1,13 @@ -import { createServer } from "http"; -import { GameManager } from "./domains/game/GameManager"; -import { RoomManager } from "./domains/room/RoomManager"; -import { SocketManager } from "./network/SocketManager"; -import { createIo } from "./network/createIo"; +import { createHttpServer } from "./network/bootstrap/createHttpServer"; +import { boot } from "./network/bootstrap/boot"; import { config } from "@repo/shared"; // サーバー待受ポート const PORT = process.env.PORT || config.NETWORK_CONFIG.DEV_SERVER_PORT; // HTTP サーバー・Socket.io サーバー生成 -const httpServer = createServer((req, res) => { - // Render の HTTP ヘルスチェック向けに 200 を返す - if (req.url === "/") { - res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" }); - res.end("ok"); - return; - } - - res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); - res.end("not found"); -}); -const io = createIo(httpServer); - -// ゲーム管理・通信管理クラス初期化 -const gameManager = new GameManager(); -const roomManager = new RoomManager(); -const socketManager = new SocketManager(io, gameManager, roomManager); - -socketManager.initialize(); +const httpServer = createHttpServer(); +boot(httpServer); // HTTP サーバー起動 httpServer.listen(PORT, () => { diff --git a/apps/server/src/network/bootstrap/boot.ts b/apps/server/src/network/bootstrap/boot.ts new file mode 100644 index 0000000..004c870 --- /dev/null +++ b/apps/server/src/network/bootstrap/boot.ts @@ -0,0 +1,14 @@ +import type { Server as HttpServer } from "http"; +import { GameManager } from "@server/domains/game/GameManager"; +import { RoomManager } from "@server/domains/room/RoomManager"; +import { SocketManager } from "../SocketManager"; +import { createIo } from "./createIo"; + +export const boot = (httpServer: HttpServer) => { + const io = createIo(httpServer); + const gameManager = new GameManager(); + const roomManager = new RoomManager(); + const socketManager = new SocketManager(io, gameManager, roomManager); + + socketManager.initialize(); +}; diff --git a/apps/server/src/network/bootstrap/createHttpServer.ts b/apps/server/src/network/bootstrap/createHttpServer.ts new file mode 100644 index 0000000..3434920 --- /dev/null +++ b/apps/server/src/network/bootstrap/createHttpServer.ts @@ -0,0 +1,14 @@ +import { createServer } from "http"; + +export const createHttpServer = () => { + return createServer((req, res) => { + if (req.url === "/") { + res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" }); + res.end("ok"); + return; + } + + res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); + res.end("not found"); + }); +}; diff --git a/apps/server/src/network/bootstrap/createIo.ts b/apps/server/src/network/bootstrap/createIo.ts new file mode 100644 index 0000000..e77fb86 --- /dev/null +++ b/apps/server/src/network/bootstrap/createIo.ts @@ -0,0 +1,12 @@ +import { Server } from "socket.io"; +import { config } from "@repo/shared"; +import type { Server as HttpServer } from "http"; + +export const createIo = (httpServer: HttpServer) => { + return new Server(httpServer, { + cors: { + origin: config.NETWORK_CONFIG.CORS_ORIGIN, + methods: [...config.NETWORK_CONFIG.CORS_METHODS], + }, + }); +};