mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-29 19:52:40 +01:00
60 lines
1.5 KiB
Rust
60 lines
1.5 KiB
Rust
|
use axum::{
|
||
|
extract::{ContentLengthLimit, Multipart},
|
||
|
prelude::*,
|
||
|
};
|
||
|
use std::net::SocketAddr;
|
||
|
|
||
|
#[tokio::main]
|
||
|
async fn main() {
|
||
|
tracing_subscriber::fmt::init();
|
||
|
|
||
|
// build our application with some routes
|
||
|
let app = route("/", get(show_form).post(accept_form))
|
||
|
.layer(tower_http::trace::TraceLayer::new_for_http());
|
||
|
|
||
|
// run it with hyper
|
||
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||
|
tracing::debug!("listening on {}", addr);
|
||
|
hyper::Server::bind(&addr)
|
||
|
.serve(app.into_make_service())
|
||
|
.await
|
||
|
.unwrap();
|
||
|
}
|
||
|
|
||
|
async fn show_form() -> response::Html<&'static str> {
|
||
|
response::Html(
|
||
|
r#"
|
||
|
<!doctype html>
|
||
|
<html>
|
||
|
<head></head>
|
||
|
<body>
|
||
|
<form action="/" method="post" enctype="multipart/form-data">
|
||
|
<label>
|
||
|
Upload file:
|
||
|
<input type="file" name="file" multiple>
|
||
|
</label>
|
||
|
|
||
|
<input type="submit" value="Upload files">
|
||
|
</form>
|
||
|
</body>
|
||
|
</html>
|
||
|
"#,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
async fn accept_form(
|
||
|
ContentLengthLimit(mut multipart): ContentLengthLimit<
|
||
|
Multipart,
|
||
|
{
|
||
|
250 * 1024 * 1024 /* 250mb */
|
||
|
},
|
||
|
>,
|
||
|
) {
|
||
|
while let Some(field) = multipart.next_field().await.unwrap() {
|
||
|
let name = field.name().unwrap().to_string();
|
||
|
let data = field.bytes().await.unwrap();
|
||
|
|
||
|
println!("Length of `{}` is {} bytes", name, data.len());
|
||
|
}
|
||
|
}
|