异步Web框架生态
Rocket框架
官方教程:The Rocket Programming Guide
Rocket设计核心哲学:
- Security, correctness, and developer experience are paramount.
- All request handling information should be typed and self-contained.
- Decisions should not be forced.
Lifecycle
Rocket’s main task is to listen for incoming web requests, dispatch the request to the application code, and return a response to the client. We call the process that goes from request to response the “lifecycle”. We summarize the lifecycle as the following sequence of steps:
- Routing 路由
- Validation 验证
- Processing 处理
- Response 回复
|
|
Lanuching
Rocket begins serving requests after being launched, which starts a multi-threaded asynchronous server and dispatches requests to matching routes as they arrive.
|
|
Dynamic Paths
|
|
Forwarding
Routes are attempted in increasing rank order. Rocket chooses a default ranking from -12 to -1, detailed in the next section, but a route’s rank can also be manually set with the rank attribute. To illustrate, consider the following routes:
|
|
actix-web
官方文档:actix_web
可以通过宏来创建提取器。
|
|
其他
- Lemmy论坛
- actix-extras:actix中间件
actix-web底层基于actix-net中间库,封装了和网络相关的东西。
在actix-net/actix-rt/src/arbiter.rs下每个arbiter对应一个线程。
gotham
比较早的框架,基于tokio实现。
Thruster
构建于hyper之上,这个中间件在请求过程中设置上下文。
tower
文档: tower
Tower is a library of modular and reusable components for building robust networking clients and servers.
hyper
官方文档:hyper
关于路由用到route-recognizer,它的底层是非确定有限状态自动机。
warp
github仓库地址:warp。
filter的工作机制,基于hyper。Rust-Warp-Example是一个应用实例。
一些底层的库
- http:解析处理HTTP协议,对http请求和响应做了一个类型抽象。
- http-body:做异步http请求和响应。
- http-types:基于async-std。
- tower-http:基于tower、http、http-body、的中间件。
- hyper:HTTP的实现,没有整合tower-http。
Rust异步Web框架
结构:
- 框架接口设计
- 路由结构实现
- 实现Handler
- 添加tracing打印日志
- 实现提取器
- 实现中间件
- 错误处理
补充学习
- 正则表达式
- Cow
- and_then、and
- dyn
- pin、unpin
- Oneshot
- pin_project!
- 网络中的请求方法有哪些?区别是什么?
- Any
- 终端命令 curl,参考Linux curl命令详解
- DSL是什么?参考谈谈 DSL 以及 DSL 的应用(以 CocoaPods 为例)
- uri
- async_trait
- map、map_err
curl http://... -I -HEAD
- tower
- 阅读axum源码
- kv-serve 和 web 如何连接在一起?