feat: run DTF stack with Cloudflare R2
This commit is contained in:
@@ -6,18 +6,40 @@ import boto3
|
||||
from botocore.config import Config
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
def require_local():
|
||||
if os.environ.get('APP_ENV') != 'local':
|
||||
raise RuntimeError('This runtime only supports APP_ENV=local')
|
||||
def require_runtime():
|
||||
"""Validate the supported local and R2-backed deployment modes.
|
||||
|
||||
Real payment, freight, ERP, and WhatsApp providers are deliberately not
|
||||
enabled yet. A non-local deployment keeps those adapters fake and disables
|
||||
checkout until their audited implementations are added.
|
||||
"""
|
||||
environment = os.environ.get('APP_ENV', 'local')
|
||||
for name in ('PAYMENT', 'FREIGHT', 'TINY', 'WHATSAPP'):
|
||||
if os.environ.get(f'{name}_ADAPTER') != 'fake':
|
||||
raise RuntimeError(f'{name} must use the fake adapter')
|
||||
if os.environ.get('STORAGE_ADAPTER') != 's3-local':
|
||||
raise RuntimeError('Only local S3 storage is supported')
|
||||
raise RuntimeError(f'{name} must use the currently supported fake adapter')
|
||||
if environment == 'local':
|
||||
if os.environ.get('STORAGE_ADAPTER') != 's3-local':
|
||||
raise RuntimeError('Local runtime requires local S3 storage')
|
||||
for name in ('S3_ENDPOINT', 'S3_PUBLIC_ENDPOINT'):
|
||||
endpoint = urlparse(os.environ[name])
|
||||
if endpoint.scheme != 'http' or endpoint.hostname not in ('storage', 'localhost', '127.0.0.1'):
|
||||
raise RuntimeError(f'{name} must point to local MinIO')
|
||||
return
|
||||
if environment != 'production':
|
||||
raise RuntimeError('APP_ENV must be local or production')
|
||||
if os.environ.get('STORAGE_ADAPTER') != 's3-r2':
|
||||
raise RuntimeError('Production runtime requires R2 storage')
|
||||
for name in ('S3_ENDPOINT', 'S3_PUBLIC_ENDPOINT'):
|
||||
endpoint = urlparse(os.environ[name])
|
||||
if endpoint.scheme != 'http' or endpoint.hostname not in ('storage', 'localhost', '127.0.0.1'):
|
||||
raise RuntimeError(f'{name} must point to local MinIO')
|
||||
if endpoint.scheme != 'https' or not (endpoint.hostname or '').endswith('.r2.cloudflarestorage.com'):
|
||||
raise RuntimeError(f'{name} must be a Cloudflare R2 S3 API endpoint')
|
||||
for name in ('AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY'):
|
||||
if not os.environ.get(name):
|
||||
raise RuntimeError(f'{name} is required for R2')
|
||||
|
||||
|
||||
# Compatibility alias for local-only callers outside the active runtime.
|
||||
require_local = require_runtime
|
||||
|
||||
class PaymentAdapter(Protocol):
|
||||
def pay(self, quote_id: str, total_cents: int) -> dict: ...
|
||||
|
||||
Reference in New Issue
Block a user