feat: 初始化接口
This commit is contained in:
parent
e668cc533b
commit
d076d249c0
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"is_init": false,
|
"is_init": true,
|
||||||
"db_type": "sqlite",
|
"db_type": "sqlite_3",
|
||||||
"mysql": {
|
"mysql": {
|
||||||
"host": "",
|
"host": "",
|
||||||
"port": "",
|
"port": "",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
CONFIG_PATH = Path(__file__).parent.parent / "config" / "init.json"
|
||||||
|
|
||||||
|
|
||||||
|
def _sync_load_config() -> dict:
|
||||||
|
with CONFIG_PATH.open("r", encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
def _sync_save_config(config_data: dict):
|
||||||
|
with CONFIG_PATH.open("w", encoding="utf-8") as f:
|
||||||
|
json.dump(config_data, f, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
|
# 异步接口
|
||||||
|
async def load_init_config() -> dict:
|
||||||
|
return await asyncio.to_thread(_sync_load_config)
|
||||||
|
|
||||||
|
|
||||||
|
async def save_init_config(config_data: dict):
|
||||||
|
await asyncio.to_thread(_sync_save_config, config_data)
|
||||||
|
|
@ -4,16 +4,7 @@ import json
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from models.schemas.system import InitStatusResponse, InitConfigRequest
|
from models.schemas.system import InitStatusResponse, InitConfigRequest
|
||||||
|
from core.config import load_init_config, save_init_config
|
||||||
|
|
||||||
# 响应类型
|
|
||||||
|
|
||||||
|
|
||||||
def load_init_config():
|
|
||||||
"""读取配置文件"""
|
|
||||||
config_path = Path(__file__).parent / "config" / "init.json"
|
|
||||||
with open(config_path, "r", encoding="utf-8") as f:
|
|
||||||
return json.load(f)
|
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
|
|
@ -30,7 +21,7 @@ def create_app():
|
||||||
response_model=InitStatusResponse,
|
response_model=InitStatusResponse,
|
||||||
)
|
)
|
||||||
async def check_init():
|
async def check_init():
|
||||||
init_config = load_init_config()
|
init_config = await load_init_config()
|
||||||
is_init = init_config.get("is_init")
|
is_init = init_config.get("is_init")
|
||||||
return InitStatusResponse(data=is_init)
|
return InitStatusResponse(data=is_init)
|
||||||
|
|
||||||
|
|
@ -41,7 +32,15 @@ def create_app():
|
||||||
response_model=InitStatusResponse,
|
response_model=InitStatusResponse,
|
||||||
)
|
)
|
||||||
async def init_system(request: InitConfigRequest):
|
async def init_system(request: InitConfigRequest):
|
||||||
return InitStatusResponse()
|
init_config = await load_init_config()
|
||||||
|
if init_config.get('is_init'):
|
||||||
|
return InitStatusResponse(data=True)
|
||||||
|
init_config["db_type"] = request.db_type
|
||||||
|
if request.db_type == "mysql":
|
||||||
|
pass
|
||||||
|
init_config["is_init"] = True
|
||||||
|
await save_init_config(init_config)
|
||||||
|
return InitStatusResponse(data=True)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,12 @@ class BaseResponse(BaseModel):
|
||||||
|
|
||||||
class InitConfigRequest(BaseModel):
|
class InitConfigRequest(BaseModel):
|
||||||
db_type: str
|
db_type: str
|
||||||
db_host: str | None
|
|
||||||
db_port: str | None
|
|
||||||
db_user: str | None
|
|
||||||
db_password: str | None
|
|
||||||
db_name: str | None
|
|
||||||
admin_password: str
|
admin_password: str
|
||||||
|
db_host: str | None = None
|
||||||
|
db_port: str | None = None
|
||||||
|
db_user: str | None = None
|
||||||
|
db_password: str | None = None
|
||||||
|
db_name: str | None = None
|
||||||
|
|
||||||
|
|
||||||
class InitStatusResponse(BaseResponse):
|
class InitStatusResponse(BaseResponse):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue