A set of simple utilities for Django apps running on Fly.io.
Note: This package is designed to work with V2 Apps. It has not been tested with V1 Apps.
pip install django-flyio-
Set your
DATABASESsettings to callget_db_configfromdjango_flyio.db.This function reads the
PRIMARY_REGIONandFLY_REGIONenvironment variables to determine the Fly region the primary database is in and the Fly region the app is currently running in. -
Add
django_flyio.routers.FlyDBReplicaRouterto yourDATABASE_ROUTERSsettings.This router reads whether the
DATABASESsetting has areplicakey and, if so, routes reads to the replica database.
# settings.py
from django_flyio.db import get_db_config
DATABASES = get_db_config()
DATABASE_ROUTERS = ["django_flyio.routers.FlyDBReplicaRouter"]To set the Fly-Server header containing the Fly server and region that served the request, add django_flyio.middleware.FlyResponseMiddleware to your MIDDLEWARE settings.
# settings.py
MIDDLEWARE = [
# ...
'django_flyio.middleware.FlyResponseMiddleware',
]django-flyio is distributed under the terms of the MIT license.