Skip to content

Commit 2617a0f

Browse files
committed
pulled from develop to solve the conflicts
2 parents 18af48a + 0945d06 commit 2617a0f

Some content is hidden

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

42 files changed

+589
-447
lines changed

Backend/game_history/Dockerfile

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
FROM alpine:3.20
1+
FROM python:3.11-alpine3.20
22

33
ENV PYTHONUNBUFFERED=1
44
ENV LANG=C.UTF-8
55

66
# Update and install dependencies
77
# trunk-ignore(hadolint/DL3018)
8-
RUN apk update && apk add --no-cache python3 py3-pip \
9-
postgresql16 postgresql16-client \
10-
bash supervisor curl openssl bash \
8+
RUN apk add --no-cache supervisor \
9+
curl openssl bash postgresql16-client \
1110
build-base libffi-dev python3-dev
1211

13-
14-
# Set work directory
15-
RUN mkdir /run/postgresql && \
16-
chown postgres:postgres /run/postgresql && \
17-
mkdir /app && chown -R postgres:postgres /app
12+
RUN mkdir /app && chown -R postgres:postgres /app
1813

1914
WORKDIR /app/
2015

@@ -26,11 +21,6 @@ COPY --chown=postgres:postgres ./Backend/game_history/supervisord.conf /etc/supe
2621
COPY --chown=postgres:postgres ./Backend/game_history/requirements.txt .
2722
COPY --chown=postgres:postgres ./Backend/game_history/game_history /app/game_history
2823
COPY --chown=postgres:postgres ./Backend/game_history/tools.sh /app
29-
COPY --chown=postgres:postgres ./Backend/game_history/init_database.sh /app
30-
31-
32-
# Install Python packages
33-
RUN . venv/bin/activate && pip install -r requirements.txt
3424

3525
# Create log files and set permissions
3626
RUN touch /var/log/django.log /var/log/django.err /var/log/init_database.log /var/log/init_database.err && \

