-
Notifications
You must be signed in to change notification settings - Fork 945
Develop #1019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Develop #1019
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "indent": 2, | ||
| "max_line_length": 100, | ||
| "format_css": true, | ||
| "format_js": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| FROM python:3.11-alpine | ||
| LABEL maintainer="student@mate.academy" | ||
|
|
||
| ENV PYTHONUNBUFFERED 1 | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Установка зависимостей системы | ||
| RUN apk add --update --no-cache \ | ||
| postgresql-client \ | ||
| jpeg-dev \ | ||
| libc-dev \ | ||
| linux-headers \ | ||
| postgresql-dev \ | ||
| zlib-dev | ||
|
|
||
| COPY requirements.txt requirements.txt | ||
| RUN pip install -r requirements.txt | ||
|
|
||
| COPY . . | ||
|
|
||
| # Создание папок для статики и медиа | ||
| RUN mkdir -p /vol/web/media | ||
| RUN mkdir -p /vol/web/static | ||
|
|
||
| # Безопасность: запуск от не-root пользователя | ||
| RUN adduser -D user | ||
| RUN chown -R user:user /vol/ | ||
| RUN chmod -R 755 /vol/web | ||
| USER user | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import time | ||
| from django.db import connections | ||
| from django.db.utils import OperationalError | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| def handle(self, *args, **options): | ||
| self.stdout.write("Waiting for database...") | ||
| db_conn = None | ||
| while not db_conn: | ||
| try: | ||
| db_conn = connections["default"] | ||
|
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After getting the connection with |
||
| self.stdout.write(self.style.SUCCESS("Database available!")) | ||
| except OperationalError: | ||
| self.stdout.write("Database unavailable, waiting 1 second...") | ||
| time.sleep(1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
| """ | ||
| from datetime import timedelta | ||
| from pathlib import Path | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| import os | ||
|
|
||
|
|
||
| # Build paths inside the project like this: BASE_DIR / 'subdir'. | ||
| BASE_DIR = Path(__file__).resolve().parent.parent | ||
|
|
@@ -84,10 +86,15 @@ | |
| # Database | ||
| # https://docs.djangoproject.com/en/4.0/ref/settings/#databases | ||
|
|
||
|
|
||
| DATABASES = { | ||
| "default": { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The database ENGINE is still There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ENGINE is still set to 'django.db.backends.sqlite3'. It must be changed to 'django.db.backends.postgresql' to connect to the PostgreSQL container. |
||
| "ENGINE": "django.db.backends.sqlite3", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NAME is set to SQLite file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NAME should use an environment variable (e.g., |
||
| "NAME": BASE_DIR / "db.sqlite3", | ||
| "USER": os.getenv("DB_USER", "postgres"), | ||
| "PASSWORD": os.getenv("DB_PASS", "postgres"), | ||
| "HOST": os.getenv("DB_HOST", "localhost"), | ||
| "PORT": "5432", | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -132,7 +139,7 @@ | |
| # https://docs.djangoproject.com/en/4.0/howto/static-files/ | ||
|
|
||
| STATIC_URL = "static/" | ||
|
|
||
| STATIC_ROOT = "/vol/web/static" | ||
| MEDIA_URL = "/media/" | ||
| MEDIA_ROOT = BASE_DIR / "media" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # @format | ||
|
|
||
| version: '3.8' | ||
|
|
||
| services: | ||
| db: | ||
| image: postgres:15-alpine | ||
| volumes: | ||
| - postgres_data:/var/lib/postgresql/data/ | ||
| environment: | ||
| - POSTGRES_DB=cinema | ||
| - POSTGRES_USER=postgres | ||
| - POSTGRES_PASSWORD=postgres | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After getting |
||
|
|
||
| app: | ||
| build: | ||
| context: . | ||
| ports: | ||
| - '8000:8000' | ||
| volumes: | ||
| - .:/app | ||
| - static_data:/vol/web/static | ||
| - media_data:/vol/web/media | ||
| environment: | ||
| - DB_HOST=db | ||
| - DB_NAME=cinema | ||
| - DB_USER=postgres | ||
| - DB_PASS=postgres | ||
| env_file: | ||
| - .env | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| command: > | ||
| sh -c "python manage.py wait_for_db && | ||
| python manage.py migrate && | ||
| python manage.py runserver 0.0.0.0:8000" | ||
| depends_on: | ||
| - db | ||
|
|
||
| volumes: | ||
| postgres_data: | ||
| static_data: | ||
| media_data: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [pytest] | ||
| DJANGO_SETTINGS_MODULE = cinema_service.settings | ||
| python_files = tests.py test_*.py *_tests.py | ||
| filterwarnings = | ||
| ignore::django.utils.deprecation.RemovedInDjango70Warning |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
wait_for_dbcommand getsconnections["default"]but never callsensure_connection()to verify the connection actually works. Adddb_conn.ensure_connection()after line 12.