完成ezapi

This commit is contained in:
cxykevin 2024-07-15 22:55:11 +08:00
parent 0974a12f10
commit 57392cec3d
7 changed files with 125 additions and 16 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
**/__pycache__ **/__pycache__
config.toml config.toml
debug_cmd.lst debug_cmd.lst
venv venv
log.log

View File

@ -14,6 +14,8 @@ port = 3306
user = "root" user = "root"
passwd = "passwd" passwd = "passwd"
dbname = "myjsonDB" dbname = "myjsonDB"
minsize = 100
maxsize = 100
[log] [log]
path = "log.log" path = "log.log"

View File

@ -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

View File

@ -16,6 +16,8 @@ DB_PORT: int = configs.get("db", {}).get("port", 3306)
DB_USER: str = configs.get("db", {}).get("user", "root") DB_USER: str = configs.get("db", {}).get("user", "root")
DB_PASSWD: str = configs.get("db", {}).get("passwd", "<no password>") DB_PASSWD: str = configs.get("db", {}).get("passwd", "<no password>")
DB_NAME: str = configs.get("db", {}).get("dbname", "myjsonDB") 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) USE_EMACE_MOD: bool = configs.get("console", {}).get("emacsmode", True)
SIGTERM_CMD: bool = configs.get("console", {}).get("softstop", True) SIGTERM_CMD: bool = configs.get("console", {}).get("softstop", True)

View File

@ -131,9 +131,9 @@ async def create_table(dbname: str, name: str) -> tuple[int, str | None]:
return (0, None) return (0, None)
async def err_callback(err_info): async def err_callback(err_info):
if ("sys_db_list.PRIMARY" in err_info): if ("already exists" in err_info):
await log.warn("Duplicate database names: "+str(name)) await log.warn("Duplicate table name: "+str(name))
return (4, "Duplicate database names: "+str(name)) return (4, "Duplicate table name: "+str(name))
else: else:
await log.warn("Other error on create table: "+str(err_info)) await log.warn("Other error on create table: "+str(err_info))
return (4, "Other error: "+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) return (0, None)
async def err_callback(err_info): async def err_callback(err_info):
await log.warn("Other error on create key: "+str(err_info)) if (".PRIMARY" in err_info):
return (5, "Other error on create key: "+str(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, return await sqllink.execute_cmd(connects,
command=f""" command=f"""
INSERT INTO `user_{dbname.replace("_", "__")}_{name.replace("_", "__")}`( INSERT INTO `user_{dbname.replace("_", "__")}_{name.replace("_", "__")}`(

View File

@ -1,3 +1,4 @@
from decimal import MIN_EMIN
from src import log from src import log
from src import config from src import config
import aiomysql import aiomysql
@ -17,7 +18,9 @@ async def connect_db() -> aiomysql.pool.Pool:
port=config.DB_PORT, port=config.DB_PORT,
user=config.DB_USER, user=config.DB_USER,
password=config.DB_PASSWD, password=config.DB_PASSWD,
db=config.DB_NAME db=config.DB_NAME,
maxsize=config.DB_MAXSIZE,
minsize=config.DB_MINSIZE
) )
except Exception as exp: except Exception as exp:
await log.err("Connect MYSQL server error") await log.err("Connect MYSQL server error")

View File

@ -1,10 +1,30 @@
from src.webcore import app from src.webcore import app
from src import core from src import core
from fastapi import Response from pydantic import BaseModel
version = "v1" 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) @app.post("/api/"+version+"/database/{database_name}", status_code=201)
async def create_database(database_name: str): async def create_database(database_name: str):
ret = await core.create_jsondb(database_name) 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) @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) ret = await core.rm_jsondb(database_name)
if (ret[0] == 0): if (ret[0] == 0):
return {"status": 200} return {"status": 200}
return {"status": 400+ret[0], "errormsg": ret[1]} 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"}