Skip to content

Commit e927074

Browse files
risssondavepgreene
authored andcommitted
packages/ak-axum: init (goauthentik#21313)
1 parent c76d9c0 commit e927074

5 files changed

Lines changed: 219 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 169 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"packages/ak-axum",
34
"packages/ak-common",
45
"packages/client-rust",
56
"website/scripts/docsmg",
@@ -21,6 +22,7 @@ publish = false
2122
arc-swap = "= 1.9.1"
2223
axum-server = { version = "= 0.8.0", features = ["tls-rustls-no-provider"] }
2324
aws-lc-rs = { version = "= 1.16.2", features = ["fips"] }
25+
axum = { version = "= 0.8.8", features = ["http2", "macros", "ws"] }
2426
clap = { version = "= 4.6.0", features = ["derive", "env"] }
2527
colored = "= 3.1.1"
2628
config-rs = { package = "config", version = "= 0.15.22", default-features = false, features = [
@@ -29,6 +31,7 @@ config-rs = { package = "config", version = "= 0.15.22", default-features = fals
2931
] }
3032
console-subscriber = "= 0.5.0"
3133
dotenvy = "= 0.15.7"
34+
durstr = "= 0.5.1"
3235
eyre = "= 0.6.12"
3336
glob = "= 0.3.3"
3437
json-subscriber = "= 0.2.8"
@@ -73,6 +76,8 @@ time = { version = "= 0.3.47", features = ["macros"] }
7376
thiserror = "= 2.0.18"
7477
tokio = { version = "= 1.51.0", features = ["full", "tracing"] }
7578
tokio-util = { version = "= 0.7.18", features = ["full"] }
79+
tower = "= 0.5.3"
80+
tower-http = { version = "= 0.6.8", features = ["timeout"] }
7681
tracing = "= 0.1.44"
7782
tracing-error = "= 0.2.1"
7883
tracing-subscriber = { version = "= 0.3.23", features = [

packages/ak-axum/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "authentik-axum"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
readme.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
license-file.workspace = true
10+
publish.workspace = true
11+
12+
[dependencies]
13+
axum.workspace = true
14+
durstr.workspace = true
15+
ak-common.workspace = true
16+
tower.workspace = true
17+
tower-http.workspace = true
18+
19+
[lints]
20+
workspace = true

packages/ak-axum/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Utilities for working with [`axum`].
2+
3+
pub mod router;

packages/ak-axum/src/router.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Utilities for working with [`Router`].
2+
3+
use ak_common::config;
4+
use axum::{Router, http::StatusCode};
5+
use tower::ServiceBuilder;
6+
use tower_http::timeout::TimeoutLayer;
7+
8+
/// Wrap a [`Router`] with common middlewares.
9+
#[inline]
10+
pub fn wrap_router(router: Router) -> Router {
11+
let config = config::get();
12+
let timeout = durstr::parse(&config.web.timeout_http_read_header)
13+
.expect("Invalid duration in http timeout")
14+
+ durstr::parse(&config.web.timeout_http_read).expect("Invalid duration in http timeout")
15+
+ durstr::parse(&config.web.timeout_http_write).expect("Invalid duration in http timeout")
16+
+ durstr::parse(&config.web.timeout_http_idle).expect("Invalid duration in http timeout");
17+
let service_builder = ServiceBuilder::new().layer(TimeoutLayer::with_status_code(
18+
StatusCode::REQUEST_TIMEOUT,
19+
timeout,
20+
));
21+
router.layer(service_builder)
22+
}

0 commit comments

Comments
 (0)