Skip to content

Commit

Permalink
add ping, get all servers
Browse files Browse the repository at this point in the history
  • Loading branch information
themanyfaceddemon committed Oct 18, 2024
1 parent 3af3a58 commit 109068a
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 1 deletion.
8 changes: 8 additions & 0 deletions hub_dbs/main_db/bans_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ async def _add_ban(
),
)

if entity_type == "ip":
await db.execute(
"""
DELETE FROM servers_connect WHERE ip_address = ?;
""",
(entity_name,),
)

if system:
LogsDatabase.log_system_action(
server_name=None,
Expand Down
52 changes: 52 additions & 0 deletions hub_dbs/main_db/server_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,58 @@ def _hash_token(cls, token: str) -> str:
def _verify_token(cls, token: str, token_hash: str) -> bool:
return bcrypt.checkpw(token.encode("utf-8"), token_hash.encode("utf-8"))

@classmethod
async def get_all_active_servers(cls) -> list[dict]:
active_servers = []

async with Database() as db:
async with db.execute(
"""
SELECT server_name, dns_name, ip_address, port, current_players, max_players
FROM servers_connect
WHERE status = 'online';
"""
) as cursor:
servers = await cursor.fetchall()
for server in servers:
active_servers.append({
"server_name": server[0],
"dns_name": server[1],
"ip_address": server[2],
"port": server[3],
"current_players": server[4],
"max_players": server[5],
})

return active_servers

@classmethod
async def get_server_info(cls, dns_name: str) -> Optional[dict]:
async with Database() as db:
async with db.execute(
"""
SELECT server_name, ip_address, port, status, max_players, current_players
FROM servers_connect
WHERE dns_name = ?;
""",
(dns_name,),
) as cursor:
server_row = await cursor.fetchone()

if not server_row:
return None

server_info = {
"server_name": server_row[0],
"ip_address": server_row[1],
"port": server_row[2],
"status": server_row[3],
"max_players": server_row[4],
"current_players": server_row[5],
}

return server_info

@classmethod
async def add_server(
cls,
Expand Down
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import FastAPI

from hub_dbs import Database, LogsDatabase, RoleManager, UserManager
from routes import user_router
from routes import connect_router, info_router, user_router


@asynccontextmanager
Expand All @@ -20,6 +20,8 @@ async def lifespan(app: FastAPI):


app = FastAPI(lifespan=lifespan)
app.include_router(connect_router)
app.include_router(info_router)
app.include_router(user_router)

if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .connect import connect_router
from .info import info_router
from .user import user_router
26 changes: 26 additions & 0 deletions routes/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from fastapi import APIRouter, HTTPException

from hub_dbs.main_db import ClusterManager, ServerManager

connect_router = APIRouter(prefix="/api_v1/connect")


@connect_router.get("/{full_path}")
async def connect(full_path: str):
try:
type_connect, name = full_path.split(".")

except ValueError:
raise HTTPException(status_code=400, detail="Invalid format. Use 'type.name'.")

result = None
if type_connect == "cluster":
result = await ClusterManager.get_least_loaded_server(name)

elif type_connect == "single":
result = await ServerManager.get_server_info(name)

if result is None:
raise HTTPException(status_code=404, detail=f"{type_connect}.{name} not found")

return result
15 changes: 15 additions & 0 deletions routes/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fastapi import APIRouter

from hub_dbs.main_db import ServerManager

info_router = APIRouter(prefix='/api_v1/')


@info_router.get("/")
async def get_servers():
return await ServerManager.get_all_active_servers()


@info_router.get("/ping")
async def ping():
return {"message": "pong!"}
Empty file added routes/server.py
Empty file.
1 change: 1 addition & 0 deletions routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async def logout(
try:
if full_logout:
await UserManager.full_logout(token)

else:
await UserManager.logout(token)

Expand Down

0 comments on commit 109068a

Please sign in to comment.