2018-04-30 20:22:48 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-05-08 23:11:26 +00:00
|
|
|
use actix_multipart::Multipart;
|
|
|
|
use actix_web::{
|
|
|
|
web::{post, resource, Data},
|
|
|
|
App, HttpResponse, HttpServer,
|
|
|
|
};
|
2020-05-15 16:59:31 +00:00
|
|
|
use form_data::{handle_multipart, Error, Field, Form};
|
2018-04-30 20:22:48 +00:00
|
|
|
|
2020-05-15 16:59:31 +00:00
|
|
|
async fn upload(mp: Multipart, state: Data<Form>) -> Result<HttpResponse, Error> {
|
|
|
|
let uploaded_content = handle_multipart(mp, state.get_ref().clone()).await?;
|
2018-04-30 20:22:48 +00:00
|
|
|
|
2020-05-15 16:59:31 +00:00
|
|
|
println!("Uploaded Content: {:?}", uploaded_content);
|
|
|
|
Ok(HttpResponse::Created().finish())
|
2018-04-30 20:22:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:59:31 +00:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2018-04-30 20:22:48 +00:00
|
|
|
let form = Form::new()
|
|
|
|
.field("Hey", Field::text())
|
|
|
|
.field(
|
|
|
|
"Hi",
|
|
|
|
Field::map()
|
|
|
|
.field("One", Field::int())
|
|
|
|
.field("Two", Field::float())
|
|
|
|
.finalize(),
|
|
|
|
)
|
2020-05-15 16:59:31 +00:00
|
|
|
.field("files", Field::array(Field::file()));
|
2018-04-30 20:22:48 +00:00
|
|
|
|
|
|
|
println!("{:?}", form);
|
|
|
|
|
2019-05-08 23:11:26 +00:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(form.clone())
|
|
|
|
.service(resource("/upload").route(post().to(upload)))
|
2018-10-09 23:35:10 +00:00
|
|
|
})
|
2019-05-08 23:11:26 +00:00
|
|
|
.bind("127.0.0.1:8080")?
|
2020-05-15 16:59:31 +00:00
|
|
|
.run()
|
|
|
|
.await?;
|
2019-05-08 23:11:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-04-30 20:22:48 +00:00
|
|
|
}
|