mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-16 21:20:39 +03:00
add auth context (#62)
* add user table and superuser creation * add gpt-4-stream-aivvm provider * rename user migration to auth migration
This commit is contained in:
0
bot_microservice/core/auth/__init__.py
Normal file
0
bot_microservice/core/auth/__init__.py
Normal file
0
bot_microservice/core/auth/models/__init__.py
Normal file
0
bot_microservice/core/auth/models/__init__.py
Normal file
22
bot_microservice/core/auth/models/users.py
Normal file
22
bot_microservice/core/auth/models/users.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from fastapi_users_db_sqlalchemy import SQLAlchemyBaseUserTable
|
||||
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyBaseAccessTokenTable
|
||||
from sqlalchemy import INTEGER, VARCHAR, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, declared_attr, mapped_column
|
||||
|
||||
from infra.database.base import Base
|
||||
|
||||
|
||||
class User(SQLAlchemyBaseUserTable[Mapped[int]], Base):
|
||||
__tablename__ = "users" # type: ignore[assignment]
|
||||
|
||||
id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
|
||||
email: Mapped[str] = mapped_column(VARCHAR(length=320), unique=True, nullable=True) # type: ignore[assignment]
|
||||
username: Mapped[str] = mapped_column(VARCHAR(length=32), unique=True, index=True, nullable=False)
|
||||
|
||||
|
||||
class AccessToken(SQLAlchemyBaseAccessTokenTable[Mapped[int]], Base):
|
||||
__tablename__ = "access_token" # type: ignore[assignment]
|
||||
|
||||
@declared_attr
|
||||
def user_id(cls) -> Mapped[int]:
|
||||
return mapped_column(INTEGER, ForeignKey("users.id", ondelete="cascade"), nullable=False)
|
||||
@@ -1,18 +1,11 @@
|
||||
from asyncio import current_task
|
||||
from typing import Awaitable, Callable
|
||||
|
||||
from fastapi import FastAPI
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
AsyncSession,
|
||||
async_scoped_session,
|
||||
async_sessionmaker,
|
||||
create_async_engine,
|
||||
)
|
||||
|
||||
from settings.config import AppSettings
|
||||
from infra.database.db_adapter import Database
|
||||
|
||||
|
||||
def startup(app: FastAPI, settings: AppSettings) -> Callable[[], Awaitable[None]]:
|
||||
def startup(app: FastAPI, database: Database) -> Callable[[], Awaitable[None]]:
|
||||
"""
|
||||
Actions to run on application startup.
|
||||
|
||||
@@ -20,13 +13,13 @@ def startup(app: FastAPI, settings: AppSettings) -> Callable[[], Awaitable[None]
|
||||
such as db_engine.
|
||||
|
||||
:param app: the fastAPI application.
|
||||
:param settings: app settings
|
||||
:param database: app settings
|
||||
:return: function that actually performs actions.
|
||||
|
||||
"""
|
||||
|
||||
async def _startup() -> None:
|
||||
_setup_db(app, settings)
|
||||
_setup_db(app, database)
|
||||
|
||||
return _startup
|
||||
|
||||
@@ -46,7 +39,7 @@ def shutdown(app: FastAPI) -> Callable[[], Awaitable[None]]:
|
||||
return _shutdown
|
||||
|
||||
|
||||
def _setup_db(app: FastAPI, settings: AppSettings) -> None:
|
||||
def _setup_db(app: FastAPI, database: Database) -> None:
|
||||
"""
|
||||
Create connection to the database.
|
||||
|
||||
@@ -56,18 +49,5 @@ def _setup_db(app: FastAPI, settings: AppSettings) -> None:
|
||||
|
||||
:param app: fastAPI application.
|
||||
"""
|
||||
engine = create_async_engine(
|
||||
str(settings.async_db_url),
|
||||
echo=settings.DB_ECHO,
|
||||
execution_options={"isolation_level": "AUTOCOMMIT"},
|
||||
)
|
||||
session_factory = async_scoped_session(
|
||||
async_sessionmaker(
|
||||
engine,
|
||||
expire_on_commit=False,
|
||||
class_=AsyncSession,
|
||||
),
|
||||
scopefunc=current_task,
|
||||
)
|
||||
app.state.db_engine = engine
|
||||
app.state.db_session_factory = session_factory
|
||||
app.state.db_engine = database.async_engine
|
||||
app.state.db_session_factory = database._async_session_factory
|
||||
|
||||
Reference in New Issue
Block a user