mirror of
https://github.com/thib8956/tic-tac-toe-ws.git
synced 2026-02-21 07:48:12 +00:00
feat: containerize server with Docker
This commit is contained in:
1
.github/workflows/main.yml
vendored
1
.github/workflows/main.yml
vendored
@@ -19,6 +19,7 @@ jobs:
|
|||||||
mkdir dist
|
mkdir dist
|
||||||
mv *.js dist/
|
mv *.js dist/
|
||||||
mv *.html dist/
|
mv *.html dist/
|
||||||
|
mv config.js dist/
|
||||||
- name: Setup Pages
|
- name: Setup Pages
|
||||||
id: pages
|
id: pages
|
||||||
uses: actions/configure-pages@v5
|
uses: actions/configure-pages@v5
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
*.js
|
*.js
|
||||||
|
!config.js
|
||||||
|
|||||||
15
Dockerfile
Normal file
15
Dockerfile
Normal 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"]
|
||||||
13
client.ts
13
client.ts
@@ -1,13 +1,16 @@
|
|||||||
import type { Click, Update, Message, Hello, EndGame, Spectate } from "common.js";
|
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 GRID_PADDING = 10;
|
||||||
const MESSAGE_PADDING = 20;
|
const MESSAGE_PADDING = 20;
|
||||||
|
|
||||||
let address = "ws://localhost:1234";
|
const address = window.TIC_TAC_TOE_CONFIG?.WS_URL || "ws://localhost:1234";
|
||||||
if (window.location.hostname !== "localhost") {
|
|
||||||
address = "wss://tic-tac-toe-ws-production.up.railway.app";
|
|
||||||
}
|
|
||||||
const ws = new WebSocket(address);
|
const ws = new WebSocket(address);
|
||||||
|
|
||||||
interface Point {
|
interface Point {
|
||||||
|
|||||||
4
config.js
Normal file
4
config.js
Normal 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
8
docker-compose.yml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
services:
|
||||||
|
server:
|
||||||
|
build: .
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:1234:1234"
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=production
|
||||||
@@ -8,5 +8,6 @@
|
|||||||
<body style="margin: 0; padding: 0; overflow: hidden; background-color: #000000; touch-action: none;">
|
<body style="margin: 0; padding: 0; overflow: hidden; background-color: #000000; touch-action: none;">
|
||||||
<canvas id="game"></canvas>
|
<canvas id="game"></canvas>
|
||||||
</body>
|
</body>
|
||||||
|
<script src="config.js"></script>
|
||||||
<script type="module" src="client.js"></script>
|
<script type="module" src="client.js"></script>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
37
nginx.conf.example
Normal file
37
nginx.conf.example
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user