-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
82 additions
and
35 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -67,4 +67,5 @@ coverage | |
*.sln | ||
*.sw? | ||
package-lock.json | ||
pnpm-lock.yaml | ||
pnpm-lock.yaml | ||
.env |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from fastapi import APIRouter | ||
from app.api.api_v1.endpoints import overlay | ||
from app.api.v1.endpoints import overlay | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(overlay.router, prefix="/overlay", tags=["overlay"]) |
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 |
---|---|---|
@@ -1,30 +1,21 @@ | ||
from fastapi import APIRouter, Depends | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.orm import Session | ||
from app.schemas.user import UserCreate | ||
from app.crud.user import create_user | ||
from sqlalchemy.future import select | ||
|
||
from app.schemas.overlay import OverlaySchema, OverlayCreate | ||
from app.crud.overlay import Overlay | ||
from app.db.session import get_db | ||
|
||
router = APIRouter() | ||
|
||
@router.get("/", response_model=list[Overlay]) | ||
@router.get("/", response_model=list[OverlaySchema]) | ||
async def get_overlays(session: AsyncSession = Depends(get_db)): | ||
result = await session.execute(select(Overlay)) | ||
overlays = result.scalars().all() | ||
return [Overlay(riotId=overlay.riotId, hdevApiKey=overlay.hdevApiKey, uuid=overlay.uuid) for overlay in overlays] | ||
return get_overlays(session) | ||
|
||
@router.get("/{overlay_id}", response_model=OverlaySchema) | ||
async def get_overlay(overlay_id: str, session: AsyncSession = Depends(get_db)): | ||
result = await session.execute(select(Overlay).filter(Overlay.uuid == overlay_id)) | ||
overlay = result.scalars().first() | ||
if overlay is None: | ||
raise HTTPException(status_code=404, detail="Overlay not found") | ||
return overlay | ||
return get_overlay(overlay_id, session) | ||
|
||
@router.post("/") | ||
@router.post("/", response_model=OverlaySchema) | ||
async def add_overlay(overlay: OverlayCreate, session: AsyncSession = Depends(get_db)): | ||
overlay = Overlay(riotId=overlay.riotId, hdevApiKey=overlay.hdevApiKey) | ||
session.add(overlay) | ||
await session.commit() | ||
await session.refresh(overlay) | ||
return overlay | ||
return add_overlay(overlay, session) |
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,29 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.future import select | ||
|
||
from app.models.overlay import Overlay | ||
# Импорты для моделей, схем и сессий | ||
from app.schemas.overlay import OverlaySchema, OverlayCreate # Pydantic-схема OverlayCreate | ||
from app.db.session import get_db # Асинхронная сессия базы данных | ||
|
||
router = APIRouter() | ||
|
||
async def get_overlays(session: AsyncSession = Depends(get_db)): | ||
result = await session.execute(select(Overlay)) | ||
overlays = result.scalars().all() | ||
return [OverlayCreate(riotId=overlay.riotId, hdevApiKey=overlay.hdevApiKey, uuid=overlay.uuid) for overlay in overlays] | ||
|
||
async def get_overlay(overlay_id: str, session: AsyncSession = Depends(get_db)): | ||
result = await session.execute(select(Overlay).filter(Overlay.uuid == overlay_id)) | ||
overlay = result.scalars().first() | ||
if overlay is None: | ||
raise HTTPException(status_code=404, detail="Overlay not found") | ||
return overlay | ||
|
||
async def add_overlay(overlay: OverlaySchema, session: AsyncSession = Depends(get_db)): | ||
new_overlay = Overlay(riotId=overlay.riotId, hdevApiKey=overlay.hdevApiKey) | ||
session.add(new_overlay) | ||
await session.commit() | ||
await session.refresh(new_overlay) | ||
return new_overlay |
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 |
---|---|---|
@@ -1,11 +1,25 @@ | ||
from uuid import UUID | ||
import uuid as uuid_pkg | ||
|
||
from pydantic import BaseModel | ||
|
||
from app.models.overlay import OverlayBase | ||
|
||
|
||
class OverlaySchema(BaseModel): | ||
uuid: UUID | ||
uuid: uuid_pkg.UUID | ||
nickname: str | ||
tag: str | ||
|
||
class Config: | ||
orm_mode = True # Включает поддержку ORM для SQLAlchemy моделей | ||
|
||
# Схема для создания записи | ||
class OverlayCreate(OverlayBase): | ||
pass # Наследует все поля от OverlayBase, используется для валидации входных данных | ||
|
||
# Схема для ответа | ||
class OverlayRead(OverlayBase): | ||
uuid: uuid_pkg.UUID # Добавляем поле UUID в схему для ответа | ||
|
||
class Config: | ||
orm_mode = True # Поддержка работы с SQLAlchemy/SQLModel объектами |
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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import ast | ||
from os import environ | ||
from pathlib import Path | ||
|
||
from dotenv import load_dotenv | ||
|
||
def str_to_bool(value: str) -> bool: | ||
return value.lower() in ("true", "1", "yes") | ||
|
||
BASE_DIR = Path(__file__).resolve().parent.parent.parent | ||
|
||
print(BASE_DIR) | ||
|
||
dotenv_file = BASE_DIR / '.env' | ||
if dotenv_file.is_file(): | ||
load_dotenv(dotenv_file) | ||
|
||
DEBUG = ast.literal_eval(environ.get('DEBUG')) | ||
DEBUG = str_to_bool(environ.get("DEBUG", "False")) | ||
|
||
PROJECT_NAME = environ.get('PROJECT_NAME') | ||
VERSION = environ.get('VERSION') |
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 |
---|---|---|
@@ -1,37 +1,45 @@ | ||
from fastapi import FastAPI | ||
from contextlib import asynccontextmanager | ||
|
||
from fastapi import FastAPI, Depends, HTTPException | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from app.api import api_router | ||
from app.db.session import init_db | ||
from app import settings | ||
|
||
from sqlmodel import select | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
|
||
from Valory.backend.app import settings | ||
from Valory.backend.app.api import api_router | ||
|
||
# Определение lifespan для управления событиями старта и завершения | ||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
# Выполняется при старте приложения | ||
await init_db() | ||
print("Application startup complete.") | ||
|
||
yield # Контроль передается приложению | ||
yield # Передача управления основному приложению | ||
|
||
# Выполняется при завершении работы приложения | ||
# Выполняется при завершении приложения | ||
print("Application shutdown complete.") | ||
|
||
|
||
# Создание экземпляра FastAPI с lifespan | ||
app = FastAPI( | ||
title=settings.PROJECT_NAME, | ||
version=settings.VERSION, | ||
lifespan=lifespan | ||
) | ||
|
||
# Подключение CORS middleware | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_origins=["*"], # Замените "*" на список доменов в production | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
# Подключение роутов | ||
app.include_router(api_router, prefix="/api/v1") | ||
|
||
|
||
# Тестовый маршрут | ||
@app.get("/") | ||
async def read_root(): | ||
return {"message": "Welcome to the API!"} |