Keep connection open instead of reconnecting every time
This commit is contained in:
63
main.py
63
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}
|
||||
|
||||
Reference in New Issue
Block a user