import redis
from django.db import connections
from django.core.exceptions import ImproperlyConfigured
from django.utils.connection import ConnectionDoesNotExist
from rest_framework_simplejwt.authentication import JWTAuthentication
from myapp.models import Company # Company model stored in the global database
class CompanyDBMiddleware:
def __init__(self, get_response):
self.get_response = get_response
self.jwt_authenticator = JWTAuthentication()
self.cache = redis.Redis(host='localhost', port=6379, db=0)
def __call__(self, request):
company_db = self.get_database_for_company(request)
if not company_db:
raise ImproperlyConfigured("Could not determine the company's database.")
# Register connection only if it does not exist in `connections.databases`
if company_db not in connections.databases:
connections.databases[company_db] = {
'ENGINE': 'django.db.backends.postgresql',
'NAME': company_db,
'USER': 'postgres',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
'CONN_MAX_AGE': 60, # To avoid opening and closing connections on each request
}
request.company_db = company_db
response = self.get_response(request)
# Close connection after the response
try:
connections[company_db].close()
except ConnectionDoesNotExist:
pass
return response
def get_database_for_company(self, request):
subdomain = request.get_host().split('.')[0]
company_db = None
cache_key = f"company_db_{subdomain}"
company_db = self.cache.get(cache_key)
if company_db:
return company_db.decode("utf-8")
try:
company = Company.objects.using('default').get(subdomain=subdomain, active=True)
company_db = company.db_name
self.cache.setex(cache_key, 300, company_db) # Cache the database name for 5 minutes
return company_db
except Company.DoesNotExist:
return None
< /code>
Meine Fragen sind: