1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 11:02:08 +01:00

62 lines
2.0 KiB
Rust
Raw Normal View History

2017-10-19 16:22:21 -07:00
#![allow(unused_variables)]
2017-10-18 23:43:50 -07:00
extern crate actix;
extern crate actix_web;
extern crate env_logger;
2017-12-19 09:51:28 -08:00
extern crate futures;
2017-10-18 23:43:50 -07:00
use actix_web::*;
2017-12-19 09:51:28 -08:00
use futures::{Future, Stream};
use futures::future::{result, Either};
2017-10-18 23:43:50 -07:00
2017-12-19 09:51:28 -08:00
fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
{
println!("{:?}", req);
2017-10-21 15:21:16 -07:00
2017-12-19 09:51:28 -08:00
// get multipart stream and iterate over multipart items
Box::new(
req.multipart()
.map_err(Error::from)
.and_then(|item| {
2017-10-20 17:16:17 -07:00
// Multipart stream is a stream of Fields and nested Multiparts
2017-10-19 16:22:21 -07:00
match item {
multipart::MultipartItem::Field(field) => {
println!("==== FIELD ==== {:?}", field);
2017-10-18 23:43:50 -07:00
2017-10-19 16:22:21 -07:00
// Read field's stream
2017-12-19 09:51:28 -08:00
Either::A(
field.map_err(Error::from)
.map(|chunk| {
println!("-- CHUNK: \n{}",
std::str::from_utf8(&chunk.0).unwrap());})
.fold((), |_, _| result::<_, Error>(Ok(()))))
2017-10-19 16:22:21 -07:00
},
multipart::MultipartItem::Nested(mp) => {
// Do nothing for nested multipart stream
2017-12-19 09:51:28 -08:00
Either::B(result(Ok(())))
2017-10-19 16:22:21 -07:00
}
}
})
// wait until stream finish
2017-12-19 09:51:28 -08:00
.fold((), |_, _| result::<_, Error>(Ok(())))
.map(|_| httpcodes::HTTPOk.response())
)
2017-10-18 23:43:50 -07:00
}
fn main() {
2017-12-19 09:51:28 -08:00
::std::env::set_var("RUST_LOG", "actix_web=info");
2017-10-18 23:43:50 -07:00
let _ = env_logger::init();
let sys = actix::System::new("multipart-example");
HttpServer::new(
2017-12-19 09:51:28 -08:00
|| Application::new()
// enable logger
.middleware(middlewares::Logger::default())
.resource("/multipart", |r| r.method(Method::POST).a(index)))
.bind("127.0.0.1:8080").unwrap()
.start();
println!("Starting http server: 127.0.0.1:8080");
2017-10-18 23:43:50 -07:00
let _ = sys.run();
}