Spaces:
Sleeping
Sleeping
File size: 2,534 Bytes
184f5e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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)
@app.post("/process-form")
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}")
@app.post("/pricing-request")
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) |