- Introduced session management using Redis for tracking active sessions. - Added session claiming and releasing functionality in the completion manager. - Enhanced HTTP and WebSocket completion endpoints to support multiple languages. - Implemented request timeout and maximum body size configurations for API routes. - Updated client-side code to handle session IDs and language parameters in completion requests. - Improved error handling for unsupported languages and session conflicts. - Added tests for the completion manager to ensure proper session handling and cleanup.
119 lines
3.0 KiB
Markdown
119 lines
3.0 KiB
Markdown
# Monica LSP Gateway
|
||
|
||
面向微服务场景的 LSP 网关(Gin),当前支持:
|
||
- Go: `gopls`
|
||
- JavaScript / TypeScript: `typescript-language-server --stdio`
|
||
|
||
网关职责:
|
||
- 对外提供 HTTP + WebSocket 接口
|
||
- 对内通过 JSON-RPC / LSP over stdio 驱动语言服务器
|
||
- 按 `language + sessionId` 维护长生命周期会话
|
||
- 会话空闲自动回收(TTL)
|
||
|
||
## 运行
|
||
|
||
```bash
|
||
go mod tidy
|
||
go run ./cmd/server
|
||
```
|
||
|
||
默认地址:`http://127.0.0.1:8080`
|
||
|
||
## 企业化配置(环境变量)
|
||
|
||
- `PORT`:默认 `8080`
|
||
- `WORKSPACE_DIR`:LSP 工作目录,默认当前目录
|
||
- `CORS_ALLOW_ORIGIN`:默认 `*`
|
||
- `LSP_API_TOKEN`:可选,设置后需在请求头传 `X-API-Key`
|
||
- `REQUEST_TIMEOUT`:单请求超时,默认 `10s`
|
||
- `MAX_BODY_BYTES`:请求体上限,默认 `2097152`(2MB)
|
||
- `SESSION_TTL`:会话空闲回收时间,默认 `20m`
|
||
- `SESSION_CLEANUP_INTERVAL`:清理周期,默认 `2m`
|
||
- `MAX_SESSIONS`:最大会话数,默认 `256`
|
||
- `ENABLE_REDIS_STICKY`:是否启用 Redis 会话外置与粘性路由,默认 `true`
|
||
- `REDIS_ADDR`:默认 `10.0.0.10:6379`
|
||
- `REDIS_DB`:默认 `1`
|
||
- `REDIS_PASSWORD`:默认空
|
||
- `REDIS_KEY_PREFIX`:默认 `lsp-gateway`
|
||
- `INSTANCE_ID`:实例唯一标识(建议由部署系统注入)
|
||
- `INSTANCE_URL`:实例可回源地址(用于路由提示),默认 `http://127.0.0.1:${PORT}`
|
||
- `INSTANCE_TTL`:实例注册 TTL,默认 `30s`
|
||
- `INSTANCE_HEARTBEAT_INTERVAL`:实例心跳周期,默认 `10s`
|
||
|
||
语言服务器命令(可替换为企业内部镜像/封装):
|
||
- `GO_LSP_COMMAND`,`GO_LSP_ARGS`
|
||
- `JAVASCRIPT_LSP_COMMAND`,`JAVASCRIPT_LSP_ARGS`
|
||
- `TYPESCRIPT_LSP_COMMAND`,`TYPESCRIPT_LSP_ARGS`
|
||
|
||
默认 JS/TS 命令:
|
||
- `typescript-language-server --stdio`
|
||
|
||
## 健康检查
|
||
|
||
- `GET /health`
|
||
- `GET /health/live`
|
||
- `GET /health/ready`(含当前会话统计)
|
||
|
||
## HTTP 补全接口
|
||
|
||
- `POST /api/v1/completions/:language`
|
||
|
||
示例(JavaScript):
|
||
|
||
```json
|
||
{
|
||
"sessionId": "editor-1",
|
||
"language": "javascript",
|
||
"uri": "file:///main.js",
|
||
"text": "const a = console.lo",
|
||
"line": 0,
|
||
"character": 20
|
||
}
|
||
```
|
||
|
||
## WebSocket 接口
|
||
|
||
- `GET /ws/completions`
|
||
- `GET /ws/completions/:language`
|
||
|
||
支持两种消息格式:
|
||
|
||
1) 简化消息:
|
||
|
||
```json
|
||
{
|
||
"id": "1",
|
||
"sessionId": "editor-1",
|
||
"language": "go",
|
||
"uri": "file:///main.go",
|
||
"text": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Pri\n}",
|
||
"line": 5,
|
||
"character": 9
|
||
}
|
||
```
|
||
|
||
2) JSON-RPC 风格:
|
||
|
||
```json
|
||
{
|
||
"jsonrpc": "2.0",
|
||
"id": 1,
|
||
"method": "completion/complete",
|
||
"params": {
|
||
"sessionId": "editor-1",
|
||
"language": "typescript",
|
||
"uri": "file:///main.ts",
|
||
"text": "console.lo",
|
||
"line": 0,
|
||
"character": 10
|
||
}
|
||
}
|
||
```
|
||
|
||
## 备注
|
||
|
||
- `line` / `character` 为 0-based。
|
||
- `uri` 支持 `file:///main.go` 这类相对根路径,网关会映射到 `WORKSPACE_DIR` 下。
|
||
- 前端建议稳定传 `sessionId`,避免频繁新建语言服务器进程。
|
||
- 多实例场景下,如果请求落到非会话所属实例,服务会返回 `409` 并携带 `routeTo` 与 `X-LSP-Route-To`。
|