Skip to content

Commit

Permalink
feat: I wrote it all over again
Browse files Browse the repository at this point in the history
  • Loading branch information
haxgun committed Dec 6, 2024
1 parent 07a5e63 commit 0bff8f1
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 219 deletions.
2 changes: 1 addition & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uv.lock
sqlite.db
.venv
.ruff_cache
Pipfile.lock
25 changes: 25 additions & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
aiosqlite = "*"
alembic = "*"
asyncpg = "*"
fastapi = {extras = ["standard"], version = "*"}
greenlet = "*"
psycopg2-binary = "*"
python-dotenv = "*"
sqlalchemy = "*"
sqlmodel = "*"
uuid = "*"
uvicorn = "*"

[dev-packages]

[requires]
python_version = "3.13"

[scripts]
dev = "fastapi dev main.py"
6 changes: 0 additions & 6 deletions backend/app/api/__init__.py

This file was deleted.

Empty file.
23 changes: 0 additions & 23 deletions backend/app/api/v1/endpoints/overlay.py

This file was deleted.

Empty file removed backend/app/crud/__init__.py
Empty file.
27 changes: 0 additions & 27 deletions backend/app/crud/overlay.py

This file was deleted.

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

from app.settings import DATABASE_URL
from app.models import overlays

engine = create_async_engine(DATABASE_URL, echo=False)


async def init_db():
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)


async def get_session():
async_session = sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session:
yield session
Empty file removed backend/app/db/__init__.py
Empty file.
26 changes: 0 additions & 26 deletions backend/app/db/session.py

This file was deleted.

Empty file removed backend/app/models/__init___.py
Empty file.
31 changes: 0 additions & 31 deletions backend/app/models/overlay.py

This file was deleted.

20 changes: 20 additions & 0 deletions backend/app/models/overlays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import uuid

from sqlmodel import SQLModel, Field


class OverlayBase(SQLModel):
riot_id: str
hdev_api_key: str


class Overlay(OverlayBase, table=True):
id: uuid.UUID = Field(
default_factory=uuid.uuid4,
primary_key=True,
nullable=False,
)


class OverlayCreate(OverlayBase):
pass
6 changes: 6 additions & 0 deletions backend/app/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from fastapi import APIRouter

from app.routers import overlays

api_router = APIRouter()
api_router.include_router(overlays.router, prefix="/overlay", tags=["overlay"])
36 changes: 36 additions & 0 deletions backend/app/routers/overlays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import uuid
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession

from app.db import get_session
from app.models.overlays import Overlay, OverlayCreate

router = APIRouter()


@router.get('/', response_model=list[Overlay])
async def get_overlays(session: AsyncSession = Depends(get_session)):
result = await session.exec(select(Overlay))
overlays = result.all()
return [Overlay(id=overlay.id, riot_id=overlay.riot_id, hdev_api_key=overlay.hdev_api_key) for overlay in overlays]

@router.get("/{overlay_id}")
async def get_overlay(overlay_id: uuid.UUID, session: AsyncSession = Depends(get_session)):
statement = select(Overlay).where(Overlay.id == overlay_id)
result = await session.exec(statement)
overlay = result.first()

if overlay is None:
raise HTTPException(status_code=404, detail="Overlay not found")

return overlay


@router.post("/")
async def create_overlay(overlay: OverlayCreate, session: AsyncSession = Depends(get_session)):
overlay = Overlay(riot_id=overlay.riot_id, hdev_api_key=overlay.hdev_api_key)
session.add(overlay)
await session.commit()
await session.refresh(overlay)
return overlay
Empty file removed backend/app/schemas/__init__.py
Empty file.
22 changes: 0 additions & 22 deletions backend/app/schemas/overlay.py

This file was deleted.

38 changes: 15 additions & 23 deletions backend/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import secrets
from os import environ
from typing import Literal

from pydantic_settings import BaseSettings, SettingsConfigDict
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 = Path(__file__).resolve().parent.parent.parent

class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file="../.env",
env_ignore_empty=True,
extra="ignore",
)
API_V1_STR: str = "/api/v1"
SECRET_KEY: str = secrets.token_urlsafe(32)
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
FRONTEND_HOST: str = "http://localhost:5173"
ENVIRONMENT: Literal["local", "staging", "production"] = "local"

PROJECT_NAME: str = environ.get("PROJECT_NAME")
dotenv_file: Path = BASE_DIR / ".env"
if dotenv_file.is_file():
load_dotenv(dotenv_file)

DEBUG: bool = str_to_bool(environ.get("DEBUG", "False"))
PROJECT_NAME: str = environ.get("PROJECT_NAME")

PROJECT_NAME: str = environ.get("PROJECT_NAME")
VERSION: str = environ.get("VERSION")
DEBUG: bool = str_to_bool(environ.get("DEBUG", "False"))

DATABASE_URL: str = environ.get("DATABASE_URL")
DEBUG_DATABASE_URL: str = environ.get("DEBUG_DATABASE_URL")
PROJECT_NAME: str = environ.get("PROJECT_NAME")
VERSION: str = environ.get("VERSION")

DATABASE_LOGIN: str = environ.get("DATABASE_LOGIN")
DATABASE_PASSWORD: str = environ.get("DATABASE_PASSWORD")
DATABASE_NAME: str = environ.get("DATABASE_NAME")
DATABASE_PORT: int = int(environ.get("DATABASE_PORT", 5432))

settings = Settings()
DATABASE_URL: str = f"postgresql+asyncpg://{DATABASE_LOGIN}:{DATABASE_PASSWORD}@localhost:{DATABASE_PORT}/{DATABASE_NAME}"
18 changes: 7 additions & 11 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import uvicorn
from fastapi import FastAPI
from contextlib import asynccontextmanager
from fastapi.middleware.cors import CORSMiddleware
from app.api import api_router
from app.db.session import init_db
from app.settings import settings
from app import settings
from app.routers import api_router


@asynccontextmanager
async def lifespan(app: FastAPI):
await init_db()
yield

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

app.add_middleware(
Expand All @@ -28,5 +20,9 @@ async def lifespan(app: FastAPI):

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

@app.get("/")
async def root():
return {"message": "Hello World"}

if __name__ == "__main__":
uvicorn.run(app="main:app", reload=True)
uvicorn.run(app="main:app", reload=True, port=8080)
49 changes: 0 additions & 49 deletions backend/pyproject.toml

This file was deleted.

0 comments on commit 0bff8f1

Please sign in to comment.