Skip to content

Commit

Permalink
feat: new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
GoToProd committed Nov 20, 2024
1 parent dd6d314 commit 47bb361
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 77 deletions.
5 changes: 5 additions & 0 deletions backend/app/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from fastapi import APIRouter
from app.api.api_v1.endpoints import overlay

api_router = APIRouter()
api_router.include_router(overlay.router, prefix="/overlay", tags=["overlay"])
Empty file.
30 changes: 30 additions & 0 deletions backend/app/api/v1/endpoints/overlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from fastapi import APIRouter, Depends
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 app.db.session import get_db

router = APIRouter()

@router.get("/", response_model=list[Overlay])
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]

@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

@router.post("/")
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
Empty file added backend/app/crud/__init__.py
Empty file.
26 changes: 0 additions & 26 deletions backend/app/db.py

This file was deleted.

Empty file added backend/app/db/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions backend/app/db/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
20 changes: 20 additions & 0 deletions backend/app/db/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel

DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/valory"
engine = create_async_engine(DATABASE_URL, echo=True, future=True)

AsyncSessionLocal = sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
)

async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
yield session

async def init_db():
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
50 changes: 0 additions & 50 deletions backend/app/main.py

This file was deleted.

Empty file added backend/app/schemas/__init__.py
Empty file.
File renamed without changes.
5 changes: 4 additions & 1 deletion backend/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
if dotenv_file.is_file():
load_dotenv(dotenv_file)

DEBUG = ast.literal_eval(environ.get('DEBUG'))
DEBUG = ast.literal_eval(environ.get('DEBUG'))

PROJECT_NAME = environ.get('PROJECT_NAME')
VERSION = environ.get('VERSION')
37 changes: 37 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware

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

@asynccontextmanager
async def lifespan(app: FastAPI):
# Выполняется при старте приложения
await init_db()
print("Application startup complete.")

yield # Контроль передается приложению

# Выполняется при завершении работы приложения
print("Application shutdown complete.")

app = FastAPI(
title=settings.PROJECT_NAME,
version=settings.VERSION,
lifespan=lifespan
)

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(api_router, prefix="/api/v1")

0 comments on commit 47bb361

Please sign in to comment.