diff --git a/.gitignore b/.gitignore index 51ae087..41fb1b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/__pycache__ config.toml debug_cmd.lst -venv \ No newline at end of file +venv +log.log \ No newline at end of file diff --git a/config.toml.sample b/config.toml.sample index 48b1e82..aad1baf 100644 --- a/config.toml.sample +++ b/config.toml.sample @@ -14,6 +14,8 @@ port = 3306 user = "root" passwd = "passwd" dbname = "myjsonDB" +minsize = 100 +maxsize = 100 [log] path = "log.log" diff --git a/log.log b/log.log deleted file mode 100644 index a011464..0000000 --- a/log.log +++ /dev/null @@ -1,7 +0,0 @@ -[2024-07-14 20:01:59,825][INFO] Server start -[2024-07-14 20:01:59,825][INFO] Connect database -[2024-07-14 20:01:59,854][INFO] Server version:8.2.0 -[2024-07-14 20:01:59,858][INFO] Use database -[2024-07-14 20:01:59,861][INFO] start webapi -[2024-07-14 20:01:59,862][INFO] Search web plugins -[2024-07-14 20:01:59,862][INFO] + Load ezapi.py diff --git a/src/config.py b/src/config.py index 6e4a05d..1743a61 100644 --- a/src/config.py +++ b/src/config.py @@ -16,6 +16,8 @@ DB_PORT: int = configs.get("db", {}).get("port", 3306) DB_USER: str = configs.get("db", {}).get("user", "root") DB_PASSWD: str = configs.get("db", {}).get("passwd", "") DB_NAME: str = configs.get("db", {}).get("dbname", "myjsonDB") +DB_MINSIZE: str = configs.get("db", {}).get("minsize", 10) +DB_MAXSIZE: str = configs.get("db", {}).get("maxsize", 50) USE_EMACE_MOD: bool = configs.get("console", {}).get("emacsmode", True) SIGTERM_CMD: bool = configs.get("console", {}).get("softstop", True) diff --git a/src/core.py b/src/core.py index 614b31d..df249a5 100644 --- a/src/core.py +++ b/src/core.py @@ -131,9 +131,9 @@ async def create_table(dbname: str, name: str) -> tuple[int, str | None]: return (0, None) async def err_callback(err_info): - if ("sys_db_list.PRIMARY" in err_info): - await log.warn("Duplicate database names: "+str(name)) - return (4, "Duplicate database names: "+str(name)) + if ("already exists" in err_info): + await log.warn("Duplicate table name: "+str(name)) + return (4, "Duplicate table name: "+str(name)) else: await log.warn("Other error on create table: "+str(err_info)) return (4, "Other error: "+str(err_info)) @@ -337,8 +337,12 @@ async def create_key(dbname: str, name: str, key: str | int | tuple, vl: Any) -> return (0, None) async def err_callback(err_info): - await log.warn("Other error on create key: "+str(err_info)) - return (5, "Other error on create key: "+str(err_info)) + if (".PRIMARY" in err_info): + await log.warn("Duplicate key: "+str(key)) + return (5, "Duplicate key: "+str(key)) + else: + await log.warn("Other error on create key: "+str(err_info)) + return (5, "Other error on create key: "+str(err_info)) return await sqllink.execute_cmd(connects, command=f""" INSERT INTO `user_{dbname.replace("_", "__")}_{name.replace("_", "__")}`( diff --git a/src/sqllink.py b/src/sqllink.py index 9499d8f..753e4c4 100644 --- a/src/sqllink.py +++ b/src/sqllink.py @@ -1,3 +1,4 @@ +from decimal import MIN_EMIN from src import log from src import config import aiomysql @@ -17,7 +18,9 @@ async def connect_db() -> aiomysql.pool.Pool: port=config.DB_PORT, user=config.DB_USER, password=config.DB_PASSWD, - db=config.DB_NAME + db=config.DB_NAME, + maxsize=config.DB_MAXSIZE, + minsize=config.DB_MINSIZE ) except Exception as exp: await log.err("Connect MYSQL server error") diff --git a/src/web/ezapi.py b/src/web/ezapi.py index cab4429..da91aab 100644 --- a/src/web/ezapi.py +++ b/src/web/ezapi.py @@ -1,10 +1,30 @@ from src.webcore import app from src import core -from fastapi import Response +from pydantic import BaseModel version = "v1" +class KeyItem(BaseModel): + data: dict | str | int | list | tuple | None = None + + +@app.get("/api/"+version+"/database", status_code=200) +async def list_database(): + ret = await core.list_jsondb() + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"} + + +@app.get("/api/"+version+"/database/{database_name}", status_code=200) +async def check_database(database_name: str): + ret = await core.check_jsondb(database_name) + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"} + + @app.post("/api/"+version+"/database/{database_name}", status_code=201) async def create_database(database_name: str): ret = await core.create_jsondb(database_name) @@ -14,8 +34,92 @@ async def create_database(database_name: str): @app.delete("/api/"+version+"/database/{database_name}", status_code=200) -async def create_database(database_name: str): +async def delete_database(database_name: str): ret = await core.rm_jsondb(database_name) if (ret[0] == 0): return {"status": 200} return {"status": 400+ret[0], "errormsg": ret[1]} + + +@app.get("/api/"+version+"/database/{database_name}/table", status_code=200) +async def list_table(database_name: str): + ret = await core.list_table(database_name) + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"} + + +@app.get("/api/"+version+"/database/{database_name}/table/{table_name}", status_code=200) +async def check_database(database_name: str, table_name: str): + ret = await core.check_table(database_name, table_name) + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"} + + +@app.post("/api/"+version+"/database/{database_name}/table/{table_name}", status_code=201) +async def create_table(database_name: str, table_name: str): + ret = await core.create_table(database_name, table_name) + if (ret[0] == 0): + return {"status": 200} + return {"status": 400+ret[0], "errormsg": ret[1]} + + +@app.delete("/api/"+version+"/database/{database_name}/table/{table_name}", status_code=200) +async def delete_table(database_name: str, table_name: str): + ret = await core.rm_table(database_name, table_name) + if (ret[0] == 0): + return {"status": 200} + return {"status": 400+ret[0], "errormsg": ret[1]} + + +@app.get("/api/"+version+"/database/{database_name}/table/{table_name}/key", status_code=200) +async def list_key(database_name: str, table_name: str): + ret = await core.list_key(database_name, table_name) + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"} + + +@app.get("/api/"+version+"/database/{database_name}/table/{table_name}/key/{key_name}/check", status_code=200) +async def create_key(database_name: str, table_name: str, key_name: int | str): + ret = await core.check_key(database_name, table_name, key_name) + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"} + + +@app.post("/api/"+version+"/database/{database_name}/table/{table_name}/key/{key_name}", status_code=201) +async def create_key(database_name: str, table_name: str, key_name: int | str, data: KeyItem): + if (data.data is None): + return {"status": 400, "errormsg": "No data"} + ret = await core.create_key(database_name, table_name, key_name, data.data) + if (ret[0] == 0): + return {"status": 200} + return {"status": 400+ret[0], "errormsg": ret[1]} + + +@app.delete("/api/"+version+"/database/{database_name}/table/{table_name}/key/{key_name}", status_code=200) +async def delete_key(database_name: str, table_name: str, key_name: int | str): + ret = await core.remove_key(database_name, table_name, key_name) + if (ret[0] == 0): + return {"status": 200} + return {"status": 400+ret[0], "errormsg": ret[1]} + + +@app.patch("/api/"+version+"/database/{database_name}/table/{table_name}/key/{key_name}", status_code=200) +async def create_key(database_name: str, table_name: str, key_name: int | str, data: KeyItem): + if (data.data is None): + return {"status": 400, "errormsg": "No data"} + ret = await core.change_key(database_name, table_name, key_name, data.data) + if (ret[0] == 0): + return {"status": 200} + return {"status": 400+ret[0], "errormsg": ret[1]} + + +@app.get("/api/"+version+"/database/{database_name}/table/{table_name}/key/{key_name}", status_code=200) +async def create_key(database_name: str, table_name: str, key_name: int | str): + ret = await core.get_key(database_name, table_name, key_name) + if (ret is not None): + return {"status": 200, "return": ret} + return {"status": 400, "errormsg": "Unknown Error"}