-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sinasezza/sinasezza
feat(config/database): connection of app to mongodb database added to…
- Loading branch information
Showing
28 changed files
with
237 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from functools import lru_cache | ||
from pathlib import Path | ||
|
||
from dotenv import load_dotenv | ||
from pydantic import Field | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
load_dotenv(BASE_DIR / ".env") | ||
|
||
|
||
class Settings(BaseSettings): | ||
debug: bool = Field(default=False) | ||
database_url: str = "mongodb://localhost:27017" | ||
database_name: str = "chat_app" | ||
jwt_secret_key: str = "your-secret-key" | ||
jwt_algorithm: str = Field(default="HS256") | ||
|
||
# CORS settings | ||
cors_allow_origins: list[str] = Field(default=["*"]) | ||
cors_allow_credentials: bool = Field(default=True) | ||
cors_allow_methods: list[str] = Field(default=["*"]) | ||
cors_allow_headers: list[str] = Field(default=["*"]) | ||
|
||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8") | ||
|
||
|
||
@lru_cache | ||
def get_settings() -> Settings: | ||
return Settings() | ||
|
||
|
||
UPLOAD_DIR = BASE_DIR / "uploads" | ||
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from motor.motor_asyncio import AsyncIOMotorClient | ||
|
||
from .config import get_settings | ||
|
||
settings = get_settings() | ||
|
||
# Global variable to hold the database client | ||
db_client = None | ||
db = None | ||
|
||
|
||
async def connect_to_mongodb(): | ||
global db_client, db | ||
db_client = AsyncIOMotorClient(settings.database_url, maxPoolSize=10, minPoolSize=1) | ||
db = db_client[settings.database_name] | ||
print("Connected to MongoDB") | ||
|
||
|
||
async def close_mongodb_connection(): | ||
global db_client | ||
if db_client: | ||
db_client.close() | ||
print("Closed MongoDB connection") |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import socketio | ||
import uvicorn | ||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
from chatApp.config.config import get_settings | ||
from chatApp.config.database import close_mongodb_connection, connect_to_mongodb | ||
from chatApp.routes import auth, chat, user | ||
|
||
# Fetch settings | ||
settings = get_settings() | ||
|
||
app = FastAPI() | ||
|
||
# Configure CORS using settings | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=settings.cors_allow_origins, | ||
allow_credentials=settings.cors_allow_credentials, | ||
allow_methods=settings.cors_allow_methods, | ||
allow_headers=settings.cors_allow_headers, | ||
) | ||
|
||
# Create a Socket.IO server | ||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*") | ||
|
||
# Wrap with ASGI application | ||
socket_app = socketio.ASGIApp(sio, app) | ||
|
||
|
||
@app.get("/") | ||
async def root(): | ||
return {"message": "Welcome to the FastAPI Chat App"} | ||
|
||
|
||
# Define lifespan event handlers | ||
async def lifespan(app: FastAPI): | ||
# On startup | ||
await connect_to_mongodb() | ||
|
||
yield | ||
|
||
# On shutdown | ||
await close_mongodb_connection() | ||
|
||
|
||
app.include_router(auth.router) | ||
app.include_router(chat.router) | ||
app.include_router(user.router) | ||
|
||
|
||
@sio.event | ||
async def connect(sid, environ): | ||
print(f"Client connected: {sid}") | ||
|
||
|
||
@sio.event | ||
async def disconnect(sid): | ||
print(f"Client disconnected: {sid}") | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run("main:socket_app", host="0.0.0.0", port=8000, reload=True) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() |
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[mypy] | ||
ignore_missing_imports = True |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters