diff --git a/__pycache__/main.cpython-312.pyc b/__pycache__/main.cpython-312.pyc index 70d9475..3636ba8 100644 Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ diff --git a/main.py b/main.py index a880789..0d61206 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,7 @@ -from fastapi import FastAPI +from fastapi import FastAPI, Depends from pydantic import BaseModel -from rcon.battleye import Client import json -import os +from rcon.battleye import Client app = FastAPI() @@ -15,37 +14,53 @@ SERVER_ADDRESS = credentials.get('SERVER_ADDRESS', '') SERVER_PORT = credentials.get('SERVER_PORT', 0) RCON_PASSWORD = credentials.get('RCON_PASSWORD', '') +class RCONManager: + def __init__(self, address, port, password): + self.client = Client(address, port, passwd=password) + self.client.connect() + + def run(self, command): + return self.client.run(command) + + def close(self): + self.client.disconnect() + +rcon_manager = RCONManager(SERVER_ADDRESS, SERVER_PORT, RCON_PASSWORD) + +@app.on_event("shutdown") +def shutdown_event(): + rcon_manager.close() + class PlayerAction(BaseModel): steamid: str reason: str duration: int -@app.get("/players") -def list_players(): - with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: - response = client.run('players') - return {"response": response} +def get_rcon_manager(): + return rcon_manager +@app.get("/players") +def list_players(rcon: RCONManager = Depends(get_rcon_manager)): + response = rcon.run('players') + return {"response": response} + +# Ähnliche Änderungen für die anderen Endpunkte anwenden @app.post("/kick") -def kick_player(action: PlayerAction): - with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: - response = client.run(f'kick {action.steamid} {action.reason}') - return {"response": response} +def kick_player(action: PlayerAction, rcon: RCONManager = Depends(get_rcon_manager)): + response = rcon.run(f'kick {action.steamid} {action.reason}') + return {"response": response} @app.post("/ban") -def ban_player(action: PlayerAction): - with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: - response = client.run(f'ban {action.steamid} {action.duration} "{action.reason}"') - return {"response": response} +def ban_player(action: PlayerAction, rcon: RCONManager = Depends(get_rcon_manager)): + response = rcon.run(f'ban {action.steamid} {action.duration} "{action.reason}"') + return {"response": response} @app.post("/unlock") -def unlock_server(): - with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: - response = client.run('#unlock') - return {"response": response} +def unlock_server(rcon: RCONManager = Depends(get_rcon_manager)): + response = rcon.run('#unlock') + return {"response": response} @app.post("/lock") -def lock_server(): - with Client(SERVER_ADDRESS, SERVER_PORT, passwd=RCON_PASSWORD) as client: - response = client.run('#lock') - return {"response": response} +def lock_server(rcon: RCONManager = Depends(get_rcon_manager)): + response = rcon.run('#lock') + return {"response": response}