Compare commits
2 Commits
29b15f07b7
...
1e772e7034
Author | SHA1 | Date |
---|---|---|
itqop | 1e772e7034 | |
itqop | 42a03de514 |
|
@ -160,3 +160,4 @@ cython_debug/
|
|||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
config.yml
|
||||
|
|
|
@ -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
|
|
@ -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()
|
|
@ -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)
|
|
@ -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
|
Loading…
Reference in New Issue