Skip to content

Commit

Permalink
add static asset build
Browse files Browse the repository at this point in the history
  • Loading branch information
addis-samtek committed Jan 27, 2025
1 parent 4610dd2 commit 49d3c44
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions solution/backend/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,73 +54,73 @@
# print(f"Error handling request: {e}")
# # Re-raise so Lambda logs the error stack trace
# raise
# import os
# import json
# from django.core.asgi import get_asgi_application
# from mangum import Mangum
import os
import json
from django.core.asgi import get_asgi_application
from mangum import Mangum

# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmcs_regulations.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmcs_regulations.settings")

# django_asgi_app = get_asgi_application()
django_asgi_app = get_asgi_application()

# def lambda_handler(event, context):
# # For debugging
# print("=== EVENT ===")
# print(json.dumps(event))
def lambda_handler(event, context):
# For debugging
print("=== EVENT ===")
print(json.dumps(event))

# stage_env = os.environ.get('STAGE_ENV', '')
# # Check what's actually in the request context
# stage_from_event = event.get('requestContext', {}).get('stage', '')
# print(f"Lambda Env STAGE_ENV: {stage_env}")
# print(f"API Gateway Stage: {stage_from_event}")
stage_env = os.environ.get('STAGE_ENV', '')
# Check what's actually in the request context
stage_from_event = event.get('requestContext', {}).get('stage', '')
print(f"Lambda Env STAGE_ENV: {stage_env}")
print(f"API Gateway Stage: {stage_from_event}")

if stage_env:
base_path = f"/{stage_env}"
elif stage_from_event:
base_path = f"/{stage_from_event}"
else:
base_path = ""

# Force Django to use that path for reversed URLs
os.environ["FORCE_SCRIPT_NAME"] = base_path
print(f"Setting FORCE_SCRIPT_NAME to: {base_path}")

# By passing api_gateway_base_path=base_path,
# Mangum will strip that segment from the incoming path
handler = Mangum(
django_asgi_app,
lifespan="off",
api_gateway_base_path=base_path
)

return handler(event, context)
# """
# WSGI config for cmcs_regulations project.

# It exposes the WSGI callable as a module-level variable named ``application``.

# For more information on this file, see:
# https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
# """

# if stage_env:
# base_path = f"/{stage_env}"
# elif stage_from_event:
# base_path = f"/{stage_from_event}"
# else:
# base_path = ""

# # Force Django to use that path for reversed URLs
# os.environ["FORCE_SCRIPT_NAME"] = base_path
# print(f"Setting FORCE_SCRIPT_NAME to: {base_path}")

# # By passing api_gateway_base_path=base_path,
# # Mangum will strip that segment from the incoming path
# handler = Mangum(
# django_asgi_app,
# lifespan="off",
# api_gateway_base_path=base_path
# )

# return handler(event, context)
"""
WSGI config for cmcs_regulations project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see:
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os
from django.core.wsgi import get_wsgi_application
from mangum import Mangum
# import os
# from django.core.wsgi import get_wsgi_application
# from mangum import Mangum

# 1) Point to Django settings if not already set
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmcs_regulations.settings")
# # 1) Point to Django settings if not already set
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmcs_regulations.settings")

# 2) Read the stage name from the environment (passed by CDK, etc.)
stage_env = os.environ.get("STAGE_ENV", "") # e.g. "dev", "eph-1522", "prod"
# # 2) Read the stage name from the environment (passed by CDK, etc.)
# stage_env = os.environ.get("STAGE_ENV", "") # e.g. "dev", "eph-1522", "prod"

# 3) If we have a stage, build the path ("/dev", "/eph-1522", etc.)
base_path = f"/{stage_env}" if stage_env else ""
# # 3) If we have a stage, build the path ("/dev", "/eph-1522", etc.)
# base_path = f"/{stage_env}" if stage_env else ""

# 4) Tell Django to include the base path in absolute URLs or redirects
os.environ["FORCE_SCRIPT_NAME"] = base_path
# # 4) Tell Django to include the base path in absolute URLs or redirects
# os.environ["FORCE_SCRIPT_NAME"] = base_path

# 5) Load the standard Django WSGI application
application = get_wsgi_application()
# # 5) Load the standard Django WSGI application
# application = get_wsgi_application()

# 6) Wrap the Django WSGI app with Mangum, stripping the same base path on inbound requests
lambda_handler = Mangum(application, lifespan="off", api_gateway_base_path=base_path)
# # 6) Wrap the Django WSGI app with Mangum, stripping the same base path on inbound requests
# lambda_handler = Mangum(application, lifespan="off", api_gateway_base_path=base_path)

0 comments on commit 49d3c44

Please sign in to comment.