# rhttpd - Rust HTTP Server 一个高性能、可配置的HTTP服务器,用Rust编写,支持多站点托管、多种代理类型和JavaScript动态配置。 ## 功能特性 ### ✅ 已实现 - **多站点支持** - 在单个端口上服务多个独立站点 - **基于Host头的路由** - 根据HTTP Host头部进行站点路由 - **静态文件服务** - 支持MIME类型自动识别和索引文件 - **反向代理** - 代理到后端HTTP服务 - **配置系统** - 支持TOML和JSON格式 - **日志记录** - 使用tracing框架 ### 🚧 开发中 - TCP代理 - 连接池和超时控制 - JavaScript配置引擎 ### 📋 计划中 - 正向代理 - SSL/TLS支持 - 负载均衡 - WebSocket支持 ## 快速开始 ### 安装 ```bash git clone https://github.com/yourusername/rhttpd.git cd rhttpd cargo build --release ``` ### 配置 创建配置文件 `config.toml`: ```toml port = 8080 [sites] [sites."example.com"] hostname = "example.com" [[sites."example.com".routes]] type = "static" path_pattern = "/*" root = "./public" index = ["index.html"] [[sites."example.com".routes]] type = "reverse_proxy" path_pattern = "/api/*" target = "http://localhost:3000" ``` ### 运行 ```bash # 使用默认配置 cargo run # 使用指定配置文件 cargo run -- config.toml ``` ## 配置参考 ### 服务器配置 | 字段 | 类型 | 描述 | |------|------|------| | `port` | `u16` | 监听端口 | | `sites` | `HashMap` | 站点配置映射 | | `js_config` | `Option` | JavaScript配置文件路径 | ### 站点配置 | 字段 | 类型 | 描述 | |------|------|------| | `hostname` | `String` | 站点主机名 | | `routes` | `Vec` | 路由规则列表 | | `tls` | `Option` | TLS配置 | ### 路由规则 #### 静态文件 ```toml type = "static" path_pattern = "/*" root = "./public" index = ["index.html", "index.htm"] directory_listing = false ``` #### 反向代理 ```toml type = "reverse_proxy" path_pattern = "/api/*" target = "http://backend:3000" ``` #### TCP代理 ```toml type = "tcp_proxy" path_pattern = "/ws/*" target = "ws://chat-server:8080" protocol = "websocket" ``` ## 开发 ### 构建和测试 ```bash # 构建 cargo build # 测试 cargo test # 运行单个测试 cargo test test_name # 代码检查 cargo clippy # 代码格式化 cargo fmt # 文档生成 cargo doc --open ``` ### 项目结构 ``` rhttpd/ ├── src/ │ ├── main.rs # 应用程序入口 │ ├── lib.rs # 库根 │ ├── config/ # 配置管理 │ ├── server/ # 服务器实现 │ ├── proxy/ # 代理功能 │ └── js_engine/ # JavaScript集成 ├── tests/ # 集成测试 ├── doc/ # 文档 ├── public/ # 静态文件示例 ├── static/ # 静态文件示例 ├── config.toml # 配置示例 └── AGENTS.md # 开发者指南 ``` ## 示例 ### 基本静态网站 ```toml port = 8080 [sites."mysite.com"] hostname = "mysite.com" [[sites."mysite.com".routes]] type = "static" path_pattern = "/*" root = "./www" index = ["index.html"] ``` ### API服务器代理 ```toml [sites."api.example.com"] hostname = "api.example.com" [[sites."api.example.com".routes]] type = "reverse_proxy" path_pattern = "/*" target = "http://localhost:3001" ``` ### 混合配置 ```toml [sites."example.com"] [[sites."example.com".routes]] type = "static" path_pattern = "/static/*" root = "./assets" [[sites."example.com".routes]] type = "static" path_pattern = "/" root = "./public" index = ["index.html"] [[sites."example.com".routes]] type = "reverse_proxy" path_pattern = "/api/*" target = "http://backend:3000" ``` ## 贡献 欢迎贡献代码!请查看 [AGENTS.md](AGENTS.md) 了解开发指南。 ## 许可证 MIT License ## 性能 rhttpd基于以下高性能Rust库构建: - `tokio` - 异步运行时 - `axum` - HTTP框架 - `hyper` - HTTP实现 - `reqwest` - HTTP客户端 ## 支持 如有问题或建议,请提交Issue或Pull Request。