2个不稳定版本
0.1.0+1 | 2023年3月16日 |
---|
#326 在 WebSocket
27KB
498 行
Sailboat
问题
Rust HTTP服务器框架类别似乎过度使用宏来生成本质上是Rust子语言的框架结构。
解决方案
Sailboat专注于简洁性,同时保持速度。这个库避免使用复杂、不清晰或不必要的宏,而是更喜欢编写(有时)更冗长但更清晰的代码。
清单
任务顺序不一定按完成顺序排列
-
基本路由
-
URL参数
-
多线程响应
-
全局资源/上下文
-
WebSocket
-
转为异步
-
Ssl/Https
-
优化
-
自定义Http实现
示例
使用 cargo run --example example_name
运行示例
使用sailboat的基本Hello World程序
use sailboat::{
application::Application,
executor::DefaultExecutor,
request::Request,
response::Response,
service::{Command, Point},
StatusCode,
};
type Data = ();
async fn hello_world<'a>(_req: &mut Request<'a>, _ctx: &Data) -> Command<Data> {
// Responding with `None` will act as a middleware System
// Responding with `Some` will respond to the request object and move on to the next request
// All systems registered after receiving a `Some` will not be run
Command::Respond(Response::empty(StatusCode(200)))
}
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let root = Point::root().fold(|s| {
// `localhost/hello_world`
s.push_child(Point::with_system(
"hello_world",
hello_world,
))
});
// The application will automatically respond to all unrecognized urls with a `StatusCode(404)` not found
// In this case, the only recognized url is `localhost/hello_world`
let app = Application::<DefaultExecutor>::new("0.0.0.0:8080", root, ())?;
// Initiate main application loop
let _ = app.run();
Ok(())
}
性能
在我的个人电脑上使用 wrk -t 4 -c 4
一个基本的hello_world示例揭示了以下信息
- sailboat: ~240k req/sec
- actix: ~280k req/sec
- tiny_http: ~280k req/sec
- rouille: ~80k req/sec
文档
这个crate旨在实现100%的全面文档。
许可
查看LICENSE-MIT
依赖
~2.4–3.5MB
~56K SLoC