rhttp/examples/current_capabilities.md

130 lines
3.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# rhttpd JavaScript引擎 - 当前状态
## ✅ 已实现的功能
### 1. 基础架构
- **模块化结构**: 完整的js_engine模块结构
- **错误处理**: JsEngineError类型支持详细的错误信息
- **中间件类型定义**: 三个级别的中间件系统
### 2. JavaScript配置支持
- **配置文件加载**: 可以从JavaScript文件加载配置
- **配置解析**: 支持`export default { ... }`格式
- **错误处理**: 详细的配置错误信息
### 3. 中间件系统基础
- **中间件容器**: 支持全局、站点、路由三个级别
- **钩子类型**: 支持onRequest、onResponse、onResponseSent三个钩子
- **配置提取**: 可以从JavaScript配置文件中提取中间件函数定义
## 📁 文件结构
```
src/js_engine/
├── mod.rs # 主模块导出API
├── error.rs # 错误类型定义
└── types.rs # 类型定义(中间件容器、钩子等)
```
## 📋 当前API
### JsEngine类型
```rust
pub struct JsEngine {
middleware: Arc<MiddlewareContainer>,
}
impl JsEngine {
pub async fn new() -> Result<Self, JsEngineError>;
pub async fn load_config(&self, config_path: &str) -> Result<serde_json::Value, JsEngineError>;
pub async fn execute_middleware(...) -> Result<Option<serde_json::Value>, JsEngineError>;
pub fn middleware(&self) -> &Arc<MiddlewareContainer>;
pub fn has_middleware(&self) -> bool;
}
```
### 中间件类型
```rust
pub enum MiddlewareHook {
OnRequest(MiddlewareFunction),
OnResponse(MiddlewareFunction),
OnResponseSent(MiddlewareFunction),
}
pub struct MiddlewareContainer {
global: Vec<MiddlewareHook>,
site: HashMap<String, Vec<MiddlewareHook>>,
route: HashMap<String, Vec<MiddlewareHook>>,
}
```
## 🧪 示例配置
### JavaScript配置示例
```javascript
// test_config.js
export default {
port: 9090,
globalMiddleware: {
onRequest: async function(req) {
console.log('Global onRequest:', req.method, req.url);
}
},
sites: {
"test.local": {
hostname: "test.local",
middleware: {
onRequest: async function(req) {
if (req.path === '/protected') {
return {
status: 401,
body: JSON.stringify({ error: 'Unauthorized' })
};
}
}
}
}
}
};
```
### TOML配置引用
```toml
# config.toml
port = 8080
js_config = "test_config.js"
[sites."example.com"]
hostname = "example.com"
# ...
```
## 🔧 如何使用
1. **创建JavaScript配置文件**
2. **在TOML配置中引用**: `js_config = "path/to/config.js"`
3. **服务器会自动加载并解析**: 配置中的port和中间件信息会被识别
## 📊 兼容性
-**向后兼容**: 原有JSON和TOML配置继续工作
-**渐进增强**: 只在使用`js_config`时才启用JavaScript特性
-**错误处理**: 详细的错误信息和恢复机制
## 🚀 下一步计划
### 短期目标
1. **真正的JavaScript执行**: 集成rquickjs支持真正的JS代码执行
2. **完整中间件执行**: 实现MiddlewareExecutor执行三个级别的中间件链
3. **头修改支持**: 实现Request/Response对象的头修改API
### 长期目标
1. **性能优化**: 确保中间件执行< 5ms
2. **错误处理改进**: 根据策略立即返回500错误
3. **控制台支持**: 实现JavaScript的console.log支持
---
当前实现已经为完整的JavaScript引擎打下了坚实的基础提供了模块化的架构和清晰的API设计下一阶段将在此基础上添加真正的JavaScript执行能力