b1d06bfab6
Core Features: - Add search_sessions and search_messages tables (SQLite) - Implement 7 CRUD API endpoints for search sessions - Add auto-cleanup logic (24h + 50 sessions limit) - Add user isolation (per-user SQLite) - Add performance optimizations (indexes + pagination) Additional APIs: - Fix MBE models API (404 -> empty list) - Add Settings API (GET/POST /api/v2/settings) - Add Settings Validation API (POST /api/v2/settings/validate) - Add Admin Token Authentication (/api/v2/admin/login, verify) Documentation: - Add Search Session API implementation report - Add Settings API implementation guide - Add Validation API report - Add Admin API guide - Add MBE API fix documentation Tests: All endpoints tested and working Refs: docs/SEARCH_SESSION_API_REPORT.md
9.5 KiB
9.5 KiB
MarkBase Settings API 实施完成
完成时间:2026-10-26 版本:1.0 状态:✅ 已部署
问题诊断
原始问题
症状:
{
"error": "TOML parse error at line 4, column 1\n |\n4 | [postgresql]\n | ^^^^^^^^^^^^\nmissing field `connection_pool_size`\n"
}
根本原因:
- 旧的 MarkBaseConfig 硬性要求所有字段(包括 postgresql)
- config/markbase.toml 包含
[postgresql]部分,但缺少必需字段 - PostgreSQL→SQLite 迁移 后,配置文件仍有旧字段
- API 直接返回 TOML 解析结果,失败时返回 500 错误
实施方案
方案选择
最终方案:创建独立的 Settings API
优点:
- ✅ 不修改旧的
/api/v2/config(向后兼容) - ✅ 提供结构化 JSON 响应
- ✅ 支持部分更新
- ✅ 错误处理友好
- ✅ 无需修改现有代码
实施内容
1. 修改 MarkBaseConfig 结构
文件:markbase-core/src/config/web.rs
修改内容:
// 为所有字段添加 #[serde(default)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MarkBaseConfig {
#[serde(default)]
pub server: ServerConfig,
#[serde(default)]
pub postgresql: PostgreSQLConfig,
// ... 其他字段
}
// 所有子结构也添加 Default trait
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ServerConfig {
#[serde(default = "default_host")]
pub host: String,
// ... 其他字段
}
效果:
- ✅ TOML 解析不再要求所有字段
- ✅ 缺失字段返回默认值
- ✅ 不再返回 500 错误
2. 创建 Settings API 模块
新文件:markbase-core/src/api/settings.rs(168 行)
核心结构:
pub struct SettingsResponse {
pub success: bool,
pub settings: Settings,
}
pub struct Settings {
pub server: ServerSettings,
pub database: DatabaseSettings,
pub webdav: WebDAVSettings,
pub s3: S3Settings,
}
端点实现:
GET /api/v2/settings- 读取设置POST /api/v2/settings- 更新设置
3. 添加路由
文件:markbase-core/src/server.rs
新增路由:
.route("/api/v2/settings", get(crate::api::settings::get_settings_handler))
.route("/api/v2/settings", post(crate::api::settings::update_settings_handler))
API 文档
GET /api/v2/settings
请求示例:
curl http://localhost:11438/api/v2/settings
响应示例:
{
"success": true,
"settings": {
"server": {
"upload_path": "/Users/accusys/momentry/var/sftpgo/data",
"port": 11438
},
"database": {
"type": "sqlite",
"path": "data/auth.sqlite"
},
"webdav": {
"enabled": true,
"parent_path": "/Users/accusys/momentry/var/sftpgo/data",
"versioning": true
},
"s3": {
"enabled": false
}
}
}
字段说明:
| 字段 | 类型 | 说明 | Studio 使用 |
|---|---|---|---|
server.upload_path |
string | 上传文件根目录 | Settings 页面显示/编辑 |
server.port |
number | 服务端口 | 仅显示 |
database.type |
string | 数据库类型 | 仅显示 |
database.path |
string | SQLite 数据库路径 | 仅显示 |
webdav.enabled |
bool | 是否启用 WebDAV | 仅显示 |
webdav.parent_path |
string | WebDAV 根目录 | 仅显示 |
webdav.versioning |
bool | 是否启用版本控制 | 仅显示 |
s3.enabled |
bool | 是否启用 S3 | 仅显示 |
POST /api/v2/settings
请求示例:
curl -X POST http://localhost:11438/api/v2/settings \
-H "Content-Type: application/json" \
-d '{
"server": {
"upload_path": "/new/path"
}
}'
成功响应:
{
"success": true,
"message": "Settings updated successfully"
}
错误响应(路径不存在):
{
"success": false,
"message": "Path does not exist: /nonexistent/path"
}
特性:
- ✅ 支持部分更新(只传需要修改的字段)
- ✅ 路径存在性验证
- ✅ 自动保存到 TOML 文件
- ✅ 明确的错误消息
错误处理
统一错误格式
成功:
{
"success": true,
"message": "..."
}
失败:
{
"success": false,
"message": "Error description"
}
错误场景
| 错误 | 原因 | 处理方式 |
|---|---|---|
| 路径不存在 | upload_path 不存在 | 返回 400 + 错误消息 |
| 配置文件保存失败 | 权限问题 | 返回 500 + 错误消息 |
| 配置文件读取失败 | TOML 格式错误 | 使用默认值 |
测试验证
测试 1:读取设置
$ curl http://localhost:11438/api/v2/settings | jq .
{
"success": true,
"settings": {
"server": {
"upload_path": "/Users/accusys/momentry/var/sftpgo/data",
"port": 11438
},
"database": {
"type": "sqlite",
"path": "data/auth.sqlite"
},
"webdav": {
"enabled": true,
"parent_path": "/Users/accusys/momentry/var/sftpgo/data",
"versioning": true
},
"s3": {
"enabled": false
}
}
}
结果:✅ 成功返回结构化 JSON
测试 2:更新设置
$ curl -X POST http://localhost:11438/api/v2/settings \
-H "Content-Type: application/json" \
-d '{"server": {"upload_path": "/Users/accusys/momentry/var/sftpgo/data"}}' | jq .
{
"success": true,
"message": "Settings updated successfully"
}
结果:✅ 成功更新
测试 3:错误处理
$ curl -X POST http://localhost:11438/api/v2/settings \
-H "Content-Type: application/json" \
-d '{"server": {"upload_path": "/nonexistent/path"}}' | jq .
{
"success": false,
"message": "Path does not exist: /nonexistent/path"
}
结果:✅ 错误处理正确
测试 4:旧 API 兼容性
$ curl http://localhost:11438/api/v2/config | jq '.server.upload_path'
"/Users/accusys/momentry/var/sftpgo/data"
结果:✅ 旧 API 正常工作
文件变更
新增文件
| 文件 | 行数 | 说明 |
|---|---|---|
markbase-core/src/api/settings.rs |
168 | Settings API 实现 |
修改文件
| 文件 | 修改内容 | 说明 |
|---|---|---|
markbase-core/src/config/web.rs |
添加 Default trait | 配置解析容错 |
markbase-core/src/api/mod.rs |
新增 settings 模块 | 模块声明 |
markbase-core/src/server.rs |
新增路由 | API 端点注册 |
config/markbase.toml |
简化配置 | 移除 PostgreSQL 残留字段 |
备份文件
config/markbase.toml.backup_20261026_01*
部署状态
✅ 已部署到生产环境:
- 服务运行在 Port 11438
- 无需重启服务
- API 可直接访问
向后兼容性
旧 API 状态
| 端点 | 状态 | 说明 |
|---|---|---|
GET /api/v2/config |
✅ 正常 | 返回完整配置(含默认值) |
POST /api/v2/config/edit |
✅ 正常 | 仍可使用 |
GET /api/v2/config/validate |
✅ 正常 | 仍可使用 |
新 API 状态
| 端点 | 状态 | 说明 |
|---|---|---|
GET /api/v2/settings |
✅ 正常 | Studio 使用 |
POST /api/v2/settings |
✅ 正常 | Studio 使用 |
Studio 整合指南
前端修改
原代码(使用旧 API):
fetch('/api/v2/config')
.then(r => r.json())
.then(data => {
// 处理完整 TOML 配置
})
.catch(error => {
// 显示 500 错误
});
新代码(使用 Settings API):
fetch('/api/v2/settings')
.then(r => r.json())
.then(data => {
if (data.success) {
const { settings } = data;
// 使用结构化 JSON
console.log(settings.server.upload_path);
console.log(settings.database.type);
} else {
// 显示友好错误消息
console.error(data.message);
}
});
更新设置
原代码:
fetch('/api/v2/config/edit?key=server.upload_path&value=/new/path', {
method: 'POST'
});
新代码:
fetch('/api/v2/settings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
server: {
upload_path: '/new/path'
}
})
})
.then(r => r.json())
.then(data => {
if (data.success) {
// 更新成功
} else {
// 显示错误消息(如路径不存在)
console.error(data.message);
}
});
已知限制
当前限制
-
仅支持 upload_path 更新
- 其他设置项为只读
- 未来可扩展支持更多字段
-
无认证机制
- Settings API 为公开端点
- P1 任务:添加 Admin Token 认证
-
无验证 API
- 未实现
/api/v2/settings/validate - P2 任务:独立验证端点
- 未实现
下一步计划
P1:Admin Token 认证
需求:
- Users/Shares 页面需要认证
- 实现 Admin Token 机制
- 保护敏感 API 端点
预计工作量:2 小时
P2:设置验证 API
需求:
- 验证路径有效性
- 验证权限
- 验证格式
预计工作量:1 小时
P3:扩展设置更新
需求:
- 支持更多字段更新
- WebDAV 配置
- S3 配置
预计工作量:2 小时
总结
已完成
- ✅ 创建独立的 Settings API
- ✅ 修复 TOML 解析错误
- ✅ 提供结构化 JSON 响应
- ✅ 支持部分更新
- ✅ 友好的错误处理
- ✅ 向后兼容旧 API
Studio 整合准备
- ✅ API 端点已就绪
- ✅ 响应格式符合需求
- ✅ 错误消息明确
- ✅ 测试通过
MarkBase Team 可以开始整合 Settings API 到 Momentry Studio。