重构第4批:入口注册 serve 命令,更新命令树和项目文档
__init__.py: - 注册 serve 命令(ekb serve) - main() 统一捕获 EkbError,rich 红字输出并 SystemExit(1) commands/serve.py(新建): - ekb serve --host --port --reload - 启动 FastAPI + uvicorn commands/help.py: - 命令树补 serve 分支 CLAUDE.md: - 更新架构分层说明(三层:表现 / 业务 / 传输) - 更新完整项目结构 - 新增 CLI ↔ HTTP Server 接口对照表 - 更新开发环境和工作规范
This commit is contained in:
1 parent
db378b9140
commit
9ec009f2cb
4 files changed
+171
-26
No files matched your search
@@ -30,10 +30,16 @@ COMMAND_TREE = {
|
||||
"rule-create": "新建审核规则 → ekb agent rule-create --help",
|
||||
"logs": "查看审核日志",
|
||||
},
|
||||
"serve": {
|
||||
"_help": "启动 HTTP API Server",
|
||||
"--host": "监听地址(默认 127.0.0.1)",
|
||||
"--port": "监听端口(默认 9528)",
|
||||
"--reload": "开发模式自动重载",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def cmd_commands(group: Optional[str] = typer.Argument(None, help="命令组名称,如 auth / agent")):
|
||||
def cmd_commands(group: Optional[str] = typer.Argument(None, help="命令组名称,如 auth / agent / serve")):
|
||||
"""查看命令树(不带参数显示全树,带组名只看该分支)"""
|
||||
if group is not None:
|
||||
if group not in COMMAND_TREE:
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
"""ekb serve:启动 HTTP API Server
|
||||
|
||||
将 ekb-cli 的功能以 HTTP API 形式暴露,
|
||||
供其他服务、AI Agent 或脚本调用,返回结构化 JSON 数据。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
|
||||
def cmd_serve(
|
||||
host: str = typer.Option("127.0.0.1", "--host", "-h", help="监听地址"),
|
||||
port: int = typer.Option(9528, "--port", "-p", help="监听端口"),
|
||||
reload: bool = typer.Option(False, "--reload", help="开发模式:文件变更时自动重载"),
|
||||
):
|
||||
"""启动 HTTP API Server(FastAPI + uvicorn)
|
||||
|
||||
启动后可访问:
|
||||
\b
|
||||
- API 文档:http://<host>:<port>/docs
|
||||
- 所有接口与 CLI 命令一一对应,返回结构化 JSON
|
||||
"""
|
||||
try:
|
||||
import uvicorn
|
||||
except ImportError:
|
||||
rprint("[red]缺少 uvicorn,请执行:uv add uvicorn[standard][/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
from ekb_cli.server import create_app
|
||||
|
||||
rprint(f"[bold green]ekb-cli API Server[/bold green]")
|
||||
rprint(f" 地址:http://{host}:{port}")
|
||||
rprint(f" 文档:[link=http://{host}:{port}/docs]http://{host}:{port}/docs[/link]")
|
||||
rprint(f" 模式:{'[yellow]开发(--reload)[/yellow]' if reload else '生产'}")
|
||||
rprint("")
|
||||
|
||||
app = create_app()
|
||||
uvicorn.run(app, host=host, port=port, reload=reload)
|
||||
Reference in new issue
Block a user