Skip to content

Commit

Permalink
Update to tower-http 0.4 and axum 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
rnestler committed Jul 16, 2023
1 parent edf3f15 commit 61620b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
39 changes: 28 additions & 11 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.5", 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.3.0", features = ["trace"] }
tower-http = { version = "0.4.0", features = ["trace"] }
12 changes: 3 additions & 9 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
.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 @@ -94,8 +88,8 @@ async fn store_file(filename: &str, bytes: Bytes, args: &Args) -> std::io::Resul
}

async fn accept_form(
ContentLengthLimit(mut multipart): LimitedMultipart,
Extension(args): Extension<Arc<Args>>,
mut multipart: Multipart,
) -> Redirect {
// Store all files in the multipart body in the file system
while let Some(field) = multipart.next_field().await.unwrap() {
Expand Down

0 comments on commit 61620b3

Please sign in to comment.