Skip to content

Commit

Permalink
feat: create utils module with function for htmx redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
transcaffeine committed Jul 4, 2024
1 parent e8afc6c commit 9bdc9fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::config::Config;
pub mod handlers;
mod db;
mod config;
mod utils;

#[tokio::main]
async fn main() {
Expand Down
19 changes: 19 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use axum::{
http::{
HeaderMap,
},
};

use axum_core::response::IntoResponse;

use crate::AppError;

pub fn make_htmx_redirect(request_headers: HeaderMap, target: &str) -> Result<impl IntoResponse, AppError> {
let mut headers = HeaderMap::new();
let header_name: &str = request_headers
.get("HX-Request")
.filter(|v| *v == "true")
.map_or_else(|| "Location", |_| "HX-Location");
headers.insert(header_name, target.parse().unwrap());
Ok(headers)
}

0 comments on commit 9bdc9fc

Please sign in to comment.