Backend/game_history/game_history/game_history/settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
DATABASES = {
116116
"default": {
117117
"ENGINE": "django.db.backends.postgresql",
118-
"NAME": "postgres",
118+
"NAME": "game_history",
119+
"HOST": "postgresql",
119120
"USER": "root",
120121
"PASSWORD": "root",
121122
"PORT": "5432",

Backend/game_history/init_database.sh

-46
This file was deleted.

Backend/game_history/requirements.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ typing-extensions==4.12.1; python_version <= '3.12'
1919
# gunicorn==20.1.0; python_version <= '3.12'
2020
pytest-django>=4.4.0
2121
django-environ
22-
23-
daphne
22+
uvicorn

Backend/game_history/supervisord.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
nodaemon=true
33

44
[program:django]
5-
command=/app/venv/bin/python /app/game_history/manage.py runserver 0.0.0.0:8000
5+
command=/app/tools.sh
66
user=postgres
77
autostart=true
88
autorestart=true

Backend/game_history/tools.sh

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#!/bin/bash
22

3-
# Initialize the database
4-
sh /app/init_database.sh
5-
63
# Activate the virtual environment
74
source venv/bin/activate
8-
pip install -r requirements.txt
5+
pip install --no-cache-dir -r requirements.txt
96
pip install tzdata
107

118
# Wait for PostgreSQL to be available
12-
while ! pg_isready -q -U "${DB_USER}" -d "postgres"; do
13-
echo >&2 "Postgres is unavailable - sleeping"
14-
sleep 5
9+
while ! psql -h postgresql -U "${DB_USER}" -d "game_history" -c '\q'; do
10+
echo >&2 "Postgres is unavailable - sleeping"
11+
sleep 5
1512
done
1613

1714
# Export Django settings and PYTHONPATH
@@ -34,4 +31,4 @@ python3 /app/game_history/manage.py migrate
3431

3532
# Start the Django application
3633
cd /app/game_history
37-
daphne -b 0.0.0.0 -p 8002 game_history.asgi:application
34+
exec uvicorn game_history.asgi:application --host 0.0.0.0 --port 8002

Backend/postgresql/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM alpine:3.20
2+
3+
RUN apk add --no-cache postgresql16 && mkdir /run/postgresql && \
4+
chown postgres:postgres /run/postgresql
5+
WORKDIR /app
6+
COPY ./Backend/postgresql/init_database.sh /app
7+
RUN chmod +x /app/init_database.sh
8+
USER postgres
9+
10+
CMD ["sh", "./init_database.sh"]

Backend/token_service/init_database.sh Backend/postgresql/init_database.sh

+20-4
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,32 @@ chown -R postgres:postgres /var/lib/postgresql/data
2323
exec postgres -D /var/lib/postgresql/data &
2424

2525
# Wait for PostgreSQL to start (you may need to adjust the sleep time)
26-
sleep 5
26+
sleep 2
2727

2828
# Create a new PostgreSQL user and set the password
2929
psql -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';"
30-
psql -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};"
30+
psql -c "ALTER USER ${DB_USER} CREATEDB;"
31+
32+
psql -c "CREATE DATABASE game_history;"
33+
psql -c "CREATE DATABASE game_server;"
34+
psql -c "CREATE DATABASE user_service;"
35+
psql -c "CREATE DATABASE token_service;"
36+
37+
# Grant the new user all privileges on the database
38+
psql -d user_service -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};"
39+
psql -d user_service -c "GRANT ALL PRIVILEGES ON DATABASE user_service TO ${DB_USER};"
40+
psql -d token_service -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};"
41+
psql -d token_service -c "GRANT ALL PRIVILEGES ON DATABASE token_service TO ${DB_USER};"
42+
psql -d game_history -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};"
43+
psql -d game_history -c "GRANT ALL PRIVILEGES ON DATABASE game_history TO ${DB_USER};"
44+
psql -d game_server -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};"
45+
psql -d game_server -c "GRANT ALL PRIVILEGES ON DATABASE game_server TO ${DB_USER};"
46+
3147
psql -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO ${DB_USER};"
3248

3349
# Stop the PostgreSQL server after setting the password
3450
pg_ctl stop -D /var/lib/postgresql/data
3551

36-
sleep 5
52+
sleep 2
3753
# Start the PostgreSQL server as the postgres user, keeping it in the foreground
38-
pg_ctl start -D /var/lib/postgresql/data
54+
exec postgres -D /var/lib/postgresql/data

Backend/token_service/Dockerfile

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
FROM alpine:3.20
1+
FROM python:3.11-alpine3.20
22

33
ENV PYTHONUNBUFFERED=1
44
ENV LANG=C.UTF-8
55

66
# Update and install dependencies
77
# trunk-ignore(hadolint/DL3018)
8-
RUN apk update && apk add --no-cache python3 py3-pip \
9-
postgresql16 postgresql16-client \
10-
bash supervisor curl openssl bash \
11-
build-base libffi-dev python3-dev nano
8+
RUN apk add --no-cache postgresql16-client \
9+
bash supervisor curl openssl \
10+
build-base libffi-dev python3-dev
1211
# Set work directory
13-
RUN mkdir /run/postgresql && \
14-
chown postgres:postgres /run/postgresql && \
15-
mkdir /app && chown -R postgres:postgres /app
12+
RUN mkdir /app && chown -R postgres:postgres /app
1613

1714
WORKDIR /app/
1815

