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

refactor: Implement log and authentication as tower::Layer #23

Merged
merged 5 commits into from
Aug 23, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ prost = "0.12.6"
async-channel = "2.3.1"
tower = "0.4.13"
async-trait = "0.1.81"
pin-project = "1.1.5"

[dependencies.web-sys]
version = "0.3.63"
Expand All @@ -65,6 +66,7 @@ features = [
]

[dev-dependencies]
futures-util = "0.3.30"
mockito = "1.4.0"
test-case = "3.3.1"
tokio = { version = "1.39.1", features = ["macros"]}
26 changes: 17 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Router,
};
use http::StatusCode;
use time::OffsetDateTime;
use url::Url;

use crate::{package, pyoci::PyOciError, templates, PyOci};
Expand Down Expand Up @@ -50,24 +51,32 @@
"/:registry/:namespace/",
post(publish_package).layer(DefaultBodyLimit::max(50 * 1024 * 1024)),
)
.layer(axum::middleware::from_fn(trace_middleware))
.layer(axum::middleware::from_fn(accesslog_middleware))
}

/// Log incoming requests
async fn trace_middleware(
async fn accesslog_middleware(
method: axum::http::Method,
uri: axum::http::Uri,
headers: axum::http::HeaderMap,
request: axum::extract::Request,
next: axum::middleware::Next,
) -> axum::response::Response {
let start = OffsetDateTime::now_utc();
let response = next.run(request).await;

let status: u16 = response.status().into();
let user_agent = headers
.get("user-agent")
.map(|ua| ua.to_str().unwrap_or(""));
tracing::info!(method = %method, status, path = %uri.path(), user_agent, "type" = "request");
tracing::info!(
elapsed_ms = (OffsetDateTime::now_utc() - start).whole_milliseconds(),
method = method.to_string(),
status,
path = uri.path(),

Check warning on line 76 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L73-L76

Added lines #L73 - L76 were not covered by tests
user_agent,
"type" = "request"
);
response
}

Expand All @@ -90,7 +99,7 @@

let package: package::Info = path_params.0.try_into()?;

let client = PyOci::new(package.registry.clone(), auth);
let mut client = PyOci::new(package.registry.clone(), auth)?;

Check warning on line 102 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L102

Added line #L102 was not covered by tests
// Fetch at most 45 packages
// https://developers.cloudflare.com/workers/platform/limits/#account-plan-limits
let files = client.list_package_files(&package, 45).await?;
Expand All @@ -101,7 +110,7 @@
}

/// Download package request handler
// #[debug_handler]
#[debug_handler]

Check warning on line 113 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L113

Added line #L113 was not covered by tests
// Mark the handler as Send when building a wasm target
// JsFuture, and most other JS objects are !Send
// Because the cloudflare worker runtime is single-threaded, we can safely mark this as Send
Expand All @@ -117,15 +126,14 @@
};
let package: package::Info = path_params.0.try_into()?;

let client = PyOci::new(package.registry.clone(), auth);
let mut client = PyOci::new(package.registry.clone(), auth)?;

Check warning on line 129 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L129

Added line #L129 was not covered by tests
let data = client
.download_package_file(&package)
.await?
.bytes()
.await
.expect("valid bytes");

// TODO: With some trickery we could stream the data directly to the response
Ok((
[(
header::CONTENT_DISPOSITION,
Expand All @@ -138,7 +146,7 @@
/// Publish package request handler
///
/// ref: https://warehouse.pypa.io/api-reference/legacy.html#upload-api
// #[debug_handler]
#[debug_handler]

Check warning on line 149 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L149

Added line #L149 was not covered by tests
// Mark the handler as Send when building a wasm target
// JsFuture, and most other JS objects are !Send
// Because the cloudflare worker runtime is single-threaded, we can safely mark this as Send
Expand All @@ -156,7 +164,7 @@
None => None,
};
let package: package::Info = (registry, namespace, None, form_data.filename).try_into()?;
let client = PyOci::new(package.registry.clone(), auth);
let mut client = PyOci::new(package.registry.clone(), auth)?;

client
.publish_package_file(&package, form_data.content)
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod pyoci;
mod templates;
// HTTP Transport
mod transport;
// Services
mod service;
// Re-export the PyOci client
pub use pyoci::PyOci;

Expand Down
2 changes: 1 addition & 1 deletion src/otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@

fn on_close(&self, id: tracing_core::span::Id, ctx: Context<'_, S>) {
let span = ctx.span(&id).expect("span not found");
if !span.parent().is_none() {
if span.parent().is_some() {

Check warning on line 217 in src/otlp.rs

View check run for this annotation

Codecov / codecov/patch

src/otlp.rs#L217

Added line #L217 was not covered by tests
// This is a sub-span, we'll flush all messages when the root span is closed
return;
}
Expand Down
Loading