diff options
| author | Fuwn <[email protected]> | 2022-03-27 00:05:52 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-03-27 00:05:52 +0000 |
| commit | 7ae322a1bb4ed93f20e98dcb3416057808dbd434 (patch) | |
| tree | c74def5a31df5e261236b7300d90390fb57a1258 /src | |
| parent | refactor(mount): context is now struct (diff) | |
| download | windmark-7ae322a1bb4ed93f20e98dcb3416057808dbd434.tar.xz windmark-7ae322a1bb4ed93f20e98dcb3416057808dbd434.zip | |
feat(router): modules
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 35 |
1 files changed, 30 insertions, 5 deletions
@@ -41,10 +41,11 @@ use std::{net::TcpStream, sync::Arc}; use matchit::Params; use openssl::ssl::{self, SslAcceptor, SslMethod}; +pub use response::Response; use url::Url; use crate::{ - response::{to_value_set_status, Response}, + response::to_value_set_status, returnable::{ErrorContext, RouteContext}, }; @@ -119,7 +120,7 @@ impl Router { /// # Examples /// /// ```rust - /// use windmark::response::Response; + /// use windmark::Response; /// /// windmark::Router::new() /// .mount("/", |_| Response::Success("This is the index page!".into())) @@ -147,9 +148,7 @@ impl Router { /// /// ```rust /// windmark::Router::new().set_error_handler(|_| { - /// windmark::response::Response::Success( - /// "You have encountered an error!".into(), - /// ) + /// windmark::Response::Success("You have encountered an error!".into()) /// }); /// ``` pub fn set_error_handler( @@ -439,6 +438,32 @@ impl Router { self } + + /// Attach a module to a `Router`. + /// + /// A module is an extension or middleware to a `Router`. Modules get full + /// access to the `Router`, but can be extended by a third party. + /// + /// # Examples + /// + /// ```rust + /// use windmark::Response; + /// + /// windmark::Router::new().attach(|r| { + /// r.mount("/module", |_| Response::Success("This is a module!".into())); + /// r.set_error_handler(|_| { + /// Response::NotFound( + /// "This error handler has been implemented by a module!".into(), + /// ) + /// }); + /// }); + /// ``` + pub fn attach<F>(&mut self, mut module: F) -> &mut Self + where F: FnMut(&mut Self) { + module(self); + + self + } } impl Default for Router { fn default() -> Self { |