-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add configuration types * Expose configuration to endpoints * Make configurations actually configurable * Use typemap approach for configuration * Add example for configuration * Change module organization of configuration * Add docs for configuration module * Add tests for Configuration, fix existing tests * Run rustfmt * Change how the configuration is nested * Add tests for Router with configuration * Add comments to configuration example * Change the name of `RequestConext::get_config` to get_config_item * Rename Configuration to Store and move it to configuration module. * This is because the Confiugration type is a App global sharable TypeMap which is avaiable across both middleware and endpoints. Therefore, this can be used to store arbitrary types and not only configuartion. * Tis commit renames all occurrances of Configuration to Store and updates the parameters and struct field names appropriately. * Add default configuration and configuration builder types * Have simple hook into the App by the way of the `setup_configuration` method. This is pretty basic rightn now, but can be the point of entry for hooking in custom configuration. * Simple useage of app configuration internally * Use address from confiugration when starting up server * Fix tests * Adress review comments. * Add more documentation for Configuration * Address clippy lints * Make naming consistent * Change name of Store tests * Add Debug impl for Store * Add configuration debug example
- Loading branch information
1 parent
5f4cc3d
commit 7139364
Showing
23 changed files
with
619 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![feature(async_await, futures_api)] | ||
|
||
use futures::future::FutureObj; | ||
use tide::{head::Path, middleware::RequestContext, ExtractConfiguration, Response}; | ||
|
||
/// A type that represents how much value will be added by the `add` handler. | ||
#[derive(Clone, Debug, Default)] | ||
struct IncreaseBy(i32); | ||
|
||
async fn add( | ||
Path(base): Path<i32>, | ||
// `ExtractConfiguration` will extract the configuration item of given type, and provide it as | ||
// `Option<T>`. If it is not set, the inner value will be `None`. | ||
ExtractConfiguration(amount): ExtractConfiguration<IncreaseBy>, | ||
) -> String { | ||
let IncreaseBy(amount) = amount.unwrap_or_default(); | ||
format!("{} plus {} is {}", base, amount, base + amount) | ||
} | ||
|
||
fn debug_store(ctx: RequestContext<()>) -> FutureObj<Response> { | ||
println!("{:#?}", ctx.store()); | ||
ctx.next() | ||
} | ||
|
||
fn main() { | ||
let mut app = tide::App::new(()); | ||
// `App::config` sets the default configuration of the app (that is, a top-level router). | ||
app.config(IncreaseBy(1)); | ||
app.middleware(debug_store); | ||
app.at("add_one/{}").get(add); // `IncreaseBy` is set to 1 | ||
app.at("add_two/{}").get(add).config(IncreaseBy(2)); // `IncreaseBy` is overridden to 2 | ||
|
||
app.serve(); | ||
} |
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
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
Oops, something went wrong.