add media files to production compose

This commit is contained in:
godd0t 2023-05-10 13:19:19 +02:00
parent 1ae2664984
commit 70a802350a
25 changed files with 129 additions and 123 deletions

View File

@ -4,10 +4,10 @@
if [ "$APP_ENV" != "prod" ]; then if [ "$APP_ENV" != "prod" ]; then
python manage.py makemigrations --noinput python manage.py makemigrations --noinput
python manage.py migrate --noinput python manage.py migrate --noinput
python manage.py runserver "$APP_HOST":"$APP_PORT"
else else
python manage.py makemigrations --noinput python manage.py makemigrations --noinput
python manage.py migrate --noinput python manage.py migrate --noinput
python manage.py collectstatic --noinput python manage.py collectstatic --noinput
gunicorn "$APP_NAME".wsgi:application --bind "$APP_HOST":"$APP_PORT" --workers 3 --log-level=debug
fi fi
gunicorn "$APP_NAME".wsgi:application --bind "$APP_HOST":"$APP_PORT" --workers 3 --log-level=debug

View File

@ -14,6 +14,7 @@ services:
- ./src:/usr/src/app/ - ./src:/usr/src/app/
- ./deployment/scripts:/app/deployment/scripts/ - ./deployment/scripts:/app/deployment/scripts/
- static_files:/usr/src/app/static - static_files:/usr/src/app/static
- media_files:/usr/src/app/media
labels: labels:
- "traefik.enable=true" - "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-backend.rule=Host(`${APP_DOMAIN}`)" - "traefik.http.routers.${APP_NAME}-backend.rule=Host(`${APP_DOMAIN}`)"
@ -21,6 +22,8 @@ services:
- "traefik.http.services.${APP_NAME}-backend.loadbalancer.server.port=${APP_PORT}" - "traefik.http.services.${APP_NAME}-backend.loadbalancer.server.port=${APP_PORT}"
- "traefik.http.routers.${APP_NAME}-backend.tls.certresolver=letsencrypt" - "traefik.http.routers.${APP_NAME}-backend.tls.certresolver=letsencrypt"
env_file: .env env_file: .env
expose:
- "${APP_PORT:-8000}"
depends_on: depends_on:
- db - db
- redis - redis
@ -33,8 +36,8 @@ services:
volumes: volumes:
- postgres_data_dir:/var/lib/postgresql/data/ - postgres_data_dir:/var/lib/postgresql/data/
env_file: .env env_file: .env
ports: expose:
- "5432:5432" - "${POSTGRES_PORT:-5432}"
shm_size: 1g shm_size: 1g
@ -70,9 +73,10 @@ services:
volumes: volumes:
- ./deployment/scripts/nginx/nginx.conf:/etc/nginx/nginx.conf:ro - ./deployment/scripts/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- static_files:/usr/src/app/static - static_files:/usr/src/app/static
- media_files:/usr/src/app/media
labels: labels:
- "traefik.enable=true" - "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-nginx.rule=Host(`${APP_HOST}`) && PathPrefix(`/static`)" - "traefik.http.routers.${APP_NAME}-nginx.rule=Host(`${APP_HOST}`) && (PathPrefix(`/static`) || PathPrefix(`/media`))"
- "traefik.http.routers.${APP_NAME}-nginx.entrypoints=web" - "traefik.http.routers.${APP_NAME}-nginx.entrypoints=web"
- "traefik.http.services.${APP_NAME}-nginx.loadbalancer.server.port=80" - "traefik.http.services.${APP_NAME}-nginx.loadbalancer.server.port=80"
depends_on: depends_on:
@ -101,6 +105,7 @@ services:
volumes: volumes:
static_files: static_files:
media_files:
postgres_data_dir: postgres_data_dir:
redis_data: redis_data:
letsencrypt: letsencrypt:

View File

