File size: 2,024 Bytes
f44e85b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from motor.motor_asyncio import AsyncIOMotorClient
from typing import List, Optional
from fastapi.middleware.cors import CORSMiddleware

# MongoDB connection URI
MONGO_URI = "mongodb+srv://npanchayan:ramnagar@cluster0.7stwm.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"

# Initialize FastAPI app
app = FastAPI()

# Allow CORS for all origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins, change this to more specific if needed
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# MongoDB client
client = AsyncIOMotorClient(MONGO_URI)
db = client.disease  # MongoDB database
disease_collection = db.diseases  # MongoDB collection

# Pydantic model to represent the Disease schema
class Disease(BaseModel):
    diseaseName: str
    description: str
    cause: str
    symptoms: List[str]
    treatments: List[str]
    preventionTips: Optional[List[str]] = []
    imageURL: Optional[str] = None
    createdAt: str

# API endpoint to fetch disease details by name
@app.get("/disease/{name}", response_model=Disease)
async def get_disease(name: str):
    # Fetch the disease from MongoDB
    disease = await disease_collection.find_one({"diseaseName": name})
    
    if disease is None:
        raise HTTPException(status_code=404, detail="Disease not found")
    
    # Convert MongoDB document to Pydantic model
    disease_data = Disease(
        diseaseName=disease["diseaseName"],
        description=disease["description"],
        cause=disease["cause"],
        symptoms=disease["symptoms"],
        treatments=disease["treatments"],
        preventionTips=disease.get("preventionTips", []),
        imageURL=disease.get("imageURL", None),
        createdAt=disease["createdAt"].isoformat(),
    )
    
    return disease_data

# Run the FastAPI server using Uvicorn
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=5000)