Skip to content

Commit

Permalink
Merge pull request #4 from coredump-ch/update-tower-and-axum
Browse files Browse the repository at this point in the history
Update to tower-http 0.4 and axum 0.6
  • Loading branch information
rnestler committed Jul 16, 2023
2 parents 581013d + 4745f46 commit 1e01e2e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
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
.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 {
// 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("/")
}

0 comments on commit 1e01e2e

Please sign in to comment.