2017-10-20 01:22:21 +02:00
|
|
|
#![allow(unused_variables)]
|
2017-10-19 08:43:50 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate env_logger;
|
2017-12-19 18:51:28 +01:00
|
|
|
extern crate futures;
|
2017-10-19 08:43:50 +02:00
|
|
|
|
2017-12-20 20:37:27 +01:00
|
|
|
use actix::*;
|
2017-10-19 08:43:50 +02:00
|
|
|
use actix_web::*;
|
2017-12-19 18:51:28 +01:00
|
|
|
use futures::{Future, Stream};
|
|
|
|
use futures::future::{result, Either};
|
2017-10-19 08:43:50 +02:00
|
|
|
|
2017-12-30 16:09:39 +01:00
|
|
|
use actix::Arbiter;
|
|
|
|
use actix::actors::signal::{ProcessSignals, Subscribe};
|
2017-10-19 08:43:50 +02:00
|
|
|
|
2017-12-19 18:51:28 +01:00
|
|
|
fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
|
|
|
|
{
|
|
|
|
println!("{:?}", req);
|
2017-10-22 00:21:16 +02:00
|
|
|
|
2017-12-21 08:19:21 +01: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-20 01:22:21 +02:00
|
|
|
}
|
2017-12-21 08:19:21 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.finish() // <- Stream::finish() combinator from actix
|
|
|
|
.map(|_| httpcodes::HTTPOk.response())
|
|
|
|
.responder()
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-12-19 18:51:28 +01:00
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
2017-10-19 08:43:50 +02: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 18:51:28 +01:00
|
|
|
|| Application::new()
|
2017-12-27 04:59:41 +01:00
|
|
|
.middleware(middleware::Logger::default()) // <- logger
|
2017-12-19 18:51:28 +01:00
|
|
|
.resource("/multipart", |r| r.method(Method::POST).a(index)))
|
|
|
|
.bind("127.0.0.1:8080").unwrap()
|
|
|
|
.start();
|
|
|
|
|
2017-12-30 16:09:39 +01:00
|
|
|
let signals = Arbiter::system_registry().get::<ProcessSignals>();
|
|
|
|
signals.send(Subscribe(addr.subscriber()));
|
|
|
|
|
2017-12-19 18:51:28 +01:00
|
|
|
println!("Starting http server: 127.0.0.1:8080");
|
2017-10-19 08:43:50 +02:00
|
|
|
let _ = sys.run();
|
|
|
|
}
|