mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
Merge pull request #108 from Dowwie/master
added async_ex2 to illustrate app-level configuration registration for asynchronous handlers
This commit is contained in:
commit
80793ccfbd
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,3 +13,5 @@ Cargo.lock
|
|||||||
|
|
||||||
# intellij files
|
# intellij files
|
||||||
.idea/**
|
.idea/**
|
||||||
|
|
||||||
|
.history/
|
||||||
|
@ -5,6 +5,7 @@ members = [
|
|||||||
"actix_todo",
|
"actix_todo",
|
||||||
"async_db",
|
"async_db",
|
||||||
"async_ex1",
|
"async_ex1",
|
||||||
|
"async_ex2",
|
||||||
"basics",
|
"basics",
|
||||||
"cookie-auth",
|
"cookie-auth",
|
||||||
"cookie-session",
|
"cookie-session",
|
||||||
|
@ -16,5 +16,4 @@ Actix-web features illustrated here include:
|
|||||||
|
|
||||||
|
|
||||||
Example query from the command line using httpie:
|
Example query from the command line using httpie:
|
||||||
```http post 127.0.0.1:8088/something id=1 name=JohnDoe```
|
```echo '{"id":"1", "name": "JohnDoe"}' | http 127.0.0.1:8080/something```
|
||||||
|
|
||||||
|
@ -11,9 +11,7 @@
|
|||||||
// 2. actix-web client features:
|
// 2. actix-web client features:
|
||||||
// - POSTing json body
|
// - POSTing json body
|
||||||
// 3. chaining futures into a single response used by an asynch endpoint
|
// 3. chaining futures into a single response used by an asynch endpoint
|
||||||
//
|
|
||||||
// There are 2 versions in this example, one that uses Boxed Futures and the
|
|
||||||
// other that uses Impl Future, available since rustc v1.26.
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate validator_derive;
|
extern crate validator_derive;
|
||||||
@ -49,60 +47,10 @@ struct HttpBinResponse {
|
|||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// v1 uses Boxed Futures, which were the only option prior to rustc v1.26
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// post json to httpbin, get it back in the response body, return deserialized
|
/// post json to httpbin, get it back in the response body, return deserialized
|
||||||
fn step_x_v1(
|
fn step_x(
|
||||||
data: SomeData,
|
|
||||||
client: &Client,
|
|
||||||
) -> Box<Future<Item = SomeData, Error = Error>> {
|
|
||||||
Box::new(
|
|
||||||
client
|
|
||||||
.post("https://httpbin.org/post")
|
|
||||||
.send_json(&data)
|
|
||||||
.map_err(Error::from) // <- convert SendRequestError to an Error
|
|
||||||
.and_then(|resp| {
|
|
||||||
resp // <- this is MessageBody type, resolves to complete body
|
|
||||||
.from_err() // <- convert PayloadError to an Error
|
|
||||||
.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
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_something_v1(
|
|
||||||
some_data: web::Json<SomeData>,
|
|
||||||
client: web::Data<Client>,
|
|
||||||
) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
|
||||||
Box::new(
|
|
||||||
step_x_v1(some_data.into_inner(), &client).and_then(move |some_data_2| {
|
|
||||||
step_x_v1(some_data_2, &client).and_then(move |some_data_3| {
|
|
||||||
step_x_v1(some_data_3, &client).and_then(|d| {
|
|
||||||
Ok(HttpResponse::Ok()
|
|
||||||
.content_type("application/json")
|
|
||||||
.body(serde_json::to_string(&d).unwrap())
|
|
||||||
.into())
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// v2 uses impl Future, available as of rustc v1.26
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
|
|
||||||
/// post json to httpbin, get it back in the response body, return deserialized
|
|
||||||
fn step_x_v2(
|
|
||||||
data: SomeData,
|
data: SomeData,
|
||||||
client: &Client,
|
client: &Client,
|
||||||
) -> impl Future<Item = SomeData, Error = Error> {
|
) -> impl Future<Item = SomeData, Error = Error> {
|
||||||
@ -123,17 +71,16 @@ fn step_x_v2(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_something_v2(
|
fn create_something(
|
||||||
some_data: web::Json<SomeData>,
|
some_data: web::Json<SomeData>,
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
step_x_v2(some_data.into_inner(), &client).and_then(move |some_data_2| {
|
step_x(some_data.into_inner(), &client).and_then(move |some_data_2| {
|
||||||
step_x_v2(some_data_2, &client).and_then(move |some_data_3| {
|
step_x(some_data_2, &client).and_then(move |some_data_3| {
|
||||||
step_x_v2(some_data_3, &client).and_then(|d| {
|
step_x(some_data_3, &client).and_then(|d| {
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("application/json")
|
.content_type("application/json")
|
||||||
.body(serde_json::to_string(&d).unwrap())
|
.body(serde_json::to_string(&d).unwrap()))
|
||||||
.into())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -142,19 +89,17 @@ fn create_something_v2(
|
|||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
let endpoint = "127.0.0.1:8080";
|
||||||
|
|
||||||
|
println!("Starting server at: {:?}", endpoint);
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.data(Client::default())
|
.data(Client::default())
|
||||||
.service(
|
.service(
|
||||||
web::resource("/something_v1")
|
web::resource("/something")
|
||||||
.route(web::post().to(create_something_v1)),
|
.route(web::post().to_async(create_something)),
|
||||||
)
|
|
||||||
.service(
|
|
||||||
web::resource("/something_v2")
|
|
||||||
.route(web::post().to_async(create_something_v2)),
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")?
|
.bind(endpoint)?
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
20
async_ex2/Cargo.toml
Normal file
20
async_ex2/Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_ex2"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-rt = "0.2.2"
|
||||||
|
actix-web = { version="1.0.0-alpha.4", features=["ssl"] }
|
||||||
|
actix-multipart = { git="https://github.com/actix/actix-web.git" }
|
||||||
|
bytes = "0.4.12"
|
||||||
|
env_logger = "0.6.1"
|
||||||
|
futures = "0.1"
|
||||||
|
serde = { version = "^1.0", features = ["derive"] }
|
||||||
|
serde_derive = "1.0.90"
|
||||||
|
serde_json = "1.0.39"
|
||||||
|
time = "0.1.42"
|
||||||
|
validator = "0.8.0"
|
||||||
|
validator_derive = "0.8.0"
|
3
async_ex2/src/README.md
Normal file
3
async_ex2/src/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
This example illustrates how to use nested resource registration through application-level configuration.
|
||||||
|
The endpoints do nothing.
|
||||||
|
|
47
async_ex2/src/appconfig.rs
Normal file
47
async_ex2/src/appconfig.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
use actix_web::{error, web};
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures::Stream;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
handlers::{
|
||||||
|
products,
|
||||||
|
parts
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
pub fn config_app<P>(cfg: &mut web::RouterConfig<P>)
|
||||||
|
where P: Stream<Item = Bytes, Error = error::PayloadError>
|
||||||
|
+ 'static
|
||||||
|
{
|
||||||
|
// domain includes: /products/{product_id}/parts/{part_id}
|
||||||
|
cfg.service(
|
||||||
|
web::scope("/products")
|
||||||
|
.service(
|
||||||
|
web::resource("")
|
||||||
|
.route(web::get().to_async(products::get_products))
|
||||||
|
.route(web::post().to_async(products::add_product))
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::scope("/{product_id}")
|
||||||
|
.service(
|
||||||
|
web::resource("")
|
||||||
|
.route(web::get().to_async(products::get_product_detail))
|
||||||
|
.route(web::delete().to_async(products::remove_product))
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::scope("/parts")
|
||||||
|
.service(
|
||||||
|
web::resource("")
|
||||||
|
.route(web::get().to_async(parts::get_parts))
|
||||||
|
.route(web::post().to_async(parts::add_part))
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{part_id}")
|
||||||
|
.route(web::get().to_async(parts::get_part_detail))
|
||||||
|
.route(web::delete().to_async(parts::remove_part))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
19
async_ex2/src/bin/main.rs
Normal file
19
async_ex2/src/bin/main.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use actix_web::{middleware, App, HttpServer};
|
||||||
|
|
||||||
|
use async_ex2::{
|
||||||
|
appconfig::config_app,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
HttpServer::new(||
|
||||||
|
App::new()
|
||||||
|
.configure(config_app)
|
||||||
|
.wrap(middleware::Logger::default())
|
||||||
|
)
|
||||||
|
.bind("127.0.0.1:8080")?
|
||||||
|
.run()
|
||||||
|
}
|
17
async_ex2/src/common.rs
Normal file
17
async_ex2/src/common.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Product {
|
||||||
|
id: Option<i64>,
|
||||||
|
product_type: Option<String>,
|
||||||
|
name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Part {
|
||||||
|
id: Option<i64>,
|
||||||
|
part_type: Option<String>,
|
||||||
|
name: Option<String>,
|
||||||
|
}
|
3
async_ex2/src/handlers/mod.rs
Normal file
3
async_ex2/src/handlers/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub mod products;
|
||||||
|
pub mod parts;
|
||||||
|
|
26
async_ex2/src/handlers/parts.rs
Normal file
26
async_ex2/src/handlers/parts.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use actix_multipart::{Field, Item, Multipart, MultipartError};
|
||||||
|
use actix_web::{HttpResponse, web, error, Error};
|
||||||
|
use futures::{future::{ok as fut_ok, err as fut_err, Either}, Future, Stream};
|
||||||
|
|
||||||
|
use crate::common::{Part, Product};
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_parts(query: web::Query<Option<Part>>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_part(new_part: web::Json<Product>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_part_detail(id: web::Path<String>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_part(id: web::Path<String>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
26
async_ex2/src/handlers/products.rs
Normal file
26
async_ex2/src/handlers/products.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use actix_multipart::{Field, Item, Multipart, MultipartError};
|
||||||
|
use actix_web::{HttpResponse, web, error, Error};
|
||||||
|
use futures::{future::{ok as fut_ok, err as fut_err, Either}, Future, Stream};
|
||||||
|
|
||||||
|
use crate::common::{Part, Product};
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_products(query: web::Query<Option<Part>>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_product(new_product: web::Json<Product>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_product_detail(id: web::Path<String>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_product(id: web::Path<String>)
|
||||||
|
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
|
fut_ok(HttpResponse::Ok().finish())
|
||||||
|
}
|
3
async_ex2/src/lib.rs
Normal file
3
async_ex2/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub mod appconfig;
|
||||||
|
pub mod common;
|
||||||
|
pub mod handlers;
|
Loading…
Reference in New Issue
Block a user