Skip to content

Commit e8206a8

Browse files
authored
Merge pull request #48 from abbastoof/chore/030-setup-game-server-socketio
Chore/030 setup game server socketio
2 parents fc7874e + 2617a0f commit e8206a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2844
-2139
lines changed

Diff for: Frontend/Dockerfile

+18-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Base stage
1+
# Frontend stage
22
FROM alpine:3.20 AS frontend
33

44
# Install necessary packages
5-
RUN apk update && apk add --no-cache nginx openrc openssl openssh curl bash nodejs npm
5+
RUN apk update && apk add --no-cache nginx openrc openssl openssh curl bash nodejs npm websocat
66

77
# Set up the application directory
88
RUN addgroup -g 1000 www && adduser -D -u 1000 -G www www && \
@@ -15,40 +15,34 @@ RUN addgroup -g 1000 www && adduser -D -u 1000 -G www www && \
1515
chown -R www:www /etc/nginx/ssl && \
1616
chown -R www:www /etc/nginx
1717

18-
COPY --chown=www:www ./Frontend/src /www/
19-
COPY --chown=www:www ./Frontend/default.jpg /www/avatars/
18+
# Set working directory
2019
WORKDIR /app
2120

22-
# Copy shared files
23-
COPY --chown=www:www ./Frontend/tools.sh /app/tools.sh
24-
RUN chmod +x /app/tools.sh
25-
2621
# Copy application files
2722
COPY --chown=www:www ./Frontend/package.json /app/package.json
2823
COPY --chown=www:www ./Frontend/package-lock.json /app/package-lock.json
2924
COPY --chown=www:www ./Frontend/vite.config.js /app/vite.config.js
30-
31-
# Set the working directory
32-
# WORKDIR /app
33-
34-
# Copy your frontend application files
35-
#COPY --chown=www:www ./Frontend/src /www/
36-
WORKDIR /app
3725
COPY --chown=www:www ./Frontend/src /app/src
38-
RUN chown -R www:www /app
26+
COPY --chown=www:www ./Frontend/src /www/
27+
COPY --chown=www:www ./Frontend/default.jpg /www/avatars/
28+
COPY --chown=www:www ./Frontend/tools.sh /app/tools.sh
3929

4030
# Install npm dependencies
4131
RUN npm cache clean --force && \
42-
rm -rf node_modules package-lock.json && \
43-
npm install
32+
rm -rf node_modules && \
33+
npm install && \
34+
npm cache verify
4435

4536
# Development stage
4637
FROM frontend AS development
4738

39+
# Copy Nginx configuration for development
40+
COPY --chown=www:www ./Frontend/nginx.conf.dev /etc/nginx/nginx.conf
41+
4842
# Expose port for Vite development server
4943
EXPOSE 3000
5044

51-
# Development command
45+
# Run Vite and Nginx
5246
CMD ["sh", "/app/tools.sh"]
5347

5448
# Production stage
@@ -57,27 +51,24 @@ FROM frontend AS production
5751
# Build the project
5852
RUN npm run build
5953

