Skip to content

Per-endpoint configuration #109

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

Merged
merged 28 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4f9bdd5
Add configuration types
tirr-c Dec 11, 2018
af5d370
Expose configuration to endpoints
tirr-c Dec 11, 2018
7ecfafe
Make configurations actually configurable
tirr-c Dec 11, 2018
5b54fc6
Use typemap approach for configuration
tirr-c Dec 12, 2018
2e4c928
Add example for configuration
tirr-c Dec 12, 2018
ce58d52
Change module organization of configuration
tirr-c Dec 12, 2018
e47776f
Add docs for configuration module
tirr-c Dec 14, 2018
e5b906d
Add tests for Configuration, fix existing tests
tirr-c Dec 14, 2018
f3789d7
Run rustfmt
tirr-c Dec 14, 2018
702a4e6
Change how the configuration is nested
tirr-c Dec 14, 2018
959713d
Add tests for Router with configuration
tirr-c Dec 14, 2018
06cfc45
Add comments to configuration example
tirr-c Dec 15, 2018
5329b01
Change the name of `RequestConext::get_config` to get_config_item
tirr-c Dec 15, 2018
3f1797e
Rename Configuration to Store and move it to configuration module.
bIgBV Dec 16, 2018
2eb5dfc
Add default configuration and configuration builder types
bIgBV Dec 17, 2018
c00e87b
Simple useage of app configuration internally
bIgBV Dec 17, 2018
c9dd3bb
Use address from confiugration when starting up server
bIgBV Dec 17, 2018
b7ced5a
Fix tests
bIgBV Dec 17, 2018
9255d0a
Adress review comments.
bIgBV Dec 19, 2018
b8ada8f
Merge pull request #1 from bIgBV/default-configuration
tirr-c Jan 9, 2019
1c80255
Add more documentation for Configuration
tirr-c Jan 9, 2019
5bc825a
Merge remote-tracking branch 'upstream/master' into configuration
tirr-c Jan 9, 2019
85e5eb6
Address clippy lints
tirr-c Jan 9, 2019
a3cb7ff
Make naming consistent
tirr-c Jan 10, 2019
73769de
Change name of Store tests
tirr-c Jan 10, 2019
262985a
Add Debug impl for Store
tirr-c Jan 10, 2019
e705b58
Add configuration debug example
tirr-c Jan 10, 2019
34486c2
Merge remote-tracking branch 'upstream/master' into configuration
tirr-c Jan 16, 2019
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
29 changes: 29 additions & 0 deletions examples/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![feature(async_await, futures_api)]

use tide::{head::Path, ExtractConfiguration};

/// A type that represents how much value will be added by the `add` handler.
#[derive(Clone, 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 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.at("add_one/{}").get(add); // `IncreaseBy` is set to 1
app.at("add_two/{}").get(add).config(IncreaseBy(2)); // `IncreaseBy` is overridden to 2

let address = "127.0.0.1:8000".to_owned();
println!("Server is listening on http://{}", address);
app.serve(address);
}
42 changes: 33 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ use futures::{
};
use hyper::service::Service;
use std::{
any::Any,
ops::{Deref, DerefMut},
sync::Arc,
};

use crate::{
body::Body,
configuration::Configuration,
endpoint::BoxedEndpoint,
endpoint::Endpoint,
extract::Extract,
middleware::{logger::RootLogger, RequestContext},
router::{Resource, RouteResult, Router},
router::{EndpointData, Resource, RouteResult, Router},
Middleware, Request, Response, RouteMatch,
};

Expand Down Expand Up @@ -84,7 +86,7 @@ use crate::{
pub struct App<Data> {
data: Data,
router: Router<Data>,
default_handler: BoxedEndpoint<Data>,
default_handler: EndpointData<Data>,
}

impl<Data: Clone + Send + Sync + 'static> App<Data> {
Expand All @@ -94,7 +96,10 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
let mut app = App {
data,
router: Router::new(),
default_handler: BoxedEndpoint::new(async || http::status::StatusCode::NOT_FOUND),
default_handler: EndpointData {
endpoint: BoxedEndpoint::new(async || http::status::StatusCode::NOT_FOUND),
config: Configuration::new(),
},
};

// Add RootLogger as a default middleware
Expand All @@ -114,9 +119,16 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
}

/// Set the default handler for the app, a fallback function when there is no match to the route requested
pub fn default_handler<T: Endpoint<Data, U>, U>(&mut self, handler: T) -> &mut Self {
self.default_handler = BoxedEndpoint::new(handler);
self
pub fn default_handler<T: Endpoint<Data, U>, U>(
&mut self,
handler: T,
) -> &mut EndpointData<Data> {
let endpoint = EndpointData {
endpoint: BoxedEndpoint::new(handler),
config: self.router.config_base.clone(),
};
self.default_handler = endpoint;
&mut self.default_handler
}

/// Apply `middleware` to the whole app. Note that the order of nesting subrouters and applying
Expand All @@ -126,7 +138,14 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
self
}

fn into_server(self) -> Server<Data> {
/// Add a default configuration `item` for the whole app.
pub fn config<T: Any + Clone + Send + Sync>(&mut self, item: T) -> &mut Self {
self.router.config(item);
self
}

fn into_server(mut self) -> Server<Data> {
self.router.apply_default_config();
Server {
data: self.data,
router: Arc::new(self.router),
Expand Down Expand Up @@ -162,7 +181,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
struct Server<Data> {
data: Data,
router: Arc<Router<Data>>,
default_handler: Arc<BoxedEndpoint<Data>>,
default_handler: Arc<EndpointData<Data>>,
}

impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
Expand Down Expand Up @@ -223,7 +242,12 @@ impl<T> DerefMut for AppData<T> {

impl<T: Clone + Send + 'static> Extract<T> for AppData<T> {
type Fut = future::Ready<Result<Self, Response>>;
fn extract(data: &mut T, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut T,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
future::ok(AppData(data.clone()))
}
}
44 changes: 37 additions & 7 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use pin_utils::pin_mut;
use std::io::Cursor;
use std::ops::{Deref, DerefMut};

use crate::{Extract, IntoResponse, Request, Response, RouteMatch};
use crate::{configuration::Configuration, Extract, IntoResponse, Request, Response, RouteMatch};

/// The raw contents of an http request or response.
///
Expand Down Expand Up @@ -202,7 +202,12 @@ impl<S: 'static> Extract<S> for MultipartForm {
// Note: cannot use `existential type` here due to ICE
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(data: &mut S, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
// https://stackoverflow.com/questions/43424982/how-to-parse-multipart-forms-using-abonander-multipart-with-rocket

const BOUNDARY: &str = "boundary=";
Expand Down Expand Up @@ -248,7 +253,12 @@ impl<T: Send + serde::de::DeserializeOwned + 'static, S: 'static> Extract<S> for
// Note: cannot use `existential type` here due to ICE
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(data: &mut S, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
let mut body = std::mem::replace(req.body_mut(), Body::empty());
FutureObj::new(Box::new(
async move {
Expand Down Expand Up @@ -295,7 +305,12 @@ impl<T: Send + serde::de::DeserializeOwned + 'static, S: 'static> Extract<S> for
// Note: cannot use `existential type` here due to ICE
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(data: &mut S, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
let mut body = std::mem::replace(req.body_mut(), Body::empty());
FutureObj::new(Box::new(
async move {
Expand Down Expand Up @@ -338,7 +353,12 @@ pub struct Str(pub String);
impl<S: 'static> Extract<S> for Str {
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(data: &mut S, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
let mut body = std::mem::replace(req.body_mut(), Body::empty());

FutureObj::new(Box::new(
Expand Down Expand Up @@ -369,7 +389,12 @@ pub struct StrLossy(pub String);
impl<S: 'static> Extract<S> for StrLossy {
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(data: &mut S, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
let mut body = std::mem::replace(req.body_mut(), Body::empty());

FutureObj::new(Box::new(
Expand Down Expand Up @@ -400,7 +425,12 @@ pub struct Bytes(pub Vec<u8>);
impl<S: 'static> Extract<S> for Bytes {
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(data: &mut S, req: &mut Request, params: &Option<RouteMatch<'_>>) -> Self::Fut {
fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
let mut body = std::mem::replace(req.body_mut(), Body::empty());

FutureObj::new(Box::new(
Expand Down
128 changes: 128 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//! Types for managing and extracting configuration.

use std::any::{Any, TypeId};
use std::collections::HashMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible performance improvement here would be to use the hashbrown crate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that currently being evaluated to be used as the default HashMap implementation in the std? If that's the case then do you think it's a good idea to replace it right now? (Though to be fair, it might be at least a few releases away..)

Copy link
Collaborator Author

@tirr-c tirr-c Jan 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I heard that hashbrown is used only in rustc, not libstd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I missed this. But yeah, it's landing in libstd; people made a PR during RustFest Rome for this!


use futures::future::FutureObj;

use crate::{Extract, Request, Response, RouteMatch};

trait ConfigurationItem: Any + Send + Sync {
fn clone_any(&self) -> Box<dyn ConfigurationItem>;
fn as_dyn_any(&self) -> &(dyn Any + Send + Sync);
fn as_dyn_any_mut(&mut self) -> &mut (dyn Any + Send + Sync);
}

impl<T> ConfigurationItem for T
where
T: Any + Clone + Send + Sync,
{
fn clone_any(&self) -> Box<dyn ConfigurationItem> {
Box::new(self.clone())
}

fn as_dyn_any(&self) -> &(dyn Any + Send + Sync) {
self
}

fn as_dyn_any_mut(&mut self) -> &mut (dyn Any + Send + Sync) {
self
}
}

impl Clone for Box<dyn ConfigurationItem> {
fn clone(&self) -> Box<dyn ConfigurationItem> {
(&**self).clone_any()
}
}

/// A cloneable typemap for saving per-endpoint configuration.
///
/// Configuration is mostly managed by `App` and `Router`, so this is normally not used directly.
#[derive(Clone)]
pub struct Configuration(HashMap<TypeId, Box<dyn ConfigurationItem>>);

impl Configuration {
pub(crate) fn new() -> Self {
Configuration(HashMap::new())
}

pub(crate) fn merge(&mut self, base: &Configuration) {
let overlay = std::mem::replace(&mut self.0, base.0.clone());
self.0.extend(overlay);
}

/// Retrieve the configuration item of given type, returning `None` if it is not found.
pub fn read<T: Any + Clone + Send + Sync>(&self) -> Option<&T> {
let id = TypeId::of::<T>();
self.0
.get(&id)
.and_then(|v| (**v).as_dyn_any().downcast_ref::<T>())
}

/// Save the given configuration item.
pub fn write<T: Any + Clone + Send + Sync>(&mut self, value: T) {
let id = TypeId::of::<T>();
self.0
.insert(id, Box::new(value) as Box<dyn ConfigurationItem>);
}
}

/// An extractor for reading configuration from endpoints.
///
/// It will try to retrieve the given configuration item. If it is not set, the extracted value
/// will be `None`.
pub struct ExtractConfiguration<T>(pub Option<T>);

impl<S: 'static, T: Any + Clone + Send + Sync + 'static> Extract<S> for ExtractConfiguration<T> {
type Fut = FutureObj<'static, Result<Self, Response>>;

fn extract(
data: &mut S,
req: &mut Request,
params: &Option<RouteMatch<'_>>,
config: &Configuration,
) -> Self::Fut {
let config_item = config.read().cloned();
FutureObj::new(Box::new(
async move { Ok(ExtractConfiguration(config_item)) },
))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn configuration_read_write() {
let mut config = Configuration::new();
assert_eq!(config.read::<usize>(), None);
assert_eq!(config.read::<isize>(), None);
config.write(42usize);
config.write(-3isize);
assert_eq!(config.read::<usize>(), Some(&42));
assert_eq!(config.read::<isize>(), Some(&-3));
config.write(3usize);
assert_eq!(config.read::<usize>(), Some(&3));
}

#[test]
fn configuration_clone() {
let mut config = Configuration::new();
config.write(42usize);
config.write(String::from("foo"));

let mut new_config = config.clone();
new_config.write(3usize);
new_config.write(4u32);

assert_eq!(config.read::<usize>(), Some(&42));
assert_eq!(config.read::<u32>(), None);
assert_eq!(config.read::<String>(), Some(&"foo".into()));

assert_eq!(new_config.read::<usize>(), Some(&3));
assert_eq!(new_config.read::<u32>(), Some(&4));
assert_eq!(new_config.read::<String>(), Some(&"foo".into()));
}
}
Loading