Compare commits

...

2 Commits

Author SHA1 Message Date
itqop 1e772e7034 Initial commit 2024-02-25 16:55:37 +03:00
itqop 42a03de514 Uptade .gitignore 2024-02-25 16:45:51 +03:00
5 changed files with 89 additions and 0 deletions

1
.gitignore vendored
View File

@ -160,3 +160,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
config.yml

13
PlayerSubscription.py Normal file
View File

@ -0,0 +1,13 @@
from pydantic import BaseModel, field_validator
from typing import Optional
class PlayerSubscription(BaseModel):
username: str
expiry: Optional[int] = None
@field_validator('expiry')
@classmethod
def expiry_must_be_positive_int(cls, v):
if v is not None and not isinstance(v, int) or v < 0:
raise ValueError('Expiry must be a positive integer')
return v

27
db.py Normal file
View File

@ -0,0 +1,27 @@
import aiomysql
from utils import get_parameters
class MySQLDB:
def __init__(self):
self.pool = None
self.parameters = get_parameters()
print(self.parameters)
async def connect(self):
self.pool = await aiomysql.create_pool(
host=self.parameters["host"],
port=self.parameters["port"],
user=self.parameters["user"],
password=self.parameters["password"],
db=self.parameters["db"],
autocommit=True
)
async def close(self):
self.pool.close()
await self.pool.wait_closed()
async def execute(self, query, *args):
async with self.pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(query, args)
return await cur.fetchall()

31
main.py Normal file
View File

@ -0,0 +1,31 @@
from fastapi import FastAPI, HTTPException
from db import MySQLDB
from PlayerSubscription import PlayerSubscription
app = FastAPI()
db = MySQLDB()
@app.post("/subscribe")
async def subscribe(player_data: PlayerSubscription):
await db.connect()
try:
uuid_query = "SELECT uuid FROM luckperms_players WHERE username = %s"
result = await db.execute(uuid_query, player_data.username)
if not result:
raise HTTPException(status_code=404, detail="Player not found")
uuid = result[0][0]
subscription_query = """
INSERT INTO luckperms_user_permissions (uuid, permission, value, server, world, expiry, contexts)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
await db.execute(subscription_query, uuid, 'group.subscribe', 1, 'global', 'global', player_data.expiry, "{}")
finally:
await db.close()
return {"message": "Subscription added successfully"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)

17
utils.py Normal file
View File

@ -0,0 +1,17 @@
import yaml
def read_config(config_file):
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
return config
def get_parameters():
parameters = {}
config = read_config('config.yml')
for key, value in config.items():
if isinstance(value, dict):
for sub_key, sub_value in value.items():
parameters[f'{key}_{sub_key}'] = sub_value
else:
parameters[key] = value
return parameters