@@ -24,7 +21,6 @@ COPY --chown=postgres:postgres ./Backend/token_service/supervisord.conf /etc/sup
2421
COPY --chown=postgres:postgres ./Backend/token_service/requirements.txt /app/
2522
COPY --chown=postgres:postgres ./Backend/token_service/token_service /app/token_service
2623
COPY --chown=postgres:postgres ./Backend/token_service/tools.sh /app
27-
COPY --chown=postgres:postgres ./Backend/token_service/init_database.sh /app
2824
COPY --chown=postgres:postgres ./Backend/token_service/run_consumer.sh /app
2925
COPY --chown=postgres:postgres ./Backend/token_service/run_consumer2.sh /app
3026
COPY --chown=postgres:postgres ./Backend/token_service/run_consumer3.sh /app

Backend/token_service/requirements.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ pytest==8.2.1; python_version >= '3.8'
1616
sqlparse==0.5.0; python_version >= '3.8'
1717
tomli==2.0.1; python_version <= '3.12'
1818
typing-extensions==4.12.1; python_version <= '3.12'
19-
gunicorn==20.1.0; python_version <= '3.12'
2019
django-environ
21-
daphne
2220
twisted[http2,tls]
2321
pytest
2422
pytest-django
25-
pytest-mock
23+
pytest-mock
24+
uvicorn

Backend/token_service/token_service/token_app/views.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
from django.http import Http404
34
from rest_framework import status
45
from token_service import settings
@@ -13,6 +14,9 @@
1314
from .models import UserTokens
1415
import jwt
1516

