Skip to content

Commit

Permalink
Merge pull request #67 from abbastoof/feature/045-matchmaking
Browse files Browse the repository at this point in the history
Feature/045 matchmaking
  • Loading branch information
mtoof authored Aug 14, 2024
2 parents e0d237c + 0af25c9 commit 8d20f6f
Show file tree
Hide file tree
Showing 73 changed files with 2,480 additions and 1,104 deletions.
9 changes: 3 additions & 6 deletions Backend/game_history/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ WORKDIR /app/

# Install Python virtual environment
RUN python3 -m venv venv && chown -R postgres:postgres venv
RUN touch /var/log/django_debug.log && chown -R postgres:postgres /var/log/django_debug.log
RUN touch /var/log/django_error.log && chown -R postgres:postgres /var/log/django_error.log

# Copy application code and adjust permissions
COPY --chown=postgres:postgres ./Backend/game_history/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY --chown=postgres:postgres ./Backend/game_history/requirements.txt .
COPY --chown=postgres:postgres ./Backend/game_history/game_history /app/game_history
COPY --chown=postgres:postgres ./Backend/game_history/tools.sh /app

# Create log files and set permissions
RUN touch /var/log/django.log /var/log/django.err /var/log/init_database.log /var/log/init_database.err && \
chown -R postgres:postgres /var/log/django.log /var/log/django.err /var/log/init_database.log /var/log/init_database.err

# Ensure supervisord and scripts are executable and owned by postgres
RUN chown -R postgres:postgres /app && \
chown -R postgres:postgres /var/log && \
Expand All @@ -36,4 +33,4 @@ USER postgres

HEALTHCHECK --interval=30s --timeout=2s --start-period=5s --retries=3 CMD curl -sSf http://localhost:8002/game-history/ > /dev/null && echo "success" || echo "failure"

ENTRYPOINT ["sh", "./tools.sh"]
ENTRYPOINT ["sh", "/app/tools.sh"]
2 changes: 1 addition & 1 deletion Backend/game_history/game_history/game_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class GameHistory(models.Model):
player1_id = models.IntegerField()
player2_username = models.CharField(max_length=50)
player2_id = models.IntegerField()
winner_id = models.IntegerField()
winner_id = models.IntegerField(null=True, blank=True)
start_time = models.DateTimeField()
end_time = models.DateTimeField(null=True, blank=True)
def __str__(self):
Expand Down
5 changes: 4 additions & 1 deletion Backend/game_history/game_history/game_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from .serializers import GameHistorySerializer, GameStatSerializer
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
import logging

logger = logging.getLogger(__name__)

class GameHistoryViewSet(viewsets.ModelViewSet):
"""
Expand Down Expand Up @@ -110,4 +113,4 @@ def destroy(self, request, pk=None, *args, **kwargs):
"""
instance = get_object_or_404(self.get_queryset(), pk=pk)
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
53 changes: 48 additions & 5 deletions Backend/game_history/game_history/game_history/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,61 @@
from datetime import timedelta
from pathlib import Path

LOG_DIR = Path('/var/log/')

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'django_debug.log'),
'formatter': 'verbose',
},
'error_file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'django_error.log'),
'formatter': 'verbose',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'django': {
'handlers': ['file', 'console'],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': ['error_file'],
'level': 'ERROR',
'propagate': False,
},
},
}

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

RABBITMQ_HOST = os.getenv("RABBITMQ_HOST")
RABBITMQ_USER = os.getenv("RABBITMQ_USER")
RABBITMQ_PASS = os.getenv("RABBITMQ_PASS")
RABBITMQ_PORT = os.getenv("RABBITMQ_PORT")

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-woftd2en2**zr(b%#*2vit2v%s@(k54gb^c(ots0abo7(wsmo%"

Expand Down
2 changes: 1 addition & 1 deletion Backend/game_history/game_history/game_history/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from django.urls import include, path

urlpatterns = [
path("admin/", admin.site.urls),
# path("admin/", admin.site.urls),
path("", include("game_data.urls")),
]
83 changes: 60 additions & 23 deletions Backend/game_history/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
-i https://pypi.org/simple
asgiref
django
django-cors-headers
django-mysql
djangorestframework
djangorestframework-simplejwt; python_version >= '3.6'
djangorestframework-simplejwt[crypto]; python_version >= '3.6'
exceptiongroup
iniconfig
packaging
pika
pluggy
psycopg2-binary
pytest
sqlparse
tomli
typing-extensions
# gunicorn
pytest-django>=4.4.0
django-environ
uvicorn
daphne
anyio==4.4.0
asgiref==3.8.1
attrs==24.2.0
autobahn==24.4.2
Automat==22.10.0
certifi==2024.7.4
cffi==1.17.0
charset-normalizer==3.3.2
click==8.1.7
constantly==23.10.4
cryptography==43.0.0
daphne==4.1.2
Django==5.1
django-cors-headers==4.4.0
django-environ==0.11.2
djangorestframework==3.15.2
djangorestframework-simplejwt==5.3.1
exceptiongroup==1.2.2
h11==0.14.0
h2==4.1.0
hpack==4.0.0
httptools==0.6.1
hyperframe==6.0.1
hyperlink==21.0.0
idna==3.7
incremental==24.7.2
iniconfig==2.0.0
msgpack==1.0.8
multidict==6.0.5
packaging==24.1
pamqp==3.3.0
pluggy==1.5.0
priority==1.3.0
psycopg2-binary==2.9.9
pyasn1==0.6.0
pyasn1_modules==0.4.0
pycparser==2.22
PyJWT==2.9.0
pyOpenSSL==24.2.1
pytest==8.3.2
pytest-django==4.8.0
pytest-mock==3.14.0
python-dotenv==1.0.1
PyYAML==6.0.2
requests==2.32.3
service-identity==24.1.0
six==1.16.0
sniffio==1.3.1
sqlparse==0.5.1
tomli==2.0.1
Twisted==24.7.0
txaio==23.1.1
typing_extensions==4.12.2
tzdata==2024.1
urllib3==2.2.2
uvicorn==0.30.5
uvloop==0.19.0
watchfiles==0.23.0
yarl==1.9.4
zope.interface==7.0.1
21 changes: 0 additions & 21 deletions Backend/game_history/supervisord.conf

This file was deleted.

5 changes: 1 addition & 4 deletions Backend/game_history/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Activate the virtual environment
source venv/bin/activate
pip install --upgrade pip
pip install --no-cache-dir -r requirements.txt
pip install tzdata

# Wait for PostgreSQL to be available
while ! psql -h postgresql -U "${DB_USER}" -d "game_history" -c '\q'; do
Expand All @@ -26,9 +26,6 @@ ls /app/game_history
python3 /app/game_history/manage.py makemigrations
python3 /app/game_history/manage.py migrate

# Run pytest with explicit PYTHONPATH
# PYTHONPATH=/app pytest -vv

# Start the Django application
cd /app/game_history
# exec uvicorn game_history.asgi:application --host 0.0.0.0 --port 8002
Expand Down
16 changes: 11 additions & 5 deletions Backend/postgresql/init_database.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#! /bin/bash

# Stop any running PostgreSQL processes
pg_ctl stop -D /var/lib/postgresql/data || true

# Remove any existing data directory
rm -rf /var/lib/postgresql/data

# Create necessary directories with appropriate permissions
cd /
mkdir -p /var/lib/postgresql/data
chown postgres:postgres /var/lib/postgresql/data
mkdir -p /run/postgresql
chown postgres:postgres /run/postgresql/

# Switch to the postgres user and run the following commands
mkdir -p /var/lib/postgresql/data
# Initialize the PostgreSQL data directory
initdb -D /var/lib/postgresql/data

# Append configurations to pg_hba.conf and postgresql.conf as the postgres user
echo "host all all 0.0.0.0/0 md5" >>/var/lib/postgresql/data/pg_hba.conf
echo "listen_addresses='*'" >>/var/lib/postgresql/data/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf
echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf

# Remove the unix_socket_directories line from postgresql.conf as the postgres user
sed -i "/^unix_socket_directories = /d" /var/lib/postgresql/data/postgresql.conf
Expand Down
19 changes: 6 additions & 13 deletions Backend/token_service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ENV LANG=C.UTF-8
# Update and install dependencies
# trunk-ignore(hadolint/DL3018)
RUN apk add --no-cache postgresql16-client \
bash supervisor curl openssl \
bash curl openssl \
build-base libffi-dev python3-dev
# Set work directory
RUN mkdir /app && chown -R postgres:postgres /app
Expand All @@ -15,28 +15,21 @@ WORKDIR /app/

# Install Python virtual environment
RUN python3 -m venv venv && chown -R postgres:postgres venv

RUN touch /var/log/django_debug.log && chown -R postgres:postgres /var/log/django_debug.log
RUN touch /var/log/django_error.log && chown -R postgres:postgres /var/log/django_error.log
# Copy application code and adjust permissions
COPY --chown=postgres:postgres ./Backend/token_service/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY --chown=postgres:postgres ./Backend/token_service/requirements.txt /app/
COPY --chown=postgres:postgres ./Backend/token_service/token_service /app/token_service
COPY --chown=postgres:postgres ./Backend/token_service/tools.sh /app
COPY --chown=postgres:postgres ./Backend/token_service/run_consumer.sh /app
COPY --chown=postgres:postgres ./Backend/token_service/run_consumer2.sh /app
COPY --chown=postgres:postgres ./Backend/token_service/run_consumer3.sh /app

# Ensure supervisord and scripts are executable and owned by postgres
RUN chown -R postgres:postgres /etc/supervisor && \
chown -R postgres:postgres /usr/bin/supervisord && \
chown -R postgres:postgres /etc/supervisor/conf.d/supervisord.conf && \
chown -R postgres:postgres /app && \
RUN chown -R postgres:postgres /app && \
chown -R postgres:postgres /var/log && \
chown -R postgres:postgres /app/venv && \
chown -R postgres:postgres /app/token_service && \
chmod +x /usr/bin/supervisord
chown -R postgres:postgres /app/token_service

USER postgres

HEALTHCHECK --interval=30s --timeout=2s --start-period=5s --retries=3 CMD curl --fail --silent --write-out http://127.0.0.1:8000/ > /dev/null && echo "success" || echo "failure"

ENTRYPOINT ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
ENTRYPOINT ["sh", "/app/tools.sh"]
Loading

0 comments on commit 8d20f6f

Please sign in to comment.