重构第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:
gitea committed 2026-04-14 11:29:58 +08:00
1 parent db378b9140
commit 9ec009f2cb
4 files changed
+171 -26

No files matched your search

+15 -2
View File
@@ -1,9 +1,13 @@
"""ekb-cli:快报命令行工具"""
"""ekb-cli:快报命令行工具
入口:注册所有命令组,统一捕获 EkbError 异常。
"""
import typer
from ekb_cli.commands import auth
from ekb_cli.commands.agent import app as agent_app
from ekb_cli.commands.help import cmd_commands
from ekb_cli.commands.serve import cmd_serve
app = typer.Typer(
name="ekb",
@@ -14,7 +18,16 @@ app = typer.Typer(
app.add_typer(auth.app, name="auth")
app.add_typer(agent_app, name="agent")
app.command("commands")(cmd_commands)
app.command("serve")(cmd_serve)
def main():
app()
"""CLI 入口:统一捕获 EkbError,输出友好错误信息"""
from ekb_cli.errors import EkbError
from rich import print as rprint
try:
app()
except EkbError as e:
rprint(f"[red]{e.message}[/red]")
raise SystemExit(1)
+7 -1
View File
@@ -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:
+38
View File
@@ -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)