from fastapi import APIRouter, Depends, FastAPI, Security, status from fastapi.staticfiles import StaticFiles from db import Player, Team, Chemistry, MVPRanking, engine from sqlmodel import ( Session, select, ) from fastapi.middleware.cors import CORSMiddleware from analysis import analysis_router from security import ( change_password, get_current_active_user, login_for_access_token, logout, read_player_me, read_own_items, set_first_password, ) app = FastAPI(title="cutt") api_router = APIRouter(prefix="/api") origins = [ "https://cutt.0124816.xyz", "http://localhost:5173", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) def add_team(team: Team): with Session(engine) as session: session.add(team) session.commit() def add_player(player: Player): with Session(engine) as session: session.add(player) session.commit() def add_players(players: list[Player]): with Session(engine) as session: for player in players: session.add(player) session.commit() def list_players(): with Session(engine) as session: statement = select(Player).order_by(Player.name) return session.exec(statement).fetchall() def list_teams(): with Session(engine) as session: statement = select(Team) return session.exec(statement).fetchall() player_router = APIRouter(prefix="/player") player_router.add_api_route("/list", endpoint=list_players, methods=["GET"]) player_router.add_api_route("/add", endpoint=add_player, methods=["POST"]) player_router.add_api_route("/me", endpoint=read_player_me, methods=["GET"]) player_router.add_api_route("/me/items", endpoint=read_own_items, methods=["GET"]) player_router.add_api_route( "/change_password", endpoint=change_password, methods=["POST"] ) team_router = APIRouter(prefix="/team") team_router.add_api_route("/list", endpoint=list_teams, methods=["GET"]) team_router.add_api_route("/add", endpoint=add_team, methods=["POST"]) @app.post("/mvps/", status_code=status.HTTP_200_OK) def submit_mvps(mvps: MVPRanking): with Session(engine) as session: session.add(mvps) session.commit() @app.post("/chemistry/", status_code=status.HTTP_200_OK) def submit_chemistry(chemistry: Chemistry): with Session(engine) as session: session.add(chemistry) session.commit() class SPAStaticFiles(StaticFiles): async def get_response(self, path: str, scope): response = await super().get_response(path, scope) if response.status_code == 404: response = await super().get_response(".", scope) return response api_router.include_router( player_router, dependencies=[Depends(get_current_active_user)] ) api_router.include_router(team_router, dependencies=[Depends(get_current_active_user)]) api_router.include_router( analysis_router, dependencies=[Security(get_current_active_user, scopes=["analysis"])], ) api_router.add_api_route("/token", endpoint=login_for_access_token, methods=["POST"]) api_router.add_api_route("/set_password", endpoint=set_first_password, methods=["POST"]) api_router.add_api_route("/logout", endpoint=logout, methods=["POST"]) app.include_router(api_router) app.mount("/", SPAStaticFiles(directory="dist", html=True), name="site")