1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 13:51:50 +01:00

60 lines
1.9 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
2017-12-20 11:37:27 -08:00
use actix::*;
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
2018-02-26 05:55:07 +03:00
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
2017-12-19 09:51:28 -08:00
{
println!("{:?}", req);
2017-10-21 15:21:16 -07:00
2017-12-20 23:19:21 -08:00
req.multipart() // <- get multipart stream for current request
.from_err() // <- convert multipart errors
.and_then(|item| { // <- iterate over multipart items
match item {
// Handle multipart Field
multipart::MultipartItem::Field(field) => {
println!("==== FIELD ==== {:?}", field);
// Field in turn is stream of *Bytes* object
Either::A(
field.map_err(Error::from)
.map(|chunk| {
println!("-- CHUNK: \n{}",
std::str::from_utf8(&chunk).unwrap());})
.finish())
},
multipart::MultipartItem::Nested(mp) => {
// Or item could be nested Multipart stream
Either::B(result(Ok(())))
2017-10-19 16:22:21 -07:00
}
2017-12-20 23:19:21 -08:00
}
})
.finish() // <- Stream::finish() combinator from actix
2017-12-31 20:08:35 -08:00
.map(|_| httpcodes::HTTPOk.into())
2017-12-20 23:19:21 -08:00
.responder()
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");
2017-12-30 16:09:39 +01:00
let addr = HttpServer::new(
2017-12-19 09:51:28 -08:00
|| Application::new()
2017-12-26 19:59:41 -08:00
.middleware(middleware::Logger::default()) // <- logger
2017-12-19 09:51:28 -08:00
.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();
}