17+
logging.basicConfig(level=logging.INFO)
18+
logger = logging.getLogger(__name__)
19+
1620
class CustomTokenObtainPairView(TokenObtainPairView):
1721
"""
1822
CustomTokenObtainPairView class to handle token request.
@@ -49,7 +53,7 @@ def handle_token_request(ch, method, properties, body):
4953
access_token = str(refresh.access_token)
5054
user.token_data = {
5155
"refresh": str(refresh),
52-
"token": access_token
56+
"access": access_token
5357
}
5458
user.save()
5559
response_message = {
@@ -83,9 +87,9 @@ def handle_token_request(ch, method, properties, body):
8387
"access": access_token
8488
}
8589
except Exception as err:
86-
response_message = {"error": err}
90+
response_message = {"error": str(err)}
8791
except Exception as err:
88-
response_message = {"error": err}
92+
response_message = {"error": str(err)}
8993
publish_message("user_token_response_queue", json.dumps(response_message))
9094

9195

@@ -122,6 +126,7 @@ def post(self, request, *args, **kwargs) -> Response:
122126

123127

124128
class ValidateToken():
129+
@staticmethod
125130
def validate_token(access_token) -> bool:
126131
"""
127132
Validate the refresh token.
@@ -158,19 +163,29 @@ def validate_token_request_queue(self, ch, method, properties, body):
158163
"""
159164
data = json.loads(body)
160165
access_token = data.get("access")
166+
id = data.get("id")
167+
response = {}
161168
try:
162-
if ValidateToken.validate_token(access_token):
163-
user = get_object_or_404(UserTokens, token_data__access=access_token)
164-
response = {"access_token": "Valid token"}
165-
else:
166-
response = {"error": "Invalid token"}
169+
result = self.validate_token(access_token)
170+
if result:
171+
logger.info("result= %s", result)
172+
user = UserTokens.objects.filter(id = id, token_data__access = access_token).first()
173+
174+
logger.info("user.username= %s", user.username)
175+
logger.info("user.token_data['access']= %s", user.token_data["access"])
176+
if result:
177+
response = {"access_token": "Valid token"}
178+
else:
179+
response = {"error": "token mismatch"}
167180
except jwt.ExpiredSignatureError:
168181
response = {"error": "token is expired"}
169182
except jwt.InvalidTokenError:
170183
response = {"error": "Invalid token"}
184+
except Http404:
185+
response = {"error": "User has not logged in yet!!"}
171186
except Exception as err:
172-
response = {"error": "Invalid token"}
173-
187+
response = {"error": str(err)}
188+
logger.info("response = %s", response)
174189
publish_message("validate_token_response_queue", json.dumps(response))
175190

176191
def start_consumer(self) -> None:
@@ -197,7 +212,7 @@ def handle_logout_request_queue(ch, method, properties, body):
197212
except Http404:
198213
response_message = {"error": "User has not logged in yet"}
199214
except Exception as err:
200-
response_message = {"error": "Something unxpected happend"}
215+
response_message = {"error": str(err)}
201216
publish_message("logout_response_queue", json.dumps(response_message))
202217

203218
def start_consumer(self):

Backend/token_service/token_service/token_service/settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@
109109
DATABASES = {
110110
"default": {
111111
"ENGINE": "django.db.backends.postgresql",
112-
"NAME": "postgres",
112+
"NAME": "token_service",
113+
"HOST": "postgresql",
113114
"USER": "root",
114115
"PASSWORD": "root",
115116
"PORT": "5432",

Backend/token_service/tools.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#! /bin/bash
22

3-
sh /app/init_database.sh
43
# trunk-ignore(shellcheck/SC1091)
54
source venv/bin/activate
6-
pip install -r /app/requirements.txt
5+
pip install --no-cache-dir -r /app/requirements.txt
76
pip install tzdata
87

9-
while ! psql -U "${DB_USER}" -d "postgres" -c '\q'; do
8+
while ! psql -h postgresql -U "${DB_USER}" -d "token_service" -c '\q'; do
109
echo >&2 "Postgres is unavailable - sleeping"
1110
sleep 5
1211
done
@@ -16,4 +15,4 @@ export DJANGO_SETTINGS_MODULE=token_service.settings
1615
python3 /app/token_service/manage.py makemigrations
1716
python3 /app/token_service/manage.py migrate
1817
cd /app/token_service
19-
daphne -b 0.0.0.0 -p 8000 token_service.asgi:application
18+
exec uvicorn token_service.asgi:application --host 0.0.0.0 --port 8000

Backend/user_service/Dockerfile

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
FROM alpine:3.20
1+
FROM python:3.11-alpine3.20
22

33
ENV PYTHONUNBUFFERED=1
44
ENV LANG=C.UTF-8
55

66
# Update and install dependencies
77
# trunk-ignore(hadolint/DL3018)
8-
RUN apk update && apk add --no-cache python3 py3-pip \
9-
postgresql16 postgresql16-client \
10-
bash supervisor curl openssl bash \
11-
build-base libffi-dev python3-dev nano
12-
# Set work directory
13-
RUN mkdir /run/postgresql && \
14-
chown postgres:postgres /run/postgresql && \
15-
mkdir /app && chown -R postgres:postgres /app
8+
RUN addgroup -g 1000 postgres && adduser -D -u 1000 -G postgres postgres
9+
10+
RUN apk add --no-cache supervisor \
11+
curl openssl bash postgresql16-client \
12+
build-base libffi-dev python3-dev
13+
14+
# Set work directory
15+
RUN mkdir /app && chown -R postgres:postgres /app && mkdir -p /app/www/avatars && chown -R postgres:postgres /app/www/avatars
1616

1717
WORKDIR /app/
1818

@@ -24,9 +24,7 @@ COPY --chown=postgres:postgres ./Backend/user_service/supervisord.conf /etc/supe
2424
COPY --chown=postgres:postgres ./Backend/user_service/requirements.txt /app/
2525
COPY --chown=postgres:postgres ./Backend/user_service/user_service /app/user_service
2626
COPY --chown=postgres:postgres ./Backend/user_service/tools.sh /app
27-
COPY --chown=postgres:postgres ./Backend/user_service/init_database.sh /app
2827
COPY --chown=postgres:postgres ./Backend/user_service/run_consumer.sh /app
29-
RUN chmod +x /app/tools.sh /app/init_database.sh /app/run_consumer.sh
3028

3129
# Ensure supervisord and scripts are executable and owned by postgres
3230
RUN chown -R postgres:postgres /etc/supervisor && \

0 commit comments

Comments
 (0)