File tree Expand file tree Collapse file tree 6 files changed +24
-25
lines changed Expand file tree Collapse file tree 6 files changed +24
-25
lines changed Original file line number Diff line number Diff line change 1+ DEBUG = True
2+
3+ PROJECT_NAME = " My app"
4+ VERSION = " 1.0.0"
5+
6+ DATABASE_URL = " postgresql+asyncpg://postgres:postgres@localhost:5432/valory"
Original file line number Diff line number Diff line change 33from sqlalchemy .future import select
44
55from app .models .overlay import Overlay
6- # Импорты для моделей, схем и сессий
7- from app .schemas .overlay import OverlaySchema , OverlayCreate # Pydantic-схема OverlayCreate
8- from app .db .session import get_db # Асинхронная сессия базы данных
6+ from app .schemas .overlay import OverlaySchema , OverlayCreate
7+ from app .db .session import get_db
98
109router = APIRouter ()
1110
Original file line number Diff line number Diff line change 11from sqlalchemy .ext .asyncio import create_async_engine , AsyncSession
22from sqlalchemy .orm import sessionmaker
33from sqlmodel import SQLModel
4+ from app import settings
45
5- DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/valory"
6- engine = create_async_engine (DATABASE_URL , echo = True , future = True )
6+ engine = create_async_engine (settings .DATABASE_URL , echo = True , future = True )
77
88AsyncSessionLocal = sessionmaker (
99 bind = engine ,
Original file line number Diff line number Diff line change @@ -11,15 +11,14 @@ class OverlaySchema(BaseModel):
1111 tag : str
1212
1313 class Config :
14- orm_mode = True # Включает поддержку ORM для SQLAlchemy моделей
14+ orm_mode = True
1515
1616# Схема для создания записи
1717class OverlayCreate (OverlayBase ):
18- pass # Наследует все поля от OverlayBase, используется для валидации входных данных
18+ pass
1919
20- # Схема для ответа
2120class OverlayRead (OverlayBase ):
22- uuid : uuid_pkg .UUID # Добавляем поле UUID в схему для ответа
21+ uuid : uuid_pkg .UUID
2322
2423 class Config :
25- orm_mode = True # Поддержка работы с SQLAlchemy/SQLModel объектами
24+ orm_mode = True
Original file line number Diff line number Diff line change @@ -8,13 +8,16 @@ def str_to_bool(value: str) -> bool:
88
99BASE_DIR = Path (__file__ ).resolve ().parent .parent .parent
1010
11- print (BASE_DIR )
12-
1311dotenv_file = BASE_DIR / '.env'
1412if dotenv_file .is_file ():
1513 load_dotenv (dotenv_file )
14+ else :
15+ raise ImportError ('⚠ .env was not found' )
16+
1617
1718DEBUG = str_to_bool (environ .get ("DEBUG" , "False" ))
1819
1920PROJECT_NAME = environ .get ('PROJECT_NAME' )
20- VERSION = environ .get ('VERSION' )
21+ VERSION = environ .get ('VERSION' )
22+
23+ DATABASE_URL = environ .get ('DATABASE_URL' )
Original file line number Diff line number Diff line change 66from app import settings
77
88
9- # Определение lifespan для управления событиями старта и завершения
109@asynccontextmanager
1110async def lifespan (app : FastAPI ):
12- # Выполняется при старте приложения
1311 await init_db ()
14- print ("Application startup complete." )
12+ print ("⚠ Application startup complete." )
1513
16- yield # Передача управления основному приложению
14+ yield
1715
18- # Выполняется при завершении приложения
19- print ("Application shutdown complete." )
16+ print ("⚠ Application shutdown complete." )
2017
2118
22- # Создание экземпляра FastAPI с lifespan
2319app = FastAPI (
2420 title = settings .PROJECT_NAME ,
2521 version = settings .VERSION ,
2622 lifespan = lifespan
2723)
2824
29- # Подключение CORS middleware
3025app .add_middleware (
3126 CORSMiddleware ,
32- allow_origins = ["*" ], # Замените "*" на список доменов в production
27+ allow_origins = ["*" ],
3328 allow_credentials = True ,
3429 allow_methods = ["*" ],
3530 allow_headers = ["*" ],
3631)
3732
38- # Подключение роутов
3933app .include_router (api_router , prefix = "/api/v1" )
4034
41-
42- # Тестовый маршрут
4335@app .get ("/" )
4436async def read_root ():
4537 return {"message" : "Welcome to the API!" }
You can’t perform that action at this time.
0 commit comments