A1: Django skeleton with web and worker processes (ADR-54, ADR-55, WEB-01, WEB-07, ARC-70..74)
Django 5.2 project (config/, apps/accounts with a custom User and case-insensitive usernames, apps/lottery with the run_worker command, apps/web with a placeholder home and /health/live, apps/ops), SQLite in WAL with IMMEDIATE transactions, secrets and hosts from the environment. The compose stack is now web + worker + caddy and serves https://localhost; the old FastAPI service is out of the stack until roadmap D1. The three old infrastructure tests follow the new Dockerfile and compose. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
79f7ebe53e
commit
fc23a75c9b
43 files changed
+709
-61
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,145 @@
|
||||
"""Django settings (ADR-54, ADR-55, WEB-07).
|
||||
|
||||
Everything environment-specific comes from environment variables, so the same image
|
||||
serves `http://localhost` today and a real domain later with configuration only.
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def _env_list(name: str) -> list[str]:
|
||||
return [item.strip() for item in os.environ.get(name, "").split(",") if item.strip()]
|
||||
|
||||
|
||||
DEBUG = os.environ.get("DJANGO_DEBUG", "false").lower() == "true"
|
||||
|
||||
# Like ADR-11 for the old server: refuse to start with an inadequate secret instead of
|
||||
# running with a guessable one. DEBUG (local development without Docker) is exempt.
|
||||
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "")
|
||||
if not DEBUG and len(SECRET_KEY) < 50:
|
||||
raise ImproperlyConfigured(
|
||||
"DJANGO_SECRET_KEY must be at least 50 characters. Generate one with: "
|
||||
"python -c \"import secrets; print(secrets.token_urlsafe(64))\""
|
||||
)
|
||||
if DEBUG and not SECRET_KEY:
|
||||
SECRET_KEY = "insecure-development-key-" + "x" * 40
|
||||
|
||||
# WEB-07: `localhost` works out of the box; a domain is added by configuration only.
|
||||
SITE_ADDRESS = os.environ.get("SITE_ADDRESS", "localhost")
|
||||
ALLOWED_HOSTS = sorted({"localhost", "127.0.0.1", "[::1]", "web", SITE_ADDRESS, *_env_list("DJANGO_ALLOWED_HOSTS")})
|
||||
CSRF_TRUSTED_ORIGINS = sorted(
|
||||
{f"https://{SITE_ADDRESS}", *_env_list("DJANGO_CSRF_TRUSTED_ORIGINS")}
|
||||
)
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
"apps.accounts",
|
||||
"apps.lottery",
|
||||
"apps.web",
|
||||
"apps.ops",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "config.urls"
|
||||
WSGI_APPLICATION = "config.wsgi.application"
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"DIRS": [],
|
||||
"APP_DIRS": True,
|
||||
"OPTIONS": {
|
||||
"context_processors": [
|
||||
"django.template.context_processors.request",
|
||||
"django.contrib.auth.context_processors.auth",
|
||||
"django.contrib.messages.context_processors.messages",
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
# ADR-55: one SQLite file in WAL mode shared by the web and the worker processes. Every write
|
||||
# transaction takes the write lock up front (`BEGIN IMMEDIATE`), so two processes never deadlock
|
||||
# upgrading a read lock, and durability matches the old server (`synchronous=FULL`).
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": os.environ.get("DJANGO_DB_PATH", str(BASE_DIR / "data" / "db" / "site.sqlite3")),
|
||||
"OPTIONS": {
|
||||
"transaction_mode": "IMMEDIATE",
|
||||
"timeout": 20,
|
||||
"init_command": "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON;",
|
||||
},
|
||||
}
|
||||
}
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
|
||||
# The model is defined before the first migration (ADR-54): changing it later is painful.
|
||||
AUTH_USER_MODEL = "accounts.User"
|
||||
|
||||
# Argon2 first, as in ADR-36; the others only keep Django's own tooling working.
|
||||
PASSWORD_HASHERS = [
|
||||
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
||||
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
||||
]
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", "OPTIONS": {"min_length": 8}},
|
||||
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
|
||||
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
|
||||
]
|
||||
|
||||
# ADR-60: Italian only for the pilot; gettext is on so other languages can be added later.
|
||||
LANGUAGE_CODE = "it"
|
||||
TIME_ZONE = "UTC"
|
||||
USE_I18N = True
|
||||
USE_TZ = True
|
||||
|
||||
STATIC_URL = "/static/"
|
||||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||||
STORAGES = {
|
||||
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
|
||||
"staticfiles": {"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"},
|
||||
}
|
||||
|
||||
# Caddy terminates TLS (even for localhost) and tells us so (WEB-02, WEB-07). Secure cookies
|
||||
# apply wherever there is HTTPS: that is every Docker run, and not `runserver` in DEBUG.
|
||||
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
|
||||
SESSION_COOKIE_SECURE = not DEBUG
|
||||
CSRF_COOKIE_SECURE = not DEBUG
|
||||
SESSION_COOKIE_HTTPONLY = True
|
||||
SESSION_COOKIE_SAMESITE = "Lax"
|
||||
CSRF_COOKIE_SAMESITE = "Lax"
|
||||
SECURE_CONTENT_TYPE_NOSNIFF = True
|
||||
X_FRAME_OPTIONS = "DENY"
|
||||
SECURE_REFERRER_POLICY = "strict-origin-when-cross-origin"
|
||||
|
||||
LOGIN_URL = "/accedi/"
|
||||
LOGIN_REDIRECT_URL = "/app/"
|
||||
LOGOUT_REDIRECT_URL = "/"
|
||||
|
||||
LOGGING = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"handlers": {"console": {"class": "logging.StreamHandler"}},
|
||||
"root": {"handlers": ["console"], "level": os.environ.get("LOG_LEVEL", "INFO")},
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
"""Settings for the test suite: a throw-away secret and an in-memory database."""
|
||||
|
||||
import os
|
||||
|
||||
os.environ.setdefault("DJANGO_SECRET_KEY", "test-secret-" + "x" * 60)
|
||||
|
||||
from .settings import * # noqa: E402,F401,F403
|
||||
|
||||
# A fast hasher: the tests create many users, and Argon2 is slow on purpose.
|
||||
PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
|
||||
STORAGES = {
|
||||
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
|
||||
"staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"},
|
||||
}
|
||||
MIDDLEWARE = [m for m in MIDDLEWARE if "whitenoise" not in m] # no collected static files in tests
|
||||
@@ -0,0 +1,7 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path("", include("apps.web.urls")),
|
||||
]
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
|
||||
application = get_wsgi_application()
|
||||
Reference in new issue
Block a user