s
This commit is contained in:
53
web.py
53
web.py
@@ -7,17 +7,17 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
app = FastAPI(title="多进程录制控制台")
|
||||
|
||||
# ---------------- 配置(修正为根目录结构) ----------------
|
||||
# ---------------- 配置(根目录结构) ----------------
|
||||
BASE_DIR = Path(__file__).parent
|
||||
CONFIG_DIR = BASE_DIR / "config" # 直接在根目录下
|
||||
DEFAULT_LOG_DIR = BASE_DIR / ".pm2" / "logs" # 直接在根目录下
|
||||
CONFIG_DIR = BASE_DIR / "config" # config 文件夹在项目根目录
|
||||
DEFAULT_LOG_DIR = BASE_DIR / ".pm2" / "logs" # .pm2/logs 在项目根目录
|
||||
|
||||
MAX_LOG_LINES = 300
|
||||
|
||||
# 支持的进程列表
|
||||
VALID_PROCESSES = {"tiktok", "youtube", "obs"}
|
||||
|
||||
# CORS(支持 POST)
|
||||
# CORS(支持 GET 和 POST)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
@@ -86,7 +86,7 @@ async def get_process_info(process: str) -> dict:
|
||||
out_path = value
|
||||
elif key == "err_log_path":
|
||||
err_path = value
|
||||
# 备用默认路径(修正为根目录)
|
||||
# 备用默认路径
|
||||
if not out_path:
|
||||
out_path = str(DEFAULT_LOG_DIR / f"{process}-out.log")
|
||||
if not err_path:
|
||||
@@ -97,11 +97,11 @@ async def get_process_info(process: str) -> dict:
|
||||
"err_path": err_path,
|
||||
}
|
||||
|
||||
# ---------------- 配置路由 ----------------
|
||||
# ---------------- 配置路由(youtube.ini,仅 youtube) ----------------
|
||||
@app.get("/get_config")
|
||||
async def get_config(process: str = Query(..., description="进程名")):
|
||||
if process != "youtube":
|
||||
return JSONResponse({"error": "仅 youtube 支持配置编辑"}, status_code=400)
|
||||
return JSONResponse({"error": "仅 youtube 支持此配置编辑"}, status_code=400)
|
||||
|
||||
config_file = CONFIG_DIR / "youtube.ini"
|
||||
if not config_file.exists():
|
||||
@@ -115,7 +115,7 @@ async def get_config(process: str = Query(..., description="进程名")):
|
||||
@app.post("/save_config")
|
||||
async def save_config(request: Request, process: str = Query(..., description="进程名")):
|
||||
if process != "youtube":
|
||||
return JSONResponse({"error": "仅 youtube 支持配置编辑"}, status_code=400)
|
||||
return JSONResponse({"error": "仅 youtube 支持此配置编辑"}, status_code=400)
|
||||
|
||||
try:
|
||||
data = await request.json()
|
||||
@@ -131,7 +131,41 @@ async def save_config(request: Request, process: str = Query(..., description="
|
||||
except Exception as e:
|
||||
return JSONResponse({"error": f"保存失败: {str(e)}"}, status_code=500)
|
||||
|
||||
# ---------------- 原有路由 ----------------
|
||||
# ---------------- 配置路由(URL_config.ini,youtube 和 tiktok 共享) ----------------
|
||||
@app.get("/get_url_config")
|
||||
async def get_url_config(process: str = Query(..., description="进程名")):
|
||||
if process not in ["youtube", "tiktok"]:
|
||||
return JSONResponse({"error": "仅 youtube 和 tiktok 支持此配置编辑"}, status_code=400)
|
||||
|
||||
config_file = CONFIG_DIR / "URL_config.ini"
|
||||
if not config_file.exists():
|
||||
return {"content": "# URL_config.ini 文件不存在,将创建空文件\n"}
|
||||
try:
|
||||
content = config_file.read_text(encoding="utf-8")
|
||||
return {"content": content}
|
||||
except Exception as e:
|
||||
return JSONResponse({"error": f"读取失败: {str(e)}"}, status_code=500)
|
||||
|
||||
@app.post("/save_url_config")
|
||||
async def save_url_config(request: Request, process: str = Query(..., description="进程名")):
|
||||
if process not in ["youtube", "tiktok"]:
|
||||
return JSONResponse({"error": "仅 youtube 和 tiktok 支持此配置编辑"}, status_code=400)
|
||||
|
||||
try:
|
||||
data = await request.json()
|
||||
content = data.get("content", "")
|
||||
except:
|
||||
return JSONResponse({"error": "无效的 JSON 数据"}, status_code=400)
|
||||
|
||||
config_file = CONFIG_DIR / "URL_config.ini"
|
||||
try:
|
||||
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
config_file.write_text(content, encoding="utf-8")
|
||||
return {"message": "配置保存成功"}
|
||||
except Exception as e:
|
||||
return JSONResponse({"error": f"保存失败: {str(e)}"}, status_code=500)
|
||||
|
||||
# ---------------- PM2 控制路由 ----------------
|
||||
@app.get("/start")
|
||||
async def start(process: str = Query(..., description="进程名")):
|
||||
if process not in VALID_PROCESSES:
|
||||
@@ -177,6 +211,7 @@ async def status(process: str = Query(..., description="进程名")):
|
||||
"recent_error": recent_error,
|
||||
})
|
||||
|
||||
# ---------------- 首页 ----------------
|
||||
@app.get("/")
|
||||
async def index():
|
||||
return FileResponse(BASE_DIR / "index.html")
|
||||
|
||||
Reference in New Issue
Block a user