Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions backend/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from . import models
from .routes import send_email, user
Expand All @@ -29,6 +30,20 @@ async def lifespan(_: FastAPI):
# Source: https://stackoverflow.com/questions/77170361/
# running-alembic-migrations-on-fastapi-startup
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:3000",
"https://uw-blueprint-starter-code.firebaseapp.com",
"https://uw-blueprint-starter-code.web.app",
# TODO: create a separate middleware function to dynamically
# determine this value
# re.compile("^https:\/\/uw-blueprint-starter-code--pr.*\.web\.app$"),
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(user.router)
app.include_router(send_email.router)

Expand Down
25 changes: 16 additions & 9 deletions backend/app/utilities/ses/ses_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
TEMPLATES_FILE = "app/utilities/ses/ses_templates.json"
TEMPLATES_DIR = "app/utilities/ses/template_files"

ses_client = boto3.client(
"ses",
region_name=os.getenv("AWS_REGION"),
aws_access_key_id=os.getenv("AWS_ACCESS_KEY"),
aws_secret_access_key=os.getenv("AWS_SECRET_KEY"),
)


def load_templates_metadata(file_path: str) -> Dict:
try:
Expand All @@ -39,7 +32,7 @@ def load_file_content(file_path: str) -> str:


# Function to create SES template
def create_ses_template(template_metadata):
def create_ses_template(template_metadata, ses_client):
name = template_metadata["TemplateName"]
try:
text_part = load_file_content(template_metadata["TextPart"])
Expand All @@ -66,6 +59,20 @@ def create_ses_template(template_metadata):
# Ensure SES templates are available at app startup
def ensure_ses_templates():
templates_metadata = load_templates_metadata(TEMPLATES_FILE)
aws_region = os.getenv("AWS_REGION")
aws_access_key = os.getenv("AWS_ACCESS_KEY")
aws_secret_key = os.getenv("AWS_SECRET_KEY")

if not all([aws_region, aws_access_key, aws_secret_key]):
print("AWS credentials not set. Skipping SES template setup.")
return

ses_client = boto3.client(
"ses",
region_name=aws_region,
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key,
)

for template_metadata in templates_metadata:
name = template_metadata["TemplateName"]
Expand All @@ -76,6 +83,6 @@ def ensure_ses_templates():
except ClientError as e:
if e.response["Error"]["Code"] == "TemplateDoesNotExist":
print(f"SES template '{name}' does not exist. Creating template...")
create_ses_template(template_metadata)
create_ses_template(template_metadata, ses_client)
else:
print(f"An error occurred while checking the SES template: {e}")