Skip to content

Commit

Permalink
Merge branch 'master' into OpenAPI-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad authored Jan 18, 2025
2 parents e0c1f89 + be4e44f commit d72c460
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs-site/content/docs/getting-started/axum-users.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,4 @@ pub fn routes() -> Routes {
### Verdict

* **A drop-in compatibility** - Loco uses Axum and keeps all of its building blocks intact so that you can just use your own existing Axum code with no efforts.
* **Route metadata for free** - one gap that Axum routers has is the ability to describe the currently configured routes, which can be used for listing or automatic OpenAPI schema generation. Loco has a small metadata layer to suppor this. If you use `Routes` you get it for free, while all of the different signatures remain compatible with Axum router.
* **Route metadata for free** - one gap that Axum routers has is the ability to describe the currently configured routes, which can be used for listing or automatic OpenAPI schema generation. Loco has a small metadata layer to support this. If you use `Routes` you get it for free, while all of the different signatures remain compatible with Axum router.
3 changes: 3 additions & 0 deletions loco-new/tests/wizard/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ fn test_combination(
// Generate deployment nginx
tester.run_generate(&vec!["deployment", "--kind", "docker"]);

// Generate deployment shuttle
tester.run_generate(&vec!["deployment", "--kind", "shuttle"]);

if db.enable() {
// Generate Model
if !settings.auth {
Expand Down
26 changes: 25 additions & 1 deletion src/controller/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//! format::json(Health { ok: true })
//! }
//! ```
use std::convert::TryInto;

use axum::{
body::Body,
Expand Down Expand Up @@ -373,10 +374,23 @@ impl RenderBuilder {
///
/// This function will return an error if IO fails
pub fn redirect(self, to: &str) -> Result<Response> {
self.redirect_with_header_key(header::LOCATION, to)
}

/// Finalizes the HTTP response and redirects to a specified location using a dynamic header key.
///
/// # Errors
///
/// This function will return an error if IO fails
pub fn redirect_with_header_key<K>(self, key: K, to: &str) -> Result<Response>
where
K: TryInto<HeaderName>,
<K as TryInto<HeaderName>>::Error: Into<axum::http::Error>,
{
Ok(self
.response
.status(StatusCode::SEE_OTHER)
.header(header::LOCATION, to)
.header(key, to)
.body(Body::empty())?)
}
}
Expand Down Expand Up @@ -637,4 +651,14 @@ mod tests {
assert_debug_snapshot!(response);
assert_eq!(response_body_to_string(response).await, String::new());
}

#[tokio::test]
async fn builder_redirect_with_custom_header_response() {
let response = render()
.redirect_with_header_key("HX-Redirect", "https://loco.rs")
.unwrap();

assert_debug_snapshot!(response);
assert_eq!(response_body_to_string(response).await, String::new());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: src/controller/format.rs
expression: response
---
Response {
status: 303,
version: HTTP/1.1,
headers: {
"hx-redirect": "https://loco.rs",
},
body: Body(
UnsyncBoxBody,
),
}

0 comments on commit d72c460

Please sign in to comment.