@ -13,13 +13,9 @@ services:
volumes: volumes:
- ./src:/usr/src/app/ - ./src:/usr/src/app/
- ./deployment/scripts:/app/deployment/scripts/ - ./deployment/scripts:/app/deployment/scripts/
- static_files:/usr/src/app/static
labels:
- "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-backend.rule=Host(`${APP_HOST}`)"
- "traefik.http.routers.${APP_NAME}-backend.entrypoints=web"
- "traefik.http.services.${APP_NAME}-backend.loadbalancer.server.port=${APP_PORT}"
env_file: .env env_file: .env
ports:
- "${APP_PORT}:${APP_PORT}"
depends_on: depends_on:
- db - db
- redis - redis
@ -32,8 +28,8 @@ services:
volumes: volumes:
- postgres_data_dir:/var/lib/postgresql/data/ - postgres_data_dir:/var/lib/postgresql/data/
env_file: .env env_file: .env
ports: expose:
- "5432:5432" - "${POSTGRES_PORT:-5432}"
shm_size: 1g shm_size: 1g
@ -63,41 +59,6 @@ services:
container_name: "${APP_NAME}-celery-beat" container_name: "${APP_NAME}-celery-beat"
command: [ "/bin/sh", "/app/deployment/scripts/celery/start-beat.sh" ] command: [ "/bin/sh", "/app/deployment/scripts/celery/start-beat.sh" ]
nginx:
image: nginx:latest
container_name: "${APP_NAME}-nginx"
volumes:
- ./deployment/scripts/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- static_files:/usr/src/app/static
labels:
- "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-nginx.rule=Host(`${APP_HOST}`) && PathPrefix(`/static`)"
- "traefik.http.routers.${APP_NAME}-nginx.entrypoints=web"
- "traefik.http.services.${APP_NAME}-nginx.loadbalancer.server.port=80"
depends_on:
- backend
traefik:
image: traefik:v2.5
container_name: "${APP_NAME}-traefik"
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
networks:
- default
depends_on:
- db
- backend
- redis
volumes: volumes:
postgres_data_dir: postgres_data_dir:
redis_data: redis_data:
static_files:

View File

@ -13,7 +13,11 @@ testpaths = [
] ]
pythonpath = [".", "src"] pythonpath = [".", "src"]
python_files = "tests.py test_*.py *_tests.py" python_files = "tests.py test_*.py *_tests.py"
DJANGO_SETTINGS_MODULE = "conf.settings.test" DJANGO_SETTINGS_MODULE = "project_name.settings.test"
filterwarnings = [
'ignore::DeprecationWarning:kombu.*:',
'ignore::DeprecationWarning:celery.*:',
]
[tool.coverage.report] [tool.coverage.report]
fail_under = 85 fail_under = 85
@ -58,6 +62,7 @@ extend-exclude = '''
[tool.ruff] [tool.ruff]
format = "grouped"
line-length = 88 # black default line-length = 88 # black default
extend-exclude = [ extend-exclude = [
"*/migrations/*", "*/migrations/*",

View File

@ -2,5 +2,5 @@
APP_PATH="src" APP_PATH="src"
black $APP_PATH
ruff $APP_PATH --fix ruff $APP_PATH --fix
black $APP_PATH

View File

@ -2,5 +2,5 @@
APP_PATH="src" APP_PATH="src"
black $APP_PATH --check
ruff $APP_PATH ruff $APP_PATH
black $APP_PATH --check

View File

@ -6,7 +6,7 @@ import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:
@ -18,5 +18,5 @@ def main():
execute_from_command_line(sys.argv) execute_from_command_line(sys.argv)
if __name__ == '__main__': if __name__ == "__main__":
main() main()

View File

@ -2,4 +2,4 @@
# Django starts so that shared_task will use this app. # Django starts so that shared_task will use this app.
from .celery import app as celery_app from .celery import app as celery_app
__all__ = ('celery_app',) __all__ = ("celery_app",)

View File

@ -11,6 +11,6 @@ import os
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
application = get_asgi_application() application = get_asgi_application()

View File

@ -1,17 +1,18 @@
import os import os
from celery import Celery from celery import Celery
from celery.schedules import crontab
# Set the default Django settings module for the 'celery' program. # Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.dev') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.dev")
app = Celery('project_name') app = Celery("project_name")
# Using a string here means the worker doesn't have to serialize # Using a string here means the worker doesn't have to serialize
# the configuration object to child processes. # the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys # - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix. # should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY') app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django apps. # Load task modules from all registered Django apps.
app.autodiscover_tasks() app.autodiscover_tasks()
@ -19,4 +20,15 @@ app.autodiscover_tasks()
@app.task(bind=True) @app.task(bind=True)
def debug_task(self): def debug_task(self):
print(f'Request: {self.request!r}') print(f"Request: {self.request!r}")
app.conf.beat_schedule = {
"delete_job_files": {
"task": "test_periodic_task",
# Every 1 minute for testing purposes
"schedule": crontab(minute="*/1"),
},
}
app.conf.timezone = "UTC"

View File

@ -6,7 +6,9 @@ from django.core.management.utils import get_random_secret_key
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent BASE_DIR = Path(__file__).resolve().parent.parent.parent
SECRET_KEY = os_getenv("SECRET_KEY", get_random_secret_key()) # If SECRET_KEY is not set, generate a random one SECRET_KEY = os_getenv(
"SECRET_KEY", get_random_secret_key()
) # If SECRET_KEY is not set, generate a random one
APP_ENV = os_getenv("APP_ENV", "dev") APP_ENV = os_getenv("APP_ENV", "dev")
DEBUG = os_getenv("DEBUG", "true").lower() in ["True", "true", "1", "yes", "y"] DEBUG = os_getenv("DEBUG", "true").lower() in ["True", "true", "1", "yes", "y"]
@ -16,61 +18,65 @@ ALLOWED_HOSTS = os_getenv("ALLOWED_HOSTS", "localhost").split(",")
if DEBUG: if DEBUG:
CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_ALLOW_ALL = True
else: else:
CSRF_TRUSTED_ORIGINS = os_getenv("CSRF_TRUSTED_ORIGINS", "http://localhost").split(",") CSRF_TRUSTED_ORIGINS = os_getenv("CSRF_TRUSTED_ORIGINS", "http://localhost").split(
CORS_ALLOWED_ORIGINS = os_getenv("CORS_ALLOWED_ORIGINS", "http://localhost").split(",") ","
)
CORS_ALLOWED_ORIGINS = os_getenv("CORS_ALLOWED_ORIGINS", "http://localhost").split(
","
)
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'django.contrib.admin', "django.contrib.admin",
'django.contrib.auth', "django.contrib.auth",
'django.contrib.contenttypes', "django.contrib.contenttypes",
'django.contrib.sessions', "django.contrib.sessions",
'django.contrib.messages', "django.contrib.messages",
'django.contrib.staticfiles', "django.contrib.staticfiles",
"corsheaders", "corsheaders",
"test_app", "test_app",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', "django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware", "whitenoise.middleware.WhiteNoiseMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware', "django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware", # CorsMiddleware should be placed as high as possible, "corsheaders.middleware.CorsMiddleware", # CorsMiddleware should be placed as high as possible,
'django.middleware.common.CommonMiddleware', "django.middleware.common.CommonMiddleware",
'django.middleware.csrf.CsrfViewMiddleware', "django.middleware.csrf.CsrfViewMiddleware",
'django.contrib.auth.middleware.AuthenticationMiddleware', "django.contrib.auth.middleware.AuthenticationMiddleware",
'django.contrib.messages.middleware.MessageMiddleware', "django.contrib.messages.middleware.MessageMiddleware",
'django.middleware.clickjacking.XFrameOptionsMiddleware', "django.middleware.clickjacking.XFrameOptionsMiddleware",
] ]
ROOT_URLCONF = 'project_name.urls' ROOT_URLCONF = "project_name.urls"
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', "BACKEND": "django.template.backends.django.DjangoTemplates",
'DIRS': [], "DIRS": [],
'APP_DIRS': True, "APP_DIRS": True,
'OPTIONS': { "OPTIONS": {
'context_processors': [ "context_processors": [
'django.template.context_processors.debug', "django.template.context_processors.debug",
'django.template.context_processors.request', "django.template.context_processors.request",
'django.contrib.auth.context_processors.auth', "django.contrib.auth.context_processors.auth",
'django.contrib.messages.context_processors.messages', "django.contrib.messages.context_processors.messages",
], ],
}, },
}, },
] ]
WSGI_APPLICATION = 'project_name.wsgi.application' WSGI_APPLICATION = "project_name.wsgi.application"
# Database # Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = { DATABASES = {
'default': { "default": {
"ENGINE": "django.db.backends.postgresql", "ENGINE": "django.db.backends.postgresql",
'NAME': os_getenv("POSTGRES_DB", "postgres"), "NAME": os_getenv("POSTGRES_DB", "postgres"),
"USER": os_getenv("POSTGRES_USER", "postgres"), "USER": os_getenv("POSTGRES_USER", "postgres"),
"PASSWORD": os_getenv("POSTGRES_PASSWORD", "postgres"), "PASSWORD": os_getenv("POSTGRES_PASSWORD", "postgres"),
"HOST": os_getenv("POSTGRES_HOST", "db"), "HOST": os_getenv("POSTGRES_HOST", "db"),
@ -83,25 +89,25 @@ DATABASES = {
AUTH_PASSWORD_VALIDATORS = [ AUTH_PASSWORD_VALIDATORS = [
{ {
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
}, },
] ]
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/ # https://docs.djangoproject.com/en/4.2/topics/i18n/
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = "en-us"
TIME_ZONE = 'UTC' TIME_ZONE = "UTC"
USE_I18N = True USE_I18N = True
@ -110,15 +116,15 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/ # https://docs.djangoproject.com/en/4.2/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = "static/"
STATIC_ROOT = os_path.join(BASE_DIR, 'static') STATIC_ROOT = os_path.join(BASE_DIR, "static")
MEDIA_URL = 'media/' MEDIA_URL = "media/"
MEDIA_ROOT = os_path.join(BASE_DIR, 'media') MEDIA_ROOT = os_path.join(BASE_DIR, "media")
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# Redis # Redis
REDIS_DB_KEYS = { REDIS_DB_KEYS = {

View File

@ -1 +1,9 @@
from .base import * # noqa from .base import * # noqa
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}

View File

@ -5,7 +5,7 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path("admin/", admin.site.urls),
path("test_app/", include("test_app.urls")), path("test_app/", include("test_app.urls")),
] ]

View File

@ -11,6 +11,6 @@ import os
from django.core.wsgi import get_wsgi_application from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.dev') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.dev")
application = get_wsgi_application() application = get_wsgi_application()

View File

@ -1,3 +1,8 @@
from django.contrib import admin # noqa from django.contrib import admin
# Register your models here. from test_app.models import TestModel
@admin.register(TestModel)
class TestModelAdmin(admin.ModelAdmin):
list_display = ("name", "description")

View File

@ -2,5 +2,5 @@ from django.apps import AppConfig
class TestAppConfig(AppConfig): class TestAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = "django.db.models.BigAutoField"
name = 'test_app' name = "test_app"

View File

@ -1,3 +1,10 @@
from django.db import models # noqa from django.db import models # noqa
# Create your models here.
class TestModel(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
file = models.FileField(upload_to="test_app/files/")
def __str__(self):
return self.name

6
src/test_app/tasks.py Normal file
View File

@ -0,0 +1,6 @@
from project_name.celery import app
@app.task(bind=True, name="test_periodic_task")
def test_periodic_task(self): # noqa: Adding self since we are using bind=True
print("Hello from periodic task")

View File

@ -1,3 +0,0 @@
from django.test import TestCase # noqa
# Create your tests here.

View File

@ -1,3 +1 @@
from django.shortcuts import render # noqa from django.shortcuts import render # noqa
# Create your views here.

View File

@ -13,7 +13,5 @@ def enable_db_access_for_all_tests(db):
@pytest.fixture @pytest.fixture
def test_user(): def test_user():
return User.objects.create_user( return User.objects.create_user(
username='test_user', username="test_user", email="test_user@test.com", password="test_password"
email="test_user@test.com",
password="test_password"
) )

View File

@ -1,5 +1,3 @@
def test_example(test_user): def test_example(test_user):
assert test_user.username == 'test_user' assert test_user.username == "test_user"
assert test_user.email is not None assert test_user.email is not None