1
0
mirror of https://github.com/thib8956/tic-tac-toe-ws.git synced 2026-02-20 23:38:14 +00:00

feat: containerize server with Docker

This commit is contained in:
2026-02-19 13:02:50 +01:00
parent ae7c3a4662
commit 366847e1bd
8 changed files with 75 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ jobs:
mkdir dist
mv *.js dist/
mv *.html dist/
mv config.js dist/
- name: Setup Pages
id: pages
uses: actions/configure-pages@v5

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules/
*.js
!config.js

15
Dockerfile Normal file
View File

@@ -0,0 +1,15 @@
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npx tsc
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/server.js ./
COPY --from=builder /app/common.js ./
EXPOSE 1234
CMD ["node", "server.js"]

View File

@@ -1,13 +1,16 @@
import type { Click, Update, Message, Hello, EndGame, Spectate } from "common.js";
const ANIMATE_DURATION = 500; // ms
declare global {
interface Window {
TIC_TAC_TOE_CONFIG?: { WS_URL?: string };
}
}
const ANIMATE_DURATION = 500;
const GRID_PADDING = 10;
const MESSAGE_PADDING = 20;
let address = "ws://localhost:1234";
if (window.location.hostname !== "localhost") {
address = "wss://tic-tac-toe-ws-production.up.railway.app";
}
const address = window.TIC_TAC_TOE_CONFIG?.WS_URL || "ws://localhost:1234";
const ws = new WebSocket(address);
interface Point {

4
config.js Normal file
View File

@@ -0,0 +1,4 @@
window.TIC_TAC_TOE_CONFIG = {
// Set WS_URL to your production WebSocket URL, e.g.:
// WS_URL: "wss://your-domain.com/ws"
};

8
docker-compose.yml Normal file
View File

@@ -0,0 +1,8 @@
services:
server:
build: .
restart: unless-stopped
ports:
- "127.0.0.1:1234:1234"
environment:
- NODE_ENV=production

View File

@@ -8,5 +8,6 @@
<body style="margin: 0; padding: 0; overflow: hidden; background-color: #000000; touch-action: none;">
<canvas id="game"></canvas>
</body>
<script src="config.js"></script>
<script type="module" src="client.js"></script>
</html>

37
nginx.conf.example Normal file
View File

@@ -0,0 +1,37 @@
# Example nginx configuration for WebSocket reverse proxy with HTTPS
# Place this in /etc/nginx/sites-available/your-domain.com
# Then symlink to /etc/nginx/sites-enabled/
server {
listen 443 ssl http2;
server_name your-domain.com;
# SSL certificates (Let's Encrypt)
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
# SSL settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
# WebSocket endpoint
location /ws {
proxy_pass http://127.0.0.1:1234;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 86400;
}
}
# Redirect HTTP to HTTPS
server {
listen 80;
server_name your-domain.com;
return 301 https://$server_name$request_uri;
}