diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c97dc53 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +.git +.gitignore +node_modules +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +*.log +.env +.env.* +Dockerfile +compose.yaml + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..12cab8f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM node:24-alpine + +WORKDIR /app + +ENV NODE_ENV=production +ENV PORT=3000 + +COPY package.json ./ +COPY index.html styles.css game.js server.js ./ + +EXPOSE 3000 + +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ + CMD node -e "fetch('http://127.0.0.1:' + (process.env.PORT || 3000) + '/healthz').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))" + +CMD ["node", "server.js"] + diff --git a/README.md b/README.md index ed21cfc..fc0402e 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,27 @@ A notepad-playable browser game that combines Tic Tac Toe's line-making goal wit - First player to make `4 in a row` wins. - If the board fills with no winner, the game is a draw. -## Run Multiplayer +## Run With Docker Compose + +```powershell +docker compose up --build +``` + +Then open `http://localhost:3000` in two browser windows. The first two connected players are paired automatically. + +Stop it with: + +```powershell +docker compose down +``` + +## Run Locally ```powershell npm start ``` -Then open `http://localhost:3000` in two browser windows. The first two connected players are paired automatically. +Then open `http://localhost:3000` in two browser windows. Set a different port with: diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..3823519 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,12 @@ +services: + tictactics: + build: + context: . + image: tictactics:local + container_name: tictactics + restart: unless-stopped + environment: + PORT: 3000 + ports: + - "3000:3000" + diff --git a/package.json b/package.json index 1e3ba19..9a519d4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "description": "A notepad-playable capture tic tac toe browser game with simple WebSocket multiplayer.", "scripts": { - "start": "node server.js" + "start": "node server.js", + "compose": "docker compose up --build" } } - diff --git a/server.js b/server.js index 19dd510..71129d7 100644 --- a/server.js +++ b/server.js @@ -22,6 +22,13 @@ const mimeTypes = { const server = http.createServer((request, response) => { const requestUrl = new URL(request.url, `http://${request.headers.host}`); + + if (requestUrl.pathname === "/healthz") { + response.writeHead(200, { "Content-Type": "application/json; charset=utf-8" }); + response.end(JSON.stringify({ ok: true })); + return; + } + const pathname = requestUrl.pathname === "/" ? "/index.html" : requestUrl.pathname; const filePath = path.normalize(path.join(root, pathname));