1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

update to beta.1

This commit is contained in:
Nikolay Kim 2019-04-21 09:36:31 -07:00
parent 0faa540b8d
commit 52811611d0
4 changed files with 24 additions and 11 deletions

View File

@ -8,7 +8,7 @@ This project illustrates two examples:
2. An asynchronous handler that executes 4 queries in *parallel*, 2. An asynchronous handler that executes 4 queries in *parallel*,
collecting the results and returning them as a single serialized json object collecting the results and returning them as a single serialized json object
Note: The use of sleep(Duration::from_secs(2)); in db.rs is to make performance Note: The use of sleep(Duration::from_secs(2)); in db.rs is to make performance
improvement with parallelism more obvious. improvement with parallelism more obvious.
*/ */
use std::io; use std::io;

View File

@ -7,7 +7,7 @@ workspace = ".."
[dependencies] [dependencies]
actix-rt = "0.2.2" actix-rt = "0.2.2"
actix-web = { version="1.0.0-alpha.4", features=["ssl"] } actix-web = { version="1.0.0-beta.1", features=["ssl"] }
actix-multipart = { git="https://github.com/actix/actix-web.git" } actix-multipart = { git="https://github.com/actix/actix-web.git" }
bytes = "0.4.12" bytes = "0.4.12"
env_logger = "0.6.1" env_logger = "0.6.1"

View File

@ -2,7 +2,7 @@ use actix_web::{error, web};
use crate::handlers::{parts, products}; use crate::handlers::{parts, products};
pub fn config_app(cfg: &mut web::RouterConfig) { pub fn config_app(cfg: &mut web::ServiceConfig) {
// domain includes: /products/{product_id}/parts/{part_id} // domain includes: /products/{product_id}/parts/{part_id}
cfg.service( cfg.service(
web::scope("/products") web::scope("/products")

View File

@ -13,20 +13,33 @@ pub struct AppState {
pub fn save_file(field: Field) -> impl Future<Item = i64, Error = Error> { pub fn save_file(field: Field) -> impl Future<Item = i64, Error = Error> {
let file_path_string = "upload.png"; let file_path_string = "upload.png";
let mut file = match fs::File::create(file_path_string) { let file = match fs::File::create(file_path_string) {
Ok(file) => file, Ok(file) => file,
Err(e) => return Either::A(err(error::ErrorInternalServerError(e))), Err(e) => return Either::A(err(error::ErrorInternalServerError(e))),
}; };
Either::B( Either::B(
field field
.fold(0i64, move |acc, bytes| { .fold((file, 0i64), move |(mut file, mut acc), bytes| {
file.write_all(bytes.as_ref()) // fs operations are blocking, we have to execute writes
.map(|_| acc + bytes.len() as i64) // on threadpool
.map_err(|e| { web::block(move || {
println!("file.write_all failed: {:?}", e); acc += file
MultipartError::Payload(error::PayloadError::Io(e)) .write_all(bytes.as_ref())
}) .map(|_| acc + bytes.len() as i64)
.map_err(|e| {
println!("file.write_all failed: {:?}", e);
MultipartError::Payload(error::PayloadError::Io(e))
})?;
Ok((file, acc))
})
.map_err(|e: error::BlockingError<MultipartError>| {
match e {
error::BlockingError::Error(e) => e,
error::BlockingError::Canceled => MultipartError::Incomplete,
}
})
}) })
.map(|(_, acc)| acc)
.map_err(|e| { .map_err(|e| {
println!("save_file failed, {:?}", e); println!("save_file failed, {:?}", e);
error::ErrorInternalServerError(e) error::ErrorInternalServerError(e)