working auth + users systems
This commit is contained in:
BIN
backend/core/__pycache__/celery_app.cpython-312.pyc
Normal file
BIN
backend/core/__pycache__/celery_app.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/config.cpython-312.pyc
Normal file
BIN
backend/core/__pycache__/config.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/database.cpython-312.pyc
Normal file
BIN
backend/core/__pycache__/database.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/exceptions.cpython-312.pyc
Normal file
BIN
backend/core/__pycache__/exceptions.cpython-312.pyc
Normal file
Binary file not shown.
10
backend/core/celery_app.py
Normal file
10
backend/core/celery_app.py
Normal file
@@ -0,0 +1,10 @@
|
||||
# core/celery_app.py
|
||||
from celery import Celery
|
||||
from core.config import settings
|
||||
|
||||
celery = Celery(
|
||||
"maia",
|
||||
broker=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/0",
|
||||
backend=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/1",
|
||||
include=["modules.auth.tasks"], # List all task modules here
|
||||
)
|
||||
21
backend/core/config.py
Normal file
21
backend/core/config.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# core/config.py
|
||||
from pydantic_settings import BaseSettings
|
||||
from os import getenv
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv() # Load .env file
|
||||
|
||||
class Settings(BaseSettings):
|
||||
DB_URL: str = "postgresql://maia:maia@localhost:5432/maia"
|
||||
|
||||
REDIS_HOST: str = "localhost"
|
||||
REDIS_PORT: int = 6379
|
||||
|
||||
JWT_ALGORITHM: str = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
||||
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
|
||||
|
||||
PEPPER: str = getenv("PEPPER", "")
|
||||
JWT_SECRET_KEY: str = getenv("JWT_SECRET_KEY", "")
|
||||
|
||||
settings = Settings()
|
||||
36
backend/core/database.py
Normal file
36
backend/core/database.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# core/database.py
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, Session, declarative_base
|
||||
from typing import Generator
|
||||
|
||||
from core.config import settings
|
||||
|
||||
Base = declarative_base() # Used for models
|
||||
|
||||
_engine = None
|
||||
_SessionLocal = None
|
||||
|
||||
def get_engine():
|
||||
global _engine
|
||||
if _engine is None:
|
||||
if not settings.DB_URL:
|
||||
raise ValueError("DB_URL is not set in Settings.")
|
||||
print(f"Connecting to database at {settings.DB_URL}")
|
||||
_engine = create_engine(settings.DB_URL)
|
||||
Base.metadata.create_all(_engine) # Create tables here
|
||||
return _engine
|
||||
|
||||
def get_sessionmaker():
|
||||
global _SessionLocal
|
||||
if _SessionLocal is None:
|
||||
engine = get_engine()
|
||||
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
return _SessionLocal
|
||||
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
SessionLocal = get_sessionmaker()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
27
backend/core/exceptions.py
Normal file
27
backend/core/exceptions.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from fastapi import HTTPException
|
||||
from starlette.status import (
|
||||
HTTP_400_BAD_REQUEST,
|
||||
HTTP_401_UNAUTHORIZED,
|
||||
HTTP_403_FORBIDDEN,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
HTTP_409_CONFLICT,
|
||||
)
|
||||
|
||||
def bad_request_exception(detail: str = "Bad Request"):
|
||||
return HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=detail)
|
||||
|
||||
def unauthorized_exception(detail: str = "Unauthorized"):
|
||||
return HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=detail)
|
||||
|
||||
def forbidden_exception(detail: str = "Forbidden"):
|
||||
return HTTPException(status_code=HTTP_403_FORBIDDEN, detail=detail)
|
||||
|
||||
def not_found_exception(detail: str = "Not Found"):
|
||||
return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=detail)
|
||||
|
||||
def internal_server_error_exception(detail: str = "Internal Server Error"):
|
||||
return HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail=detail)
|
||||
|
||||
def conflict_exception(detail: str = "Conflict"):
|
||||
return HTTPException(status_code=HTTP_409_CONFLICT, detail=detail)
|
||||
Reference in New Issue
Block a user