Skip to content

Commit f7be290

Browse files
committed
whitenoise and other changes to prep for deploy
0 parents  commit f7be290

27 files changed

+764
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm

.devcontainer/devcontainer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}

.github/workflows/deploy.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]
22+
with:
23+
config-env-vars: POSTGRES_PASSWORD
24+
env:
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Django Celery
2+
3+
[![1-click-deploy](https://defang.io/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-django-celery-template%26template_owner%3DDefangSamples)
4+
5+
This is a sample Django application that uses Celery for background tasks. It uses Postgres as the database and Redis as the message broker.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose -f compose.dev.yaml up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
24+
25+
> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
26+
27+
### `POSTGRES_PASSWORD`
28+
The password for the Postgres database.
29+
```bash
30+
defang config set POSTGRES_PASSWORD
31+
```
32+
33+
## Deployment
34+
35+
> [!NOTE]
36+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
37+
38+
### Defang Playground
39+
40+
Deploy your application to the Defang Playground by opening up your terminal and typing:
41+
```bash
42+
defang compose up
43+
```
44+
45+
### BYOC
46+
47+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).
48+
49+
---
50+
51+
Title: Django Celery
52+
53+
Short Description: A Django application that uses Celery for background tasks, Postgres as the database, and Redis as the message broker.
54+
55+
Tags: Django, Celery, Postgres, Redis
56+
57+
Languages: python, sql

app/.dockerignore

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
env/
8+
build/
9+
develop-eggs/
10+
dist/
11+
downloads/
12+
eggs/
13+
.eggs/
14+
lib/
15+
lib64/
16+
parts/
17+
sdist/
18+
var/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Django
24+
*.log
25+
local_settings.py
26+
db.sqlite3
27+
db.sqlite3-journal
28+
media/
29+
static/
30+
staticfiles/
31+
32+
# Virtual Environment
33+
venv/
34+
ENV/
35+
.env
36+
.venv
37+
38+
# Version Control
39+
.git
40+
.gitignore
41+
.gitattributes
42+
43+
# IDE/Editor files
44+
.idea/
45+
.vscode/
46+
*.swp
47+
*.swo
48+
*~
49+
.DS_Store
50+
51+
# Testing
52+
.coverage
53+
htmlcov/
54+
.tox/
55+
.pytest_cache/
56+
57+
# Documentation
58+
docs/
59+
README.md
60+
LICENSE
61+
62+
# Docker
63+
Dockerfile*
64+
docker-compose*.yml
65+
.docker/
66+
67+
# Celery
68+
celerybeat-schedule
69+
celerybeat.pid
70+
71+
# Temporary files
72+
*.tmp
73+
.cache/

app/.gitignore

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Django #
2+
*.log
3+
*.pot
4+
*.pyc
5+
__pycache__/
6+
local_settings.py
7+
db.sqlite3
8+
db.sqlite3-journal
9+
media
10+
11+
# If you are using PostgreSQL
12+
*.psql
13+
*.pgsql
14+
*.sql
15+
16+
# Python #
17+
*.py[cod]
18+
*$py.class
19+
*.so
20+
.Python
21+
build/
22+
develop-eggs/
23+
dist/
24+
downloads/
25+
eggs/
26+
.eggs/
27+
lib/
28+
lib64/
29+
parts/
30+
sdist/
31+
var/
32+
wheels/
33+
*.egg-info/
34+
.installed.cfg
35+
*.egg
36+
MANIFEST
37+
38+
# Virtual Environment #
39+
venv/
40+
env/
41+
ENV/
42+
.env
43+
44+
# IDE #
45+
.idea/
46+
.vscode/
47+
*.swp
48+
*.swo
49+
.DS_Store
50+
51+
# Celery #
52+
celerybeat-schedule
53+
celerybeat.pid
54+
55+
# Coverage #
56+
htmlcov/
57+
.tox/
58+
.nox/
59+
.coverage
60+
.coverage.*
61+
.cache
62+
nosetests.xml
63+
coverage.xml
64+
*.cover
65+
.hypothesis/
66+
67+
# Unit test / coverage reports
68+
htmlcov/
69+
.tox/
70+
.coverage
71+
.coverage.*
72+
.cache
73+
coverage.xml
74+
*.cover
75+
.pytest_cache/
76+
77+
# Django stuff:
78+
staticfiles/
79+
staticfiles.json
80+
.static_storage/
81+
.media/
82+
83+
# Jupyter Notebook
84+
.ipynb_checkpoints

app/Dockerfile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM python:3.12-slim
2+
3+
# Set environment variables
4+
ENV PYTHONDONTWRITEBYTECODE=1 \
5+
PYTHONUNBUFFERED=1 \
6+
DJANGO_SETTINGS_MODULE=django_celery.settings
7+
8+
# Create a non-root user
9+
RUN adduser --disabled-password --gecos "" django
10+
11+
# Install system dependencies (merged from both stages)
12+
RUN apt-get update && apt-get install -y --no-install-recommends \
13+
libpq5 \
14+
curl \
15+
build-essential \
16+
libpq-dev \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
# Set working directory
20+
WORKDIR /app
21+
22+
# Install dependencies
23+
COPY requirements.txt .
24+
RUN pip install --no-cache-dir -r requirements.txt \
25+
gunicorn \
26+
whitenoise \
27+
dj-database-url \
28+
psycopg2-binary \
29+
redis
30+
31+
# Copy project files
32+
COPY --chown=django:django . .
33+
34+
# Collect static files
35+
RUN python manage.py collectstatic --noinput
36+
37+
# Switch to non-root user
38+
USER django
39+
40+
# Expose port 8000
41+
EXPOSE 8000
42+
43+
CMD [ "./command.sh" ]

app/command.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Apply database migrations
4+
python manage.py migrate
5+
6+
# Create superuser if not exists
7+
python manage.py createsuperauto
8+
9+
# Start the Django development server if DEBUG is True
10+
if [ "$DEBUG" = "True" ]; then
11+
python manage.py runserver 0.0.0.0:8000
12+
else
13+
gunicorn django_celery.wsgi:application --bind 0.0.0.0:8000 --workers 1 --threads 2 --timeout 120
14+
fi

app/django_celery/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
# This will make sure the app is always imported when
4+
# Django starts so that shared_task will use this app.
5+
from .celery import app as celery_app
6+
7+
__all__ = ('celery_app',)

app/django_celery/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for django_celery project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)