mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Extractor configuration Migration (#937)
added guide for Extractor configuration in MIGRATION.md
This commit is contained in:
parent
1a24ff8717
commit
b948f74b54
58
MIGRATION.md
58
MIGRATION.md
@ -31,6 +31,64 @@
|
|||||||
|
|
||||||
## 1.0.0
|
## 1.0.0
|
||||||
|
|
||||||
|
* Extractor configuration. In version 1.0 this is handled with the new `Data` mechanism for both setting and retrieving the configuration
|
||||||
|
|
||||||
|
instead of
|
||||||
|
|
||||||
|
```rust
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ExtractorConfig {
|
||||||
|
config: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRequest for YourExtractor {
|
||||||
|
type Config = ExtractorConfig;
|
||||||
|
type Result = Result<YourExtractor, Error>;
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result {
|
||||||
|
println!("use the config: {:?}", cfg.config);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
App::new().resource("/route_with_config", |r| {
|
||||||
|
r.post().with_config(handler_fn, |cfg| {
|
||||||
|
cfg.0.config = "test".to_string();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
use the HttpRequest to get the configuration like any other `Data` with `req.app_data::<C>()` and set it with the `data()` method on the `resource`
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ExtractorConfig {
|
||||||
|
config: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRequest for YourExtractor {
|
||||||
|
type Error = Error;
|
||||||
|
type Future = Result<Self, Self::Error>;
|
||||||
|
type Config = ExtractorConfig;
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
|
let cfg = req.app_data::<ExtractorConfig>();
|
||||||
|
println!("config data?: {:?}", cfg.unwrap().role);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
App::new().service(
|
||||||
|
resource("/route_with_config")
|
||||||
|
.data(ExtractorConfig {
|
||||||
|
config: "test".to_string(),
|
||||||
|
})
|
||||||
|
.route(post().to(handler_fn)),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
* Resource registration. 1.0 version uses generalized resource
|
* Resource registration. 1.0 version uses generalized resource
|
||||||
registration via `.service()` method.
|
registration via `.service()` method.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user