mirror of
https://github.com/actix/examples
synced 2025-06-28 09:50:36 +02:00
upgrade to 2.0 alpha.3
This commit is contained in:
@ -1,18 +1,18 @@
|
||||
[package]
|
||||
name = "awc_examples"
|
||||
version = "0.1.0"
|
||||
version = "2.0.0"
|
||||
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||
edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "0.2.2"
|
||||
actix-web = { version="1.0.0", features=["ssl"] }
|
||||
actix-rt = "1.0.0-alpha.3"
|
||||
actix-web = { version="2.0.0-alpha.3", features=["openssl"] }
|
||||
|
||||
futures = "0.1"
|
||||
futures = "0.3.1"
|
||||
serde = "1.0.43"
|
||||
serde_derive = "1.0.43"
|
||||
serde_json = "1.0.16"
|
||||
validator = "0.6.3"
|
||||
validator_derive = "0.6.5"
|
||||
env_logger = "0.5.9"
|
||||
env_logger = "0.6"
|
||||
|
@ -26,7 +26,7 @@ use actix_web::{
|
||||
web::{self, BytesMut},
|
||||
App, Error, HttpResponse, HttpServer,
|
||||
};
|
||||
use futures::{Future, Stream};
|
||||
use futures::StreamExt;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Debug, Validate, Deserialize, Serialize)]
|
||||
@ -50,56 +50,51 @@ struct HttpBinResponse {
|
||||
}
|
||||
|
||||
/// validate data, post json to httpbin, get it back in the response body, return deserialized
|
||||
fn step_x(
|
||||
data: SomeData,
|
||||
client: &Client,
|
||||
) -> impl Future<Item = SomeData, Error = Error> {
|
||||
let validation = futures::future::result(data.validate()).map_err(ErrorBadRequest);
|
||||
let post_response = client
|
||||
async fn step_x(data: SomeData, client: &Client) -> Result<SomeData, Error> {
|
||||
// validate data
|
||||
data.validate().map_err(ErrorBadRequest)?;
|
||||
|
||||
let mut res = client
|
||||
.post("https://httpbin.org/post")
|
||||
.send_json(&data)
|
||||
.map_err(Error::from) // <- convert SendRequestError to an Error
|
||||
.and_then(|resp| {
|
||||
resp.from_err()
|
||||
.fold(BytesMut::new(), |mut acc, chunk| {
|
||||
acc.extend_from_slice(&chunk);
|
||||
Ok::<_, Error>(acc)
|
||||
})
|
||||
.map(|body| {
|
||||
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
||||
body.json
|
||||
})
|
||||
});
|
||||
.await
|
||||
.map_err(Error::from)?; // <- convert SendRequestError to an Error
|
||||
|
||||
validation.and_then(|_| post_response)
|
||||
let mut body = BytesMut::new();
|
||||
while let Some(chunk) = res.next().await {
|
||||
body.extend_from_slice(&chunk?);
|
||||
}
|
||||
|
||||
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
||||
Ok(body.json)
|
||||
}
|
||||
|
||||
fn create_something(
|
||||
async fn create_something(
|
||||
some_data: web::Json<SomeData>,
|
||||
client: web::Data<Client>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
step_x(some_data.into_inner(), &client).and_then(move |some_data_2| {
|
||||
step_x(some_data_2, &client).and_then(move |some_data_3| {
|
||||
step_x(some_data_3, &client).and_then(|d| {
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(serde_json::to_string(&d).unwrap()))
|
||||
})
|
||||
})
|
||||
})
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let some_data_2 = step_x(some_data.into_inner(), &client).await?;
|
||||
let some_data_3 = step_x(some_data_2, &client).await?;
|
||||
let d = step_x(some_data_3, &client).await?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(serde_json::to_string(&d).unwrap()))
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
let endpoint = "127.0.0.1:8080";
|
||||
|
||||
println!("Starting server at: {:?}", endpoint);
|
||||
HttpServer::new(|| {
|
||||
App::new().data(Client::default()).service(
|
||||
web::resource("/something").route(web::post().to_async(create_something)),
|
||||
)
|
||||
App::new()
|
||||
.data(Client::default())
|
||||
.service(web::resource("/something").route(web::post().to(create_something)))
|
||||
})
|
||||
.bind(endpoint)?
|
||||
.run()
|
||||
.start()
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user