60-
# Copy the built files to the nginx directory
54+
# Copy the built files to the Nginx directory
6155
RUN cp -r /app/dist/* /www
6256

63-
# Copy nginx configuration
57+
# Copy Nginx configuration for production
6458
COPY --chown=www:www ./Frontend/nginx.conf /etc/nginx/nginx.conf
59+
60+
# Generate self-signed SSL certificates
6561
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx-selfsigned.key \
6662
-out /etc/nginx/ssl/nginx-selfsigned.crt -subj \
6763
"/C=FI/ST=UUSIMAA/L=HELSINKI/O=HIVE/OU=HIVE/CN=localhost"
6864

65+
# Fix permissions
6966
RUN chmod 600 /etc/nginx/ssl/nginx-selfsigned.key /etc/nginx/ssl/nginx-selfsigned.crt && \
7067
chown -R www:www /var/lib/nginx/logs/ /run/nginx /etc/nginx/ssl
7168

72-
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -sSf http://localhost:443 > /dev/null && echo "success" || echo "failure"
73-
RUN chown -R www:www /run/nginx
74-
7569
# Expose port for Nginx server
7670
EXPOSE 443
7771

78-
# Health check
79-
# HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -sSf http://localhost:443 > /dev/null && echo "success" || echo "failure"
80-
8172
# Set up the final command
8273
USER www
8374
CMD ["sh", "/app/tools.sh"]

Diff for: Frontend/nginx.conf

+67-45
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
user www;
2-
worker_processes auto; # it will be determined automatically by the number of cores
3-
4-
error_log /var/log/nginx/error.log warn;
5-
#pid /var/run/nginx/nginx.pid; # it permits you to use rc-service nginx reload|restart|stop|start
1+
user www;
2+
worker_processes auto;
63

4+
error_log /var/log/nginx/error.log info;
75
events {
8-
worker_connections 1024;
6+
worker_connections 1024;
97
}
108

119
http {
12-
include /etc/nginx/mime.types;
13-
default_type application/octet-stream;
14-
sendfile on;
15-
access_log /var/log/nginx/access.log;
16-
keepalive_timeout 3000;
10+
include /etc/nginx/mime.types;
11+
default_type application/octet-stream;
12+
sendfile on;
13+
access_log /var/log/nginx/access.log;
14+
keepalive_timeout 3000;
1715

16+
# Define upstream servers
1817
upstream token-service {
1918
server token-service:8000;
2019
}
@@ -27,47 +26,56 @@ http {
2726
server game-history:8002;
2827
}
2928

29+
upstream game-server {
30+
server game-server:8010;
31+
}
32+
33+
# Redirect HTTP to HTTPS
3034
server {
31-
listen 80;
32-
server_name localhost;
35+
listen 80;
36+
server_name localhost;
3337

34-
# Redirect all HTTP requests to HTTPS
3538
location / {
3639
return 301 https://$host$request_uri;
3740
}
3841
}
3942

43+
# HTTPS server block
4044
server {
41-
listen 443 ssl;
42-
ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
43-
ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;
44-
ssl_protocols TLSv1.2 TLSv1.3;
45-
root /www;
46-
index index.html index.htm;
47-
server_name localhost;
48-
client_max_body_size 32m;
49-
error_page 500 502 503 504 /50x.html;
45+
listen 443 ssl;
46+
server_name localhost;
47+
ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
48+
ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;
49+
ssl_protocols TLSv1.2 TLSv1.3;
50+
ssl_prefer_server_ciphers on;
51+
52+
root /www;
53+
index index.html index.htm;
54+
client_max_body_size 32m;
55+
error_page 500 502 503 504 /50x.html;
56+
5057
location = /50x.html {
51-
root /var/lib/nginx/html;
58+
root /var/lib/nginx/html;
5259
}
53-
54-
# Serve JavaScript files
60+
61+
# Serve static files
5562
location / {
5663
try_files $uri $uri/ /index.html;
5764
}
5865

66+
# Serve media files
5967
location /media/ {
6068
alias /www/avatars/;
6169
}
62-
# Proxy requests to the authentication service
70+
71+
# Proxy requests to the token service
6372
location /auth/ {
64-
proxy_pass http://token-service;
65-
proxy_set_header Host $host;
66-
proxy_set_header X-Real-IP $remote_addr;
67-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
68-
proxy_set_header X-Forwarded-Proto $scheme;
73+
proxy_pass http://token-service;
74+
proxy_set_header Host $host;
75+
proxy_set_header X-Real-IP $remote_addr;
76+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
77+
proxy_set_header X-Forwarded-Proto $scheme;
6978

70-
# Add CORS headers for /auth requests
7179
add_header Access-Control-Allow-Origin *;
7280
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
7381
add_header Access-Control-Allow-Headers 'Authorization, Content-Type';
@@ -79,21 +87,35 @@ http {
7987
}
8088
}
8189

90+
# Proxy requests to the user service
8291
location /user/ {
83-
proxy_pass http://user-service;
84-
proxy_set_header Host $host;
85-
proxy_set_header X-Real-IP $remote_addr;
86-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
87-
proxy_set_header X-Forwarded-Proto $scheme;
92+
proxy_pass http://user-service;
93+
proxy_set_header Host $host;
94+
proxy_set_header X-Real-IP $remote_addr;
95+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
96+
proxy_set_header X-Forwarded-Proto $scheme;
8897
}
8998

99+
# Proxy requests to the game history service
90100
location /game-history/ {
91-
proxy_pass http://game-history;
92-
proxy_set_header Host $host;
93-
proxy_set_header X-Real-IP $remote_addr;
94-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
95-
proxy_set_header X-Forwarded-Proto $scheme;
101+
proxy_pass http://game-history;
102+
proxy_set_header Host $host;
103+
proxy_set_header X-Real-IP $remote_addr;
104+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
105+
proxy_set_header X-Forwarded-Proto $scheme;
106+
}
107+
108+
# Proxy WebSocket and HTTP requests to the game server
109+
location /game-server/socket.io/ {
110+
rewrite ^/game-server/socket.io/(.*) /socket.io/$1 break;
111+
proxy_pass http://game-server;
112+
proxy_set_header Host $host;
113+
proxy_set_header X-Real-IP $remote_addr;
114+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
115+
proxy_set_header X-Forwarded-Proto $scheme;
116+
proxy_http_version 1.1;
117+
proxy_set_header Upgrade $http_upgrade;
118+
proxy_set_header Connection 'upgrade';
96119
}
97-
98120
}
99-
}
121+
}

Diff for: Frontend/nginx.conf.dev

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
user www;
2+
worker_processes auto;
3+
4+
error_log /var/log/nginx/error.log info;
5+
events {
6+
worker_connections 1024;
7+
}
8+
9+
http {
10+
include /etc/nginx/mime.types;
11+
default_type application/octet-stream;
12+
sendfile on;
13+
access_log /var/log/nginx/access.log;
14+
keepalive_timeout 3000;
15+
16+
# Server to handle frontend development
17+
server {
18+
listen 80;
19+
server_name localhost;
20+
21+
# Proxy requests to Vite development server
22+
location / {
23+
proxy_pass http://frontend:3000; # Forward to Vite server
24+
proxy_set_header Host $host;
25+
proxy_set_header X-Real-IP $remote_addr;
26+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
27+
proxy_set_header X-Forwarded-Proto $scheme;
28+
}
29+
30+
# WebSocket requests
31+
location /game-server/socket.io/ {
32+
proxy_pass http://game-server:8010/socket.io;
33+
proxy_set_header Host $host;
34+
proxy_set_header X-Real-IP $remote_addr;
35+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36+
proxy_set_header X-Forwarded-Proto $scheme;
37+
proxy_http_version 1.1;
38+
proxy_set_header Upgrade $http_upgrade;
39+
proxy_set_header Connection 'upgrade';
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)