# HTTP API

> serve feature —— 浏览器 UI、/api/v1 下的 REST 端点、CORS 与访问日志。

---

LLMS 索引： [llms.txt](/codeweb/zh/llms.txt)

---

## 启动服务 {#start-the-server}

以 `serve` feature 构建后，`codeweb serve` 加载项目 store 并启动带内嵌浏览器 UI 的 HTTP 服务:

```bash
cargo build --release --features serve
codeweb serve --addr 127.0.0.1:3000 --open
```

| 参数 | 默认值 | 含义 |
| --- | --- | --- |
| `-p, --project` | `.` | 项目目录 |
| `-a, --addr` | `127.0.0.1:3000` | 绑定地址(host:port) |
| `--open` | 关闭 | 启动后自动打开浏览器 |
| `--log-level` | `info` | 访问日志级别:`info` 或 `debug` |

UI 提供带搜索的节点列表、Cytoscape.js 图画布(dagre 布局)和上下游详情面板。请求允许任意 CORS，并写入项目目录下的 `.codeweb/` 访问日志。

## REST 端点 {#rest-endpoints}

全部端点位于 `/api/v1` 下，除特别说明外返回 JSON。

| 方法 | 路径 | 参数 | 说明 |
| --- | --- | --- | --- |
| GET | `/api/v1/stats` | — | 按类型统计节点数、边数、项目名 |
| GET | `/api/v1/files` | — | 已分析文件及节点数 |
| GET | `/api/v1/nodes` | `search`、`node_type`、`orphan`、`low_degree`、`limit`(默认 100)、`offset`(默认 0) | 过滤并分页节点摘要;`search` 按匹配度排序 |
| GET | `/api/v1/nodes/:id` | — | 节点属性、直接上游、下游 |
| GET | `/api/v1/nodes/:id/callers` | `limit`、`offset`、`depth` | 上游邻居 |
| GET | `/api/v1/nodes/:id/callees` | `limit`、`offset`、`depth` | 下游邻居 |
| GET | `/api/v1/nodes/search-sql` | `q`(必填) | 按 SQL 内容搜索 `mapper`/`sql` 节点，带评分 |
| GET | `/api/v1/trace` | `from`(必填)、`depth`(默认 2，上限 10)、`max_nodes`(默认 500) | 双向调用链树;响应含 `truncated` 标记 |
| GET | `/api/v1/columns` | `procedure`、`package` 二选一;可选 `table` | 聚合列级分析(issue #165 JSON) |
| GET | `/api/v1/lineage` | `target`(必填)、`direction`(`upstream`/`downstream`/`both`)、`depth`(默认 5) | 表/列血缘 |
| POST | `/api/v1/query` | body:[QuerySpec JSON](/codeweb/zh/docs/trace-and-query/#declarative-queries) | 声明式遍历 |
| GET | `/api/v1/export` | `format`:`dot`、`json`、`ndjson`、`mermaid` | 下载图谱 |
| GET | `/api/v1/graph` | — | 浏览器 UI 画布使用的完整图数据 |

状态码约定:名称无匹配返回 `404`;名称歧义或参数非法返回 `400`(例如同时传 `procedure` 和 `package` 给 `/api/v1/columns`)。

## 示例 {#examples}

```bash
codeweb serve -p my-project --addr 127.0.0.1:3000 &

curl -s http://127.0.0.1:3000/api/v1/stats
curl -s "http://127.0.0.1:3000/api/v1/nodes?search=order&node_type=proc&limit=10"
curl -s "http://127.0.0.1:3000/api/v1/trace?from=process_order&depth=3"
curl -s "http://127.0.0.1:3000/api/v1/lineage?target=orders&direction=upstream"
curl -s "http://127.0.0.1:3000/api/v1/nodes/search-sql?q=UPDATE%20inventory"
curl -s -X POST http://127.0.0.1:3000/api/v1/query \
  -H 'Content-Type: application/json' \
  -d '{"start":{"type":"proc","name":"order"},"collect":"paths"}'
curl -s "http://127.0.0.1:3000/api/v1/export?format=ndjson" -o graph.ndjson
```

## 安全边界 {#security-boundaries}

服务读取项目 store 并绑定你指定的地址。它不对请求做鉴权，CORS 完全放开，因此定位是本地开发工具:不要暴露给不可信网络。读取路径可指向 `analysis.paths` 下的任意位置;serve 模式没有任何写入端点。

---

反链：

- [CLI 参考](/codeweb/zh/docs/cli-reference/)
- [快速上手](/codeweb/zh/docs/getting-started/)
