Compare commits
2 Commits
django-fas
...
342cf151c0
| Author | SHA1 | Date | |
|---|---|---|---|
| 342cf151c0 | |||
| 2d88f3c0c5 |
@@ -1,87 +0,0 @@
|
|||||||
# Git
|
|
||||||
.git
|
|
||||||
.gitignore
|
|
||||||
.gitattributes
|
|
||||||
|
|
||||||
|
|
||||||
# CI
|
|
||||||
.codeclimate.yml
|
|
||||||
.travis.yml
|
|
||||||
.taskcluster.yml
|
|
||||||
|
|
||||||
# Docker
|
|
||||||
docker-compose.yml
|
|
||||||
Dockerfile
|
|
||||||
.docker
|
|
||||||
.dockerignore
|
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
|
||||||
**/__pycache__/
|
|
||||||
**/*.py[cod]
|
|
||||||
|
|
||||||
# C extensions
|
|
||||||
*.so
|
|
||||||
|
|
||||||
# Distribution / packaging
|
|
||||||
.Python
|
|
||||||
env/
|
|
||||||
build/
|
|
||||||
develop-eggs/
|
|
||||||
dist/
|
|
||||||
downloads/
|
|
||||||
eggs/
|
|
||||||
lib/
|
|
||||||
lib64/
|
|
||||||
parts/
|
|
||||||
sdist/
|
|
||||||
var/
|
|
||||||
*.egg-info/
|
|
||||||
.installed.cfg
|
|
||||||
*.egg
|
|
||||||
|
|
||||||
# PyInstaller
|
|
||||||
# Usually these files are written by a python script from a template
|
|
||||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
||||||
*.manifest
|
|
||||||
*.spec
|
|
||||||
|
|
||||||
# Installer logs
|
|
||||||
pip-log.txt
|
|
||||||
pip-delete-this-directory.txt
|
|
||||||
|
|
||||||
# Unit test / coverage reports
|
|
||||||
htmlcov/
|
|
||||||
.tox/
|
|
||||||
.coverage
|
|
||||||
.cache
|
|
||||||
nosetests.xml
|
|
||||||
coverage.xml
|
|
||||||
|
|
||||||
# Translations
|
|
||||||
*.mo
|
|
||||||
*.pot
|
|
||||||
|
|
||||||
# Django stuff:
|
|
||||||
*.log
|
|
||||||
|
|
||||||
# Sphinx documentation
|
|
||||||
docs/_build/
|
|
||||||
|
|
||||||
# PyBuilder
|
|
||||||
target/
|
|
||||||
|
|
||||||
# PyCharm
|
|
||||||
.idea
|
|
||||||
|
|
||||||
# Python mode for VIM
|
|
||||||
.ropeproject
|
|
||||||
**/.ropeproject
|
|
||||||
|
|
||||||
# Vim swap files
|
|
||||||
**/*.swp
|
|
||||||
|
|
||||||
# VS Code
|
|
||||||
.vscode/
|
|
||||||
|
|
||||||
.flake8
|
|
||||||
.pre-commit-config.yaml
|
|
||||||
@@ -12,3 +12,10 @@ DB_PORT=5432
|
|||||||
REDIS_HOST=redis
|
REDIS_HOST=redis
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
REDIS_PASSWORD=redis
|
REDIS_PASSWORD=redis
|
||||||
|
|
||||||
|
RABBIT_HOST=rabbitmq
|
||||||
|
RABBIT_VHOST=/
|
||||||
|
RABBIT_PORT=5672
|
||||||
|
RABBIT_PORT_API=15672
|
||||||
|
RABBIT_LOGIN=admin
|
||||||
|
RABBIT_PASSWORD=admin
|
||||||
|
|||||||
5
.flake8
5
.flake8
@@ -1,5 +0,0 @@
|
|||||||
[flake8]
|
|
||||||
ignore = E203, E266, E501, W503, F403, F401, E402
|
|
||||||
max-line-length = 119
|
|
||||||
max-complexity = 18
|
|
||||||
select = B,C,E,F,W,T4,B9
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
repos:
|
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
||||||
rev: v4.6.0
|
|
||||||
hooks:
|
|
||||||
- id: check-merge-conflict
|
|
||||||
- repo: https://github.com/PyCQA/autoflake
|
|
||||||
rev: v2.3.1
|
|
||||||
hooks:
|
|
||||||
- id: autoflake
|
|
||||||
args:
|
|
||||||
- "--in-place"
|
|
||||||
- "--remove-duplicate-keys"
|
|
||||||
- "--remove-unused-variables"
|
|
||||||
- "--remove-all-unused-imports"
|
|
||||||
- repo: https://github.com/pre-commit/mirrors-isort
|
|
||||||
rev: v5.10.1
|
|
||||||
hooks:
|
|
||||||
- id: isort
|
|
||||||
args: ["--profile", "black"]
|
|
||||||
- repo: https://github.com/ambv/black
|
|
||||||
rev: 24.4.2
|
|
||||||
hooks:
|
|
||||||
- id: black
|
|
||||||
args:
|
|
||||||
- "--line-length=119"
|
|
||||||
- repo: https://github.com/PyCQA/flake8
|
|
||||||
rev: 7.1.0
|
|
||||||
hooks:
|
|
||||||
- id: flake8
|
|
||||||
|
|
||||||
default_language_version:
|
|
||||||
python: python3.12
|
|
||||||
30
README.md
30
README.md
@@ -1,36 +1,22 @@
|
|||||||
# simpliest django(uvicorn)+postgresql+fastapi+redis+nginx docker-compose (ready for production and dev)
|
# simpliest django(uvicorn)+postgresql+fastapi+redis+nginx docker-compose (ready for production and dev)
|
||||||
## How to use
|
|
||||||
To run:
|
To run:
|
||||||
`docker-compose up -d`
|
`docker-compose up -d`
|
||||||
|
|
||||||
Site available on 8000 port.
|
Site available on 8000 port.
|
||||||
|
|
||||||
You can make any changes in code, they will appear automatically. If you want to execute something with manage.py use:
|
You can make any changes in code, they will appear automatically. If you want to execute something with manage.py use:
|
||||||
```sh
|
|
||||||
|
```
|
||||||
docker-compose exec app python3 manage.py migrate
|
docker-compose exec app python3 manage.py migrate
|
||||||
docker-compose exec app python3 manage.py makemigrations
|
docker-compose exec app python3 manage.py makemigrations
|
||||||
docker-compose exec app python3 manage.py update_admin admin adminpass # create superuser
|
docker-compose exec app python3 manage.py update_admin admin adminpass # create superuser
|
||||||
```
|
```
|
||||||
|
|
||||||
and so on.
|
and so on.
|
||||||
|
|
||||||
## Install formatting
|
Example task [task_example.py](src/application/tasks/task_example.py)
|
||||||
**Features**
|
|
||||||
- check for unsolved merge conflicts
|
|
||||||
- black formatting
|
|
||||||
- sort imports
|
|
||||||
- remove unused variables, imports, duplicates
|
|
||||||
- flake8 verification
|
|
||||||
|
|
||||||
It executes on **every** commit
|
Example register task [__init__.py](src/application/tasks/__init__.py)
|
||||||
```sh
|
|
||||||
pip install pre-commit flake8 black
|
Example send task [send_task.py](src/application/management/commands/send_task.py)
|
||||||
pre-commit install
|
|
||||||
```
|
|
||||||
Apply for all files in current directory:
|
|
||||||
```sh
|
|
||||||
pre-commit run --all-files
|
|
||||||
```
|
|
||||||
If there is PEP8 errors, commit will be forbidden. To force commit use flag --no-verify:
|
|
||||||
```sh
|
|
||||||
git commit --no-verify ...
|
|
||||||
```
|
|
||||||
|
|||||||
@@ -59,8 +59,56 @@ services:
|
|||||||
- redis-data:/data
|
- redis-data:/data
|
||||||
restart: on-failure
|
restart: on-failure
|
||||||
|
|
||||||
|
rabbitmq:
|
||||||
|
image: rabbitmq:3.13.2-management
|
||||||
|
environment:
|
||||||
|
RABBITMQ_DEFAULT_USER: ${RABBIT_LOGIN}
|
||||||
|
RABBITMQ_DEFAULT_PASS: ${RABBIT_PASSWORD}
|
||||||
|
RABBITMQ_DEFAULT_PORT: ${RABBIT_PORT}
|
||||||
|
RABBITMQ_DEFAULT_VHOST: ${RABBIT_VHOST}
|
||||||
|
restart: always
|
||||||
volumes:
|
volumes:
|
||||||
|
- rabbitmq_data:/var/lib/rabbitmq
|
||||||
|
ports:
|
||||||
|
- "5672:5672"
|
||||||
|
|
||||||
|
flower:
|
||||||
|
image: mher/flower
|
||||||
|
hostname: flower
|
||||||
|
command: [
|
||||||
|
"celery",
|
||||||
|
"--broker=amqp://admin:admin@rabbitmq:5672//",
|
||||||
|
"flower",
|
||||||
|
"--broker-api=http://admin:admin@rabbitmq:15672/api/",
|
||||||
|
"--url_prefix=/flower"
|
||||||
|
]
|
||||||
|
depends_on:
|
||||||
|
- redis
|
||||||
|
- app
|
||||||
|
- celery_worker
|
||||||
|
- rabbitmq
|
||||||
|
volumes:
|
||||||
|
- flower_data:/data
|
||||||
|
restart: on-failure
|
||||||
|
|
||||||
|
celery_worker:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
command: celery -A core.celery_app worker --loglevel=info
|
||||||
|
volumes:
|
||||||
|
- ./src/:/app/
|
||||||
|
depends_on:
|
||||||
|
- app
|
||||||
|
- redis
|
||||||
|
- rabbitmq
|
||||||
|
- db
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
flower_data:
|
||||||
postgresql-data:
|
postgresql-data:
|
||||||
|
rabbitmq_data:
|
||||||
static:
|
static:
|
||||||
redis-data:
|
redis-data:
|
||||||
external: false
|
external: false
|
||||||
@@ -27,6 +27,9 @@ http {
|
|||||||
server api:8000;
|
server api:8000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
upstream flower {
|
||||||
|
server flower:5555;
|
||||||
|
}
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 8000;
|
listen 8000;
|
||||||
@@ -48,6 +51,15 @@ http {
|
|||||||
proxy_pass http://app;
|
proxy_pass http://app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
location /flower/ {
|
||||||
|
proxy_redirect off;
|
||||||
|
proxy_set_header Host app;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Host $server_name;
|
||||||
|
proxy_pass http://flower;
|
||||||
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
proxy_redirect off;
|
proxy_redirect off;
|
||||||
proxy_set_header Host app;
|
proxy_set_header Host app;
|
||||||
|
|||||||
15
src/application/management/commands/send_task.py
Normal file
15
src/application/management/commands/send_task.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
|
from core import celery_app
|
||||||
|
from helpers.const.tasks import EXAMPLE_TASK_NAME
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
celery_app.send_task(
|
||||||
|
name=EXAMPLE_TASK_NAME,
|
||||||
|
kwargs={
|
||||||
|
"x": 2,
|
||||||
|
"y": 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
4
src/application/tasks/__init__.py
Normal file
4
src/application/tasks/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from core import celery_app
|
||||||
|
from .task_example import Example
|
||||||
|
|
||||||
|
celery_app.register_task(Example())
|
||||||
0
src/application/tasks/abstract/__init__.py
Normal file
0
src/application/tasks/abstract/__init__.py
Normal file
13
src/application/tasks/abstract/base_task.py
Normal file
13
src/application/tasks/abstract/base_task.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
from celery import Task
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessingQueue(ABC, Task):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def processed_task(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def run(self, *args, **kwargs):
|
||||||
|
return self.processed_task(*args, **kwargs)
|
||||||
12
src/application/tasks/task_example.py
Normal file
12
src/application/tasks/task_example.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from application.tasks.abstract.base_task import ProcessingQueue
|
||||||
|
from helpers.const.tasks import EXAMPLE_TASK_NAME
|
||||||
|
|
||||||
|
|
||||||
|
class Example(ProcessingQueue):
|
||||||
|
name = EXAMPLE_TASK_NAME
|
||||||
|
max_retries = 5
|
||||||
|
default_retry_delay = 5
|
||||||
|
autoretry_for = (Exception,)
|
||||||
|
|
||||||
|
def processed_task(self, x: int | float, y: int | float):
|
||||||
|
return x + y
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .celery_app import app as celery_app
|
||||||
|
|
||||||
|
__all__ = ('celery_app',)
|
||||||
|
|||||||
14
src/core/celery_app.py
Normal file
14
src/core/celery_app.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from celery import Celery
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
||||||
|
|
||||||
|
app = Celery(
|
||||||
|
'core',
|
||||||
|
broker=settings.CELERY_BROKER_URL,
|
||||||
|
)
|
||||||
|
|
||||||
|
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||||
|
app.autodiscover_tasks()
|
||||||
@@ -12,10 +12,6 @@ ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "*").split(" ")
|
|||||||
|
|
||||||
CSRF_TRUSTED_ORIGINS = os.getenv("CSRF_TRUSTED_ORIGINS", "http://* https://*").split(" ")
|
CSRF_TRUSTED_ORIGINS = os.getenv("CSRF_TRUSTED_ORIGINS", "http://* https://*").split(" ")
|
||||||
|
|
||||||
REDIS_HOST = os.getenv("REDIS_HOST")
|
|
||||||
REDIS_PORT = int(os.getenv("REDIS_PORT"))
|
|
||||||
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD")
|
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
@@ -23,7 +19,9 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'application.apps.ApplicationConfig'
|
'application.apps.ApplicationConfig',
|
||||||
|
'djangoql',
|
||||||
|
'django_celery_results'
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -98,3 +96,18 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static')
|
|||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
|
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
|
||||||
|
|
||||||
|
REDIS_HOST = os.getenv("REDIS_HOST")
|
||||||
|
REDIS_PORT = int(os.getenv("REDIS_PORT"))
|
||||||
|
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD")
|
||||||
|
|
||||||
|
RABBIT_HOST = os.getenv("RABBIT_HOST")
|
||||||
|
RABBIT_PORT = int(os.getenv("RABBIT_PORT"))
|
||||||
|
RABBIT_LOGIN = os.getenv("RABBIT_LOGIN")
|
||||||
|
RABBIT_PASSWORD = os.getenv("RABBIT_PASSWORD")
|
||||||
|
RABBIT_VHOST = os.getenv("RABBIT_VHOST")
|
||||||
|
|
||||||
|
CELERY_BROKER_URL = f'amqp://{RABBIT_LOGIN}:{RABBIT_PASSWORD}@{RABBIT_HOST}:{RABBIT_PORT}/{RABBIT_VHOST}'
|
||||||
|
CELERY_RESULT_BACKEND = 'django-db'
|
||||||
|
CELERY_CACHE_BACKEND = 'django-cache'
|
||||||
|
CELERY_RESULT_EXTENDED = True
|
||||||
|
|||||||
0
src/helpers/const/__init__.py
Normal file
0
src/helpers/const/__init__.py
Normal file
1
src/helpers/const/tasks.py
Normal file
1
src/helpers/const/tasks.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
EXAMPLE_TASK_NAME = "example"
|
||||||
@@ -4,3 +4,6 @@ Django==5.0.2
|
|||||||
psycopg2
|
psycopg2
|
||||||
redis==4.6.0
|
redis==4.6.0
|
||||||
djangoql==0.18.1
|
djangoql==0.18.1
|
||||||
|
celery==5.4.0
|
||||||
|
django-celery-beat
|
||||||
|
django-celery-results==2.5.1
|
||||||
Reference in New Issue
Block a user