Skip to content

Commit 745b12e

Browse files
committed
feat: more redirect options
1 parent b712942 commit 745b12e

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

astra/lua/http.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ local http = {}
102102
---@field set_cookie fun(self: HTTPServerResponse, cookie: Cookie)
103103
---@field remove_cookie fun(self: HTTPServerResponse, cookie: Cookie)
104104
---@field redirect_to fun(self: HTTPServerResponse, redirect_uri: string)
105+
---@field redirect_temporary fun(self: HTTPServerResponse, redirect_uri: string)
106+
---@field redirect_permanent fun(self: HTTPServerResponse, redirect_uri: string)
105107

106108
---@class Cookie
107109
---@field set_name fun(self: Cookie, name: string)

astra/teal/http.tl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ global interface HTTPServerResponse is userdata
100100
set_cookie: function(self: HTTPServerResponse, cookie: Cookie)
101101
remove_cookie: function(self: HTTPServerResponse, cookie: Cookie)
102102
redirect_to: function(self: HTTPServerResponse, redirect_uri: string)
103+
redirect_temporary: function(self: HTTPServerResponse, redirect_uri: string)
104+
redirect_permanent: function(self: HTTPServerResponse, redirect_uri: string)
103105
end
104106

105107
global interface Cookie is userdata

src/components/http/server/responses.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::components::http::server::cookie::AstraHTTPCookie;
2-
use axum::http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
2+
use axum::{
3+
http::{HeaderMap, HeaderName, HeaderValue, StatusCode},
4+
response::Redirect,
5+
};
36

47
// ! Support more cookie types like signed and private
58
#[derive(Debug, Clone)]
@@ -13,7 +16,7 @@ pub struct ResponseLua<'a> {
1316
pub status_code: StatusCode,
1417
pub headers: HeaderMap,
1518
pub cookie_operations: Vec<CookieOperation<'a>>,
16-
pub redirect: Option<String>,
19+
pub redirect: Option<Redirect>,
1720
}
1821
impl Default for ResponseLua<'_> {
1922
fn default() -> Self {
@@ -39,9 +42,16 @@ impl mlua::UserData for ResponseLua<'_> {
3942
}
4043
});
4144

42-
methods.add_method_mut("redirect_to", |_, this, redirect_to: String| {
43-
this.redirect = Some(redirect_to);
44-
45+
methods.add_method_mut("redirect_to", |_, this, redirect_path: String| {
46+
this.redirect = Some(Redirect::to(&redirect_path));
47+
Ok(())
48+
});
49+
methods.add_method_mut("redirect_temporary", |_, this, redirect_path: String| {
50+
this.redirect = Some(Redirect::temporary(&redirect_path));
51+
Ok(())
52+
});
53+
methods.add_method_mut("redirect_permanent", |_, this, redirect_path: String| {
54+
this.redirect = Some(Redirect::permanent(&redirect_path));
4555
Ok(())
4656
});
4757

src/components/http/server/routes.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ pub async fn route(
7272
let response_details = response.borrow::<responses::ResponseLua>()?;
7373

7474
if let Some(redirect_to) = &response_details.redirect {
75-
return Ok((
76-
cookie_jar,
77-
axum::response::Redirect::temporary(redirect_to).into_response(),
78-
));
75+
return Ok((cookie_jar, redirect_to.clone().into_response()));
7976
}
8077

8178
let mut resulting_response = match result {

0 commit comments

Comments
 (0)