Skip to content

Commit bd854c9

Browse files
committed
feat: added fastapi-mongo sample
Signed-off-by: Syed Ali Ul Hasan <syedaliulhasan19@gmail.com>
1 parent a8453d3 commit bd854c9

16 files changed

Lines changed: 784 additions & 0 deletions

File tree

fastapi-mongo/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/__pycache__/**
2+
**/__init__.py/**
3+
__init__.py
4+
.DS_Store

fastapi-mongo/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 1. Use a lightweight Python 3.10 image
2+
FROM python:3.10-slim
3+
4+
# 2. Set environment variables
5+
ENV PYTHONDONTWRITEBYTECODE=1
6+
ENV PYTHONUNBUFFERED=1
7+
8+
# 3. Set working directory
9+
WORKDIR /app
10+
11+
# 4. Install system dependencies
12+
RUN apt-get update && apt-get install -y --no-install-recommends \
13+
build-essential \
14+
gcc \
15+
libpq-dev \
16+
curl \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
# 5. Copy requirements.txt from the root folder
20+
COPY ../requirements.txt .
21+
22+
# 6. Install Python dependencies
23+
RUN pip install --upgrade pip && pip install -r requirements.txt
24+
25+
# 7. Copy everything from quizzly folder (backend code)
26+
COPY . .
27+
28+
# 8. Expose port
29+
EXPOSE 8000
30+
31+
# 9. Run FastAPI app with uvicorn
32+
CMD ["uvicorn", "quizzly.main:app", "--host", "0.0.0.0", "--port", "8000"]

fastapi-mongo/auth/jwt_handler.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from jose import jwt
2+
from datetime import datetime, timedelta
3+
from dotenv import load_dotenv
4+
import os
5+
6+
load_dotenv()
7+
8+
SECRET_KEY = os.getenv("SECRET_KEY")
9+
ALGORITHM = "HS256"
10+
11+
12+
def create_access_token(data: dict, expires_delta: timedelta = timedelta(days=1)):
13+
to_encode = data.copy()
14+
expire = datetime.utcnow() + expires_delta
15+
to_encode.update({"exp": expire})
16+
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
17+
18+
19+
def decode_access_token(token: str):
20+
return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])

fastapi-mongo/core/config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# app/core/config.py
2+
from pydantic_settings import BaseSettings
3+
4+
5+
class Settings(BaseSettings):
6+
MONGODB_URI: str
7+
SECRET_KEY: str
8+
FRONTEND_URL: str
9+
GEMINI_API_KEY: str
10+
YOUTUBE_API_KEY: str
11+
CORS_ORIGINS: str
12+
13+
class Config:
14+
env_file = ".env"
15+
16+
17+
settings = Settings()

fastapi-mongo/db/mongodb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# app/db/mongodb.py
2+
from motor.motor_asyncio import AsyncIOMotorClient
3+
from quizzly.core.config import settings
4+
5+
client = AsyncIOMotorClient(settings.MONGODB_URI)
6+
db = client.quiz_app
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Keploy API Test Generator for FastAPI with MongoDB
2+
3+
This project demonstrates how to use Keploy to automatically generate API tests for a FastAPI application that interacts with a MongoDB database. Keploy captures the API requests and responses, allowing you to create comprehensive test cases effortlessly.
4+
5+
Keploy API Test Generator: https://keploy.io/docs/running-keploy/api-test-generator/
6+
7+
### About the Project
8+
9+
This is FastAPI + MongoDB backend written for a Quiz Application. The application allows users to perform CRUD operations on quiz data stored in a MongoDB database.
128 KB
Loading
565 KB
Loading

fastapi-mongo/main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# app/main.py
2+
from fastapi import FastAPI
3+
from quizzly.routes import parent, quiz, content
4+
from fastapi.middleware.cors import CORSMiddleware
5+
from fastapi.middleware.cors import CORSMiddleware
6+
from quizzly.routes import parent, quiz
7+
from quizzly.core.config import settings
8+
9+
10+
app = FastAPI()
11+
# Read and parse CORS origins
12+
origins_str = settings.CORS_ORIGINS
13+
origins = [origin.strip() for origin in origins_str.split(",") if origin.strip()]
14+
15+
print(origins)
16+
17+
# Enable CORS
18+
app.add_middleware(
19+
CORSMiddleware,
20+
allow_origins=origins,
21+
allow_credentials=True,
22+
allow_methods=["*"],
23+
allow_headers=["*"],
24+
)
25+
26+
app.include_router(parent.router, prefix="/parent", tags=["Parent"])
27+
app.include_router(quiz.router, prefix="/quiz", tags=["Quiz"])
28+
app.include_router(content.router, prefix="/content", tags=["Content"])

fastapi-mongo/models/parent.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# app/models/parent.py
2+
from pydantic import BaseModel, Field
3+
from typing import Optional
4+
from bson import ObjectId
5+
6+
7+
class ParentCreate(BaseModel):
8+
username: str
9+
password: str
10+
name: Optional[str]
11+
12+
13+
class Parent(BaseModel):
14+
id: Optional[str] = Field(default_factory=lambda: str(ObjectId()), alias="_id")
15+
name: str
16+
username: str
17+
password: str

0 commit comments

Comments
 (0)