mirror of
https://github.com/aykhans/portfolio-blog.git
synced 2025-09-08 15:10:45 +00:00
Added MongoDB
This commit is contained in:
@@ -3,44 +3,56 @@ from pathlib import Path
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import (
|
||||
EmailStr,
|
||||
PostgresDsn
|
||||
PostgresDsn,
|
||||
MongoDsn
|
||||
)
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
PROJECT_NAME: str = 'FastAPI Portfolio & Blog'
|
||||
|
||||
MAIN_PATH: Path = (
|
||||
Path(__file__).
|
||||
resolve().
|
||||
parent.
|
||||
parent.
|
||||
parent
|
||||
) # path to src folder
|
||||
APP_PATH: Path = MAIN_PATH / 'app' # path to app folder
|
||||
MEDIA_FOLDER: Path = Path('media') # name of media folder
|
||||
MEDIA_PATH: Path = MAIN_PATH / MEDIA_FOLDER # path to media folder
|
||||
STATIC_FOLDER_NAME: Path = Path('static') # name of static folder
|
||||
STATIC_FOLDER: Path =\
|
||||
MAIN_PATH / STATIC_FOLDER_NAME # path to static folder
|
||||
# -------------------------------- Paths --------------------------------
|
||||
|
||||
# path to src folder
|
||||
MAIN_PATH: Path = Path(__file__).resolve().parent.parent.parent
|
||||
# path to app folder
|
||||
APP_PATH: Path = MAIN_PATH / 'app'
|
||||
# name of media folder
|
||||
MEDIA_FOLDER: Path = Path('media')
|
||||
# path to media folder
|
||||
MEDIA_PATH: Path = MAIN_PATH / MEDIA_FOLDER
|
||||
# name of static folder
|
||||
STATIC_FOLDER_NAME: Path = Path('static')
|
||||
# path to static folder
|
||||
STATIC_FOLDER: Path = MAIN_PATH / STATIC_FOLDER_NAME
|
||||
|
||||
FILE_FOLDERS: dict[str, Path] = {
|
||||
'post_images': Path('post_images'),
|
||||
}
|
||||
|
||||
# ------------------------------ Paths End ------------------------------
|
||||
|
||||
|
||||
# --------------------------------- App ---------------------------------
|
||||
|
||||
SECRET_KEY: str
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 43200 # 30 days
|
||||
|
||||
@property
|
||||
def LOGIN_URL(self) -> str:
|
||||
return self.SECRET_KEY[-10:]
|
||||
|
||||
# ------------------------------- App End -------------------------------
|
||||
|
||||
|
||||
# ------------------------------ PostgreSQL ------------------------------
|
||||
|
||||
POSTGRES_USER: str
|
||||
POSTGRES_PASSWORD: str
|
||||
POSTGRES_SERVER: str
|
||||
POSTGRES_PORT: int = 5432
|
||||
POSTGRES_DB: str
|
||||
|
||||
@property
|
||||
def LOGIN_URL(self) -> str:
|
||||
return self.SECRET_KEY[-10:]
|
||||
|
||||
def get_postgres_dsn(self, _async: bool = False) -> PostgresDsn:
|
||||
scheme = 'postgresql+asyncpg' if _async else 'postgresql'
|
||||
|
||||
@@ -53,6 +65,30 @@ class Settings(BaseSettings):
|
||||
path=self.POSTGRES_DB
|
||||
)
|
||||
|
||||
# ---------------------------- PostgreSQL End ----------------------------
|
||||
|
||||
|
||||
# ------------------------------- MongoDB -------------------------------
|
||||
|
||||
MONGO_INITDB_ROOT_USERNAME: str
|
||||
MONGO_INITDB_ROOT_PASSWORD: str
|
||||
MONGO_DB_LOGS_NAME: str = 'logs'
|
||||
|
||||
@property
|
||||
def MONGO_DB_DSN(self) -> MongoDsn:
|
||||
return MongoDsn.build(
|
||||
scheme='mongodb',
|
||||
username=self.MONGO_INITDB_ROOT_USERNAME,
|
||||
password=self.MONGO_INITDB_ROOT_PASSWORD,
|
||||
host='mongodb',
|
||||
port=27017,
|
||||
)
|
||||
|
||||
# ----------------------------- MongoDB End -----------------------------
|
||||
|
||||
|
||||
# -------------------------------- Email ---------------------------------
|
||||
|
||||
SMTP_SSL_TLS: bool = True
|
||||
SMTP_PORT: int = 587
|
||||
SMTP_HOST: str = "smtp.gmail.com"
|
||||
@@ -62,5 +98,6 @@ class Settings(BaseSettings):
|
||||
EMAILS_FROM_NAME: str = PROJECT_NAME
|
||||
EMAIL_RECIPIENTS: list[EmailStr] = []
|
||||
|
||||
# ------------------------------ Email End -------------------------------
|
||||
|
||||
settings = Settings()
|
||||
|
@@ -10,11 +10,13 @@ from slowapi.errors import RateLimitExceeded
|
||||
from app.core.config import settings
|
||||
from app.views.router import main_router
|
||||
from app.utils.rate_limiter import limiter
|
||||
from app.utils.mongodb_utils import create_mongodb_database_if_not_exists
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title=settings.PROJECT_NAME
|
||||
)
|
||||
|
||||
app.state.limiter = limiter
|
||||
|
||||
# app.mount(
|
||||
@@ -23,8 +25,23 @@ app.state.limiter = limiter
|
||||
# name='static'
|
||||
# )
|
||||
|
||||
# --------------------------------- Routers ---------------------------------
|
||||
|
||||
app.include_router(main_router)
|
||||
|
||||
# ------------------------------- Routers end -------------------------------
|
||||
|
||||
|
||||
# ---------------------------------- Events ----------------------------------
|
||||
|
||||
@app.on_event('startup')
|
||||
async def app_startup():
|
||||
await create_mongodb_database_if_not_exists('logs')
|
||||
|
||||
# -------------------------------- Events end --------------------------------
|
||||
|
||||
|
||||
# ---------------------------- Exception handlers ----------------------------
|
||||
|
||||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
||||
|
||||
@@ -37,3 +54,5 @@ async def validation_exception_handler(request, exc):
|
||||
@app.exception_handler(404)
|
||||
async def custom_404_handler(_, __):
|
||||
return FileResponse(settings.STATIC_FOLDER / '404.jpg')
|
||||
|
||||
# -------------------------- Exception handlers end --------------------------
|
12
src/app/utils/mongodb_utils.py
Normal file
12
src/app/utils/mongodb_utils.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from contextlib import contextmanager
|
||||
|
||||
from app.views.depends import get_mongo_client
|
||||
|
||||
|
||||
async def create_mongodb_database_if_not_exists(
|
||||
db_name: str
|
||||
) -> None:
|
||||
|
||||
with contextmanager(get_mongo_client)() as client:
|
||||
if db_name not in client.list_database_names():
|
||||
client[db_name].create_collection('init')
|
@@ -8,10 +8,14 @@ from typing import (
|
||||
from PIL import Image
|
||||
from jose import jwt
|
||||
|
||||
from pydantic import ValidationError
|
||||
from pydantic import (
|
||||
ValidationError
|
||||
)
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from pymongo import MongoClient
|
||||
|
||||
from fastapi import (
|
||||
Cookie,
|
||||
Depends,
|
||||
@@ -50,6 +54,11 @@ async def get_async_db() -> Generator:
|
||||
yield async_db
|
||||
|
||||
|
||||
def get_mongo_client() -> Generator:
|
||||
with MongoClient(str(settings.MONGO_DB_DSN)) as client:
|
||||
yield client
|
||||
|
||||
|
||||
async def get_access_token_from_cookie_or_die(
|
||||
access_token: Annotated[str, Cookie()]
|
||||
) -> str:
|
||||
|
Reference in New Issue
Block a user