diff --git a/.my_pgpass b/.my_pgpass new file mode 100644 index 0000000..7bea649 --- /dev/null +++ b/.my_pgpass @@ -0,0 +1 @@ +localhost:5432:bloonsworld:password123 \ No newline at end of file diff --git a/.pg_service.conf b/.pg_service.conf new file mode 100644 index 0000000..87d0a65 --- /dev/null +++ b/.pg_service.conf @@ -0,0 +1,5 @@ +[bloonsworld] +host=localhost +user=bloonsworld +dbname=bloonsworld +port=5432 \ No newline at end of file diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 0000000..cac0681 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.12.2-alpine3.21 + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 + +WORKDIR app +COPY requirements.txt . +RUN pip install -r requirements.txt && \ + pip install gunicorn + +COPY . . \ No newline at end of file diff --git a/app/settings/settings.py b/app/settings/settings.py index ee8ed97..ec4db37 100644 --- a/app/settings/settings.py +++ b/app/settings/settings.py @@ -10,8 +10,11 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.2/ref/settings/ """ import os +from sys import platform from pathlib import Path +from django.db.backends.postgresql.psycopg_any import IsolationLevel + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -19,12 +22,16 @@ BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-)e!wy4)=xinnd!d(iuw6*-tf^-)ptiwnttwf+9ql%*jy63wtd8' - # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True +if platform != "win32": + with open("/secret", "r") as f: + SECRET_KEY = f.read() +else: + # SECURITY WARNING: keep the secret key used in production secret! + SECRET_KEY = 'django-insecure-)e!wy4)=xinnd!d(iuw6*-tf^-)ptiwnttwf+9ql%*jy63wtd8' + ALLOWED_HOSTS = ["*"] # Application definition @@ -87,12 +94,22 @@ WSGI_APPLICATION = 'settings.wsgi.application' # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases -DATABASES = { - 'default': { +DATABASES = {} + +if DEBUG: + DATABASES["default"] = { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } -} +else: + DATABASES["default"] = { + "ENGINE": "django.db.backends.postgresql", + "OPTIONS": { + "service": "bloonsworld_db", + "passfile": ".my_pgpass", + # "isolation_level": IsolationLevel.SERIALIZABLE, + } + } # Password validation @@ -142,4 +159,6 @@ MEDIA_URL = "/media/" DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4" -CRISPY_TEMPLATE_PACK = "bootstrap4" \ No newline at end of file +CRISPY_TEMPLATE_PACK = "bootstrap4" + +CRISPY_FAIL_SILENTLY = not DEBUG \ No newline at end of file diff --git a/app/settings/wsgi.py b/app/settings/wsgi.py index 118ef7d..91cbf4f 100644 --- a/app/settings/wsgi.py +++ b/app/settings/wsgi.py @@ -14,3 +14,4 @@ from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.settings') application = get_wsgi_application() + diff --git a/app/users/views.py b/app/users/views.py index 62b6d90..b4a6567 100644 --- a/app/users/views.py +++ b/app/users/views.py @@ -20,6 +20,7 @@ class LoginView(TemplateView): if not form.is_valid(): return render(request=request, template_name="users/login.html", context={"form": form}) user = form.get_user() + bloonsa_util.init_player(request=request) login(request=request, user=user) bloonsa_util.log(player=user.player, action=actions.login) @@ -48,6 +49,7 @@ class RegisterView(TemplateView): class LogoutView(TemplateView): def get(self, request, *args, **kwargs): if request.user.is_authenticated: + bloonsa_util.init_player(request=request) bloonsa_util.log(request=request, action=actions.logout) logout(request) diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..03b27cc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +services: + bloonsworld: + build: ./app + volumes: + - ./.media:/app/media:ro + - ./.static:/app/static:rw + - ./.django:/app/data:rw + command: + - sh + - -c + - | + python manage.py collectstatic --noinput + gunicorn --chdir /app --bind :80 --workers 3 Settings.wsgi:application + environment: + - DJANGO_SECRET_KEY + - DJANGO_ALLOWED_HOSTS + - DJANGO_DEBUG + networks: + - nginx_private + - bloonsworld + restart: unless-stopped + + bloonsworld-db: + + networks: + - bloonsworld + restart: unless-stopped + +networks: + nginx_private: + external: true + bloonsworld: \ No newline at end of file