Skip to content

Commit dd555a6

Browse files
authored
feat: Allow configuring the status code of the timeout middleware (#947)
1 parent f0efc13 commit dd555a6

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ jobs:
198198
runs-on: ubuntu-latest
199199
steps:
200200
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
201-
- uses: EmbarkStudios/cargo-deny-action@34899fc7ba81ca6268d5947a7a16b4649013fea1
201+
- uses: EmbarkStudios/cargo-deny-action@76cd80eb775d7bbbd2d80292136d74d39e1b4918
202202
with:
203203
command: check
204204
arguments: "--all-features --locked"

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rust-version = "1.88"
1616
[features]
1717
default = ["open-api", "jwt-ietf", "cli", "otel"]
1818

19-
http = ["dep:axum", "dep:axum-extra", "dep:tower", "dep:tower-http", "dep:http-body-util", "dep:mime"]
19+
http = ["dep:axum", "dep:axum-extra", "dep:tower", "dep:tower-http", "dep:http-body-util", "dep:mime", "dep:http-serde"]
2020
open-api = ["http", "dep:aide", "dep:schemars"]
2121

2222
worker = ["dep:rand", "dep:num_cpus", "dep:cron", "uuid/v7"]
@@ -90,6 +90,7 @@ aide = { workspace = true, features = ["axum", "axum-json", "axum-query", "redoc
9090
schemars = { workspace = true, optional = true }
9191
http-body-util = { workspace = true, optional = true }
9292
mime = { workspace = true, optional = true }
93+
http-serde = { workspace = true, optional = true }
9394

9495
# DB - SeaORM
9596
sea-orm = { workspace = true, features = ["debug-print", "runtime-tokio-rustls", "sqlx-postgres", "macros"], optional = true }
@@ -204,11 +205,12 @@ aide = { version = "0.16.0-alpha.1", features = ["axum"] }
204205
axum-core = "0.5.2"
205206
axum = "0.8.4"
206207
axum-extra = "0.12.1"
207-
tower-http = "0.6.2"
208+
tower-http = "0.6.7"
208209
tower = "0.5.2"
209210
schemars = "1.0.4"
210211
mime = "0.3.17"
211212
http-body-util = { version = "0.1.2" }
213+
http-serde = "2.0.0"
212214

213215
# DB - SeaORM
214216
sea-orm = { version = "1.1.15" }

src/config/service/http/config/default.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ priority = -9960
4545
[service.http.middleware.timeout]
4646
priority = 0
4747
timeout = 10000
48+
status-code = 408
4849

4950
[service.http.middleware.size-limit]
5051
priority = -9970

src/service/http/middleware/timeout.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::app::context::AppContext;
22
use crate::service::http::middleware::Middleware;
33
use axum::Router;
4+
use axum::http::StatusCode;
45
use axum_core::extract::FromRef;
56
use serde_derive::{Deserialize, Serialize};
67
use serde_with::serde_as;
@@ -10,19 +11,13 @@ use validator::Validate;
1011

1112
#[serde_as]
1213
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
13-
#[serde(rename_all = "kebab-case", default)]
14+
#[serde(rename_all = "kebab-case")]
1415
#[non_exhaustive]
1516
pub struct TimeoutConfig {
1617
#[serde_as(as = "serde_with::DurationMilliSeconds")]
1718
pub timeout: Duration,
18-
}
19-
20-
impl Default for TimeoutConfig {
21-
fn default() -> Self {
22-
Self {
23-
timeout: Duration::from_secs(10),
24-
}
25-
}
19+
#[serde(with = "http_serde::status_code")]
20+
pub status_code: StatusCode,
2621
}
2722

2823
pub struct TimeoutMiddleware;
@@ -63,17 +58,19 @@ where
6358

6459
fn install(&self, state: &S, router: Router) -> Result<Router, Self::Error> {
6560
let context = AppContext::from_ref(state);
66-
let timeout = &context
61+
let config = &context
6762
.config()
6863
.service
6964
.http
7065
.custom
7166
.middleware
7267
.timeout
73-
.custom
74-
.timeout;
68+
.custom;
69+
70+
let status_code = config.status_code;
71+
let timeout = config.timeout;
7572

76-
let router = router.layer(TimeoutLayer::new(*timeout));
73+
let router = router.layer(TimeoutLayer::with_status_code(status_code, timeout));
7774

7875
Ok(router)
7976
}

0 commit comments

Comments
 (0)