-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (66 loc) · 2.43 KB
/
main.py
File metadata and controls
81 lines (66 loc) · 2.43 KB
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from databases import Database
import os
import random
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
allowed_origins = [
"http://localhost:3000", # Assuming your React app runs on localhost:3000
"https://gen-bridge-frontend.vercel.app/", # Your production React app domain
]
# Add CORSMiddleware to the application
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows specified origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# Database URL from environment variable
DATABASE_URL = os.getenv("DATABASE_URL").replace("postgres://", "postgresql://", 1)
# Initialize the Database
database = Database(DATABASE_URL)
# Define a Pydantic model for the User
class User(BaseModel):
name: str
senior: bool
interests: list[str]
calendly: str
# In-memory storage for demonstration
users = []
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
@app.get("/")
async def root():
return {"message": "Welcome to the GenBridge Backend!"}
@app.post("/signup/")
async def signup(user: User):
query = "INSERT INTO users(name, senior, interests, calendly) VALUES (:name, :senior, :interests, :calendly)"
values = {"name": user.name, "senior": user.senior, "interests": user.interests, "calendly": user.calendly}
await database.execute(query=query, values=values)
return {"message": f"User {user.name} signed up successfully!"}
@app.post("/match/")
async def match(for_user: User):
# Fetch users from the database
match_senior = not for_user.senior
query = "SELECT * FROM users WHERE senior = :match_senior"
logger.info("Querying database: %s", query)
values = {"match_senior": match_senior}
result = await database.fetch_all(query=query, values=values)
if not result:
raise HTTPException(status_code=404, detail="No users found")
match = random.choice(result)
if not match:
logger.info("No match found")
return {"message": "No matching users found", "user": match}
else:
logger.info("Found match: %s", match)
return {"message": "Matched users successfully!", "user": match}