Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to tower-http 0.4 and axum 0.6 #4

Merged
merged 2 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 31 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ version = "0.1.0"
edition = "2021"

[dependencies]
axum = { version = "0.4", features = ["multipart"] }
axum = { version = "0.6", features = ["multipart"] }
clap = { version = "4", features = ["derive"] }
rust-embed = "6"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version="0.3", features = ["env-filter"] }
tower-http = { version = "0.2.0", features = ["trace"] }
tower-http = { version = "0.4.0", features = ["trace"] }
17 changes: 4 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{borrow::Cow, net::SocketAddr, path::PathBuf, process, sync::Arc};

use axum::{
body::{boxed, Bytes, Full},
extract::{ContentLengthLimit, Extension, Multipart},
extract::{DefaultBodyLimit, Extension, Multipart},
http::{header, Response},
response::{Html, IntoResponse, Redirect},
routing::get,
Expand Down Expand Up @@ -52,6 +52,7 @@ async fn main() {
.route("/", get(index).post(accept_form))
.route("/scripts.js", get(scripts))
.layer(Extension(args.clone()))
.layer(DefaultBodyLimit::max(250 * 1024 * 1024)) // 250MB limit
dbrgn marked this conversation as resolved.
Show resolved Hide resolved
.layer(tower_http::trace::TraceLayer::new_for_http());

// run it with hyper
Expand All @@ -75,13 +76,6 @@ async fn scripts() -> impl IntoResponse {
.unwrap()
}

type LimitedMultipart = ContentLengthLimit<
Multipart,
{
250 * 1024 * 1024 /* 250mb */
},
>;

async fn store_file(filename: &str, bytes: Bytes, args: &Args) -> std::io::Result<()> {
info!(
"Store file with filename {:?} and {} bytes to dir {:?}",
Expand All @@ -93,16 +87,13 @@ async fn store_file(filename: &str, bytes: Bytes, args: &Args) -> std::io::Resul
Ok(())
}

async fn accept_form(
ContentLengthLimit(mut multipart): LimitedMultipart,
Extension(args): Extension<Arc<Args>>,
) -> Redirect {
async fn accept_form(Extension(args): Extension<Arc<Args>>, mut multipart: Multipart) -> Redirect {
dbrgn marked this conversation as resolved.
Show resolved Hide resolved
// Store all files in the multipart body in the file system
while let Some(field) = multipart.next_field().await.unwrap() {
let file_name = field.file_name().unwrap().to_string();
if let Err(e) = store_file(&file_name, field.bytes().await.unwrap(), &args).await {
error!("Failed to upload file {}: {:?}", file_name, e);
}
}
Redirect::to("/".parse().unwrap())
Redirect::to("/")
dbrgn marked this conversation as resolved.
Show resolved Hide resolved
}