Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "s3-active-storage"
version = "0.1.0"
edition = "2021"
# Due to AWS SDK.
rust-version = "1.62.1"
rust-version = "1.66.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -14,8 +14,7 @@ aws-sdk-s3 = "0.27"
aws-smithy-http = "0.55"
aws-smithy-types = "0.55"
aws-types = "0.55"
# Axum 0.6.13 introduced a change in behaviour around trimming trailing paths.
axum = { version = "=0.6.12", features = ["headers"] }
axum = { version = "0.6", features = ["headers"] }
axum-server = { version = "0.4.7", features = ["tls-rustls"] }
clap = { version = "4.2", features = ["derive", "env"] }
expanduser = "1.2.2"
Expand All @@ -33,6 +32,7 @@ thiserror = "1.0"
tokio = { version = "1.28", features = ["full"] }
tower = "0.4"
tower-http = { version = "0.4", features = ["normalize-path", "trace", "validate-request"] }
tower-service = "0.3"
tokio-stream = "0.1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
Expand Down
25 changes: 21 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use axum::{
Router, TypedHeader,
};

use tower::Layer;
use tower::ServiceBuilder;
use tower_http::normalize_path::NormalizePathLayer;
use tower_http::trace::TraceLayer;
Expand Down Expand Up @@ -54,9 +55,7 @@ impl IntoResponse for models::Response {
/// * a [tower_http::trace::TraceLayer] for tracing requests and responses
/// * a [tower_http::validate_request::ValidateRequestHeaderLayer] for validating authorisation
/// headers
/// * a [tower_http::normalize_path::NormalizePathLayer] for trimming trailing slashes from
/// requests
pub fn router() -> Router {
fn router() -> Router {
fn v1() -> Router {
Router::new()
.route("/count", post(operation_handler::<operations::Count>))
Expand Down Expand Up @@ -85,7 +84,25 @@ pub fn router() -> Router {
Router::new()
.route("/.well-known/s3-active-storage-schema", get(schema))
.nest("/v1", v1())
.layer(NormalizePathLayer::trim_trailing_slash())
}

/// Returns a [tower_service::Service] for the Active Storage server API
///
/// The service is populated with all routes as well as the following middleware:
///
/// * a [tower_http::trace::TraceLayer] for tracing requests and responses
/// * a [tower_http::validate_request::ValidateRequestHeaderLayer] for validating authorisation
/// headers
/// * a [tower_http::normalize_path::NormalizePathLayer] for trimming trailing slashes from
/// requests
pub fn service() -> tower_http::normalize_path::NormalizePath<Router> {
// FIXME: The return type should be some form of tower_service::Service, but couldn't find the
// necessary trait bounds.

// Note that any middleware that should affect routing must wrap the router.
// See
// https://docs.rs/axum/0.6.18/axum/middleware/index.html#rewriting-request-uri-in-middleware.
NormalizePathLayer::trim_trailing_slash().layer(router())
}

/// TODO: Return an OpenAPI schema
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use std::{net::SocketAddr, process::exit, str::FromStr, time::Duration};

use axum::ServiceExt;
use axum_server::{tls_rustls::RustlsConfig, Handle};
use clap::Parser;
use expanduser::expanduser;
Expand Down Expand Up @@ -77,7 +78,7 @@ async fn main() {

init_tracing();

let router = app::router();
let service = app::service();
let addr = SocketAddr::from_str(&format!("{}:{}", args.host, args.port))
.expect("invalid host name, IP address or port number");

Expand Down Expand Up @@ -120,14 +121,14 @@ async fn main() {
// run HTTPS server with hyper
axum_server::bind_rustls(addr, tls_config)
.handle(handle)
.serve(router.into_make_service())
.serve(service.into_make_service())
.await
.unwrap();
} else {
// run HTTP server with hyper
axum_server::bind(addr)
.handle(handle)
.serve(router.into_make_service())
.serve(service.into_make_service())
.await
.unwrap();
}
Expand Down