-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
68 lines (54 loc) · 2.49 KB
/
main.py
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
"""
This module is used for creating a FastAPI application.
"""
# Importing necessary libraries and modules
from loguru import logger # For logging
import uvicorn # ASGI server
from fastapi import FastAPI # Web framework
from schemas.fastapi_schemas import User, Quiz # Importing User and Quiz schemas
from database.mongodb import MongoDB # MongoDB database connection
from endpoint_modules.generate_quiz import generate_quiz_from # Function to generate quiz
from endpoint_modules.generate_career_path import generate_career_path # Function to generate career path
from fastapi.middleware.cors import CORSMiddleware # Middleware for managing Cross-Origin Resource Sharing (CORS)
import json # For handling JSON data
# Creating an instance of FastAPI with a title
app = FastAPI(title="genius_guru_backend")
# List of allowed origins for CORS. Currently allowing all origins.
origins = ["*"] # TODO: update this to limit requests for specific origins
# Adding CORS middleware to the FastAPI app
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Allowing the origins defined above
allow_credentials=True, # Allowing credentials
allow_methods=["*"], # Allowing all HTTP methods
allow_headers=["*"], # Allowing all headers
)
# Creating a MongoDB client instance
client = MongoDB()
@app.post("/generate_career_path")
async def generate_career_path_api(submitted_quiz: Quiz):
"""
This is a POST endpoint for submitting the quiz and generating a career path.
"""
print('submitted_quiz', submitted_quiz) # Logging the submitted quiz
# Loading courses from the offline database
with open("offline_database/courses/Courses.json", "r") as json_file:
courses = json.load(json_file)
# Returning the generated career path based on the submitted quiz and loaded courses
return generate_career_path(submitted_quiz, courses)
@app.post("/sign_up")
async def sign_up(user_data: User):
"""
This is a POST endpoint for user sign up.
"""
client.add_user(user_data) # Adding the user to the database
@app.get("/get_quiz", response_model=Quiz)
async def get_quiz(user_major):
"""
This is a GET endpoint for generating a quiz based on the user's major.
"""
quiz = generate_quiz_from(user_major) # Generating quiz
return quiz # Returning the generated quiz
# Running the FastAPI application using uvicorn when this module is run directly
if __name__ == "__main__":
uvicorn.run("main:app", host="localhost", port=8000, reload=True)