Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
import boto3 | |
import json | |
import uuid | |
from fastapi.middleware.cors import CORSMiddleware | |
# from mangum import Mangum | |
import logging | |
# AWS S3 Configuration | |
AWS_ACCESS_KEY = "AKIARZ5BMNDRSZIKZIVE" | |
AWS_SECRET_KEY = "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" | |
BUCKET_NAME = "c2r-demo-requests" | |
BUCKET_NAME_PRICING = "c2r-website-pricing-request" | |
# Initialize S3 client | |
s3_client = boto3.client( | |
"s3", | |
aws_access_key_id=AWS_ACCESS_KEY, | |
aws_secret_access_key=AWS_SECRET_KEY, | |
) | |
# Initialize FastAPI | |
app = FastAPI() | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], # Update to a specific domain in production | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Data Model for User Input | |
class UserData(BaseModel): | |
name: str | |
companyName: str | |
email: str | |
class UserDataPricing(BaseModel): | |
name: str | |
email: str | |
interestedInDemo : bool | |
logging.basicConfig(level=logging.INFO) | |
async def process_form(data: UserData): | |
logging.info(f"Received data:{data}") | |
try: | |
# Generate a unique filename | |
filename = f"user_data_{uuid.uuid4()}.json" | |
# Convert user data to JSON format | |
json_data = json.dumps(data.dict()) | |
# Upload JSON data to S3 | |
s3_client.put_object( | |
Bucket=BUCKET_NAME, | |
Key=filename, | |
Body=json_data, | |
ContentType="application/json" | |
) | |
return {"message": "User data saved successfully!"} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error saving user data: {e}") | |
async def process_form(data: UserDataPricing): | |
logging.info(f"Received data:{data}") | |
try: | |
# Generate a unique filename | |
filename = f"user_data_{uuid.uuid4()}.json" | |
# Convert user data to JSON format | |
json_data = json.dumps(data.dict()) | |
# Upload JSON data to S3 | |
s3_client.put_object( | |
Bucket=BUCKET_NAME_PRICING, | |
Key=filename, | |
Body=json_data, | |
ContentType="application/json" | |
) | |
return {"message": "User data saved successfully!"} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error saving user data: {e}") | |
# Lambda handler using Mangum | |
# handler = Mangum(app) |