Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redirect handlers #202

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/shared/src/core/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use std::sync::Arc;

use super::RouteHandler;
use crate::{
server::{context::AppState, Method, Middlewares, NgynContext, NgynResponse, Routes},
server::{context::AppState, Method, NgynContext, NgynResponse},
traits::{Middleware, NgynController, NgynInterpreter, NgynMiddleware, NgynModule},
};

#[derive(Default)]
pub struct PlatformData {
routes: Routes,
middlewares: Middlewares,
routes: Vec<(String, Option<Method>, Box<crate::core::RouteHandler>)>,
middlewares: Vec<Box<dyn crate::traits::Middleware>>,
interpreters: Vec<Box<dyn NgynInterpreter>>,
state: Option<Arc<Box<dyn AppState>>>,
}
Expand Down
51 changes: 49 additions & 2 deletions crates/shared/src/core/handler.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{future::Future, pin::Pin};

use http::{HeaderValue, StatusCode};

use crate::server::{NgynContext, NgynResponse, ToBytes};

/// Represents a handler function that takes in a mutable reference to `NgynContext` and `NgynResponse`.
pub type Handler = dyn Fn(&mut NgynContext, &mut NgynResponse) + Send + Sync + 'static;
pub(crate) type Handler = dyn Fn(&mut NgynContext, &mut NgynResponse) + Send + Sync + 'static;

pub type AsyncHandler = Box<
pub(crate) type AsyncHandler = Box<
dyn for<'a, 'b> Fn(
&'a mut NgynContext,
&'b mut NgynResponse,
Expand Down Expand Up @@ -73,3 +75,48 @@ pub fn async_handler<S: ToBytes + 'static, Fut: Future<Output = S> + Send + 'sta
})
})
}

/// Create a not-implemented handler that returns a `501 Not Implemented` status code.
///
/// This is very similar to unimplemented! macro in Rust.
pub fn not_implemented() -> Box<Handler> {
Box::new(|_ctx: &mut NgynContext, res: &mut NgynResponse| {
*res.status_mut() = StatusCode::NOT_IMPLEMENTED;
})
}

/// Redirects to a specified location with a `303 See Other` status code.
pub fn redirect_to(location: &'static str) -> Box<Handler> {
Box::new(|_ctx: &mut NgynContext, res: &mut NgynResponse| {
res.headers_mut()
.insert("Location", HeaderValue::from_str(location).unwrap());
*res.status_mut() = StatusCode::SEE_OTHER;
})
}

/// Redirects to a specified location with a `307 Temporary Redirect` status code.
pub fn redirect_temporary(location: &'static str) -> Box<Handler> {
Box::new(|_ctx: &mut NgynContext, res: &mut NgynResponse| {
res.headers_mut()
.insert("Location", HeaderValue::from_str(location).unwrap());
*res.status_mut() = StatusCode::TEMPORARY_REDIRECT;
})
}

/// Redirects to a specified location with a `301 Moved Permanently` status code.
pub fn redirect_permanent(location: &'static str) -> Box<Handler> {
Box::new(|_ctx: &mut NgynContext, res: &mut NgynResponse| {
res.headers_mut()
.insert("Location", HeaderValue::from_str(location).unwrap());
*res.status_mut() = StatusCode::MOVED_PERMANENTLY;
})
}

/// Redirects to a specified location with a `302 Found` status code.
pub fn redirect_found(location: &'static str) -> Box<Handler> {
Box::new(|_ctx: &mut NgynContext, res: &mut NgynResponse| {
res.headers_mut()
.insert("Location", HeaderValue::from_str(location).unwrap());
*res.status_mut() = StatusCode::FOUND;
})
}
3 changes: 0 additions & 3 deletions crates/shared/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ pub use transformer::{Body, Param, Query, Transducer, Transformer};

pub type NgynRequest = http::Request<Vec<u8>>;
pub type NgynResponse = http::Response<Full<Bytes>>;

pub(crate) type Routes = Vec<(String, Option<Method>, Box<crate::core::RouteHandler>)>;
pub(crate) type Middlewares = Vec<Box<dyn crate::traits::Middleware>>;
Loading