-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f2f323
commit 31a5fdf
Showing
6 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Lets you make routes that return responses synchronously. | ||
//! This is similar to how routes used to work in pre 3.0.0 versions of afire. | ||
|
||
use crate::{error::AnyResult, route::Route, trace::emoji, Context, Method, Response, Server}; | ||
|
||
/// Lets you make routes that return responses synchronously. | ||
pub trait SyncRoute<State> { | ||
/// Creates a new route on the server. | ||
/// Unlike [`Server::route`], you send your response by returning a Result<Response, Error> from your handler. | ||
/// Because of this, you should refrain from using [`Context::send`] in your handler as it will throw an error when trying to send a response twice.] | ||
/// | ||
/// Refer to the [`Server::route`] docs for more information on making routes. | ||
/// | ||
/// ## Example | ||
/// ```rust | ||
/// # use afire::{Server, AnyResult, Context, Response, Method}; | ||
/// # fn test(server: &mut Server<()>) { | ||
/// server.sync_route(Method::GET, "/hello/{name}", |ctx| { | ||
/// let name = ctx.param("name"); | ||
/// Ok(Response::new().text(format!("Hello, {name}!"))) | ||
/// }); | ||
/// # } | ||
/// ``` | ||
fn sync_route( | ||
&mut self, | ||
method: Method, | ||
path: impl AsRef<str>, | ||
handler: impl Fn(&Context<State>) -> AnyResult<Response> + Send + Sync + 'static, | ||
) -> &mut Self; | ||
} | ||
|
||
impl<State: Send + Sync> SyncRoute<State> for Server<State> { | ||
fn sync_route( | ||
&mut self, | ||
method: Method, | ||
path: impl AsRef<str>, | ||
handler: impl Fn(&Context<State>) -> AnyResult<Response> + Send + Sync + 'static, | ||
) -> &mut Self { | ||
trace!("{}Adding Route {} {}", emoji("🚗"), method, path.as_ref()); | ||
|
||
let handler = move |ctx: &Context<State>| Ok(ctx.with_response(handler(ctx)?).send()?); | ||
|
||
self.routes.push( | ||
Route::new(method, path.as_ref(), Box::new(handler)).expect("Error creating route."), | ||
); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters