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

Server: add with_shared_state() #642

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 45 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
#![doc(html_favicon_url = "https://yoshuawuyts.com/assets/http-rs/favicon.ico")]
#![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")]

use std::sync::Arc;

mod cookies;
mod endpoint;
mod fs;
Expand Down Expand Up @@ -245,7 +247,7 @@ pub fn new() -> server::Server<()> {
Server::new()
}

/// Create a new Tide server with shared application scoped state.
/// Create a new Tide server with application scoped state.
///
/// Application scoped state is useful for storing items
///
Expand Down Expand Up @@ -283,5 +285,47 @@ where
Server::with_state(state)
}

/// Create a new Tide server with shared application scoped state.
///
/// Shared application scoped state is useful for storing items,
/// including across multiple tide applications.
///
/// # Examples
///
/// ```no_run
/// # use async_std::task::block_on;
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
/// #
/// use std::sync::Arc;
/// use tide::Request;
///
/// /// The shared application state.
/// struct State {
/// name: String,
/// }
///
/// // Define a new instance of the state.
/// let state = Arc::new(State {
/// name: "Nori".to_string()
/// });
///
/// // Initialize the application with state.
/// let mut app1 = tide::with_shared_state(state.clone());
/// let mut app2 = tide::with_shared_state(state.clone());
/// app2.at("/name").get(|req: Request<State>| async move {
/// Ok(format!("Hello, {}!", &req.state().name))
/// });
/// app1.at("/hello").nest(app2);
/// app1.listen("127.0.0.1:8080/hello/name").await?;
/// #
/// # Ok(()) }) }
/// ```
pub fn with_shared_state<State>(state: Arc<State>) -> server::Server<State>
where
State: Send + Sync + 'static,
{
Server::with_shared_state(state)
}

Comment on lines +288 to +329
Copy link
Member

Choose a reason for hiding this comment

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

Now that State: Clone this method is no longer needed.

Suggested change
/// Create a new Tide server with shared application scoped state.
///
/// Shared application scoped state is useful for storing items,
/// including across multiple tide applications.
///
/// # Examples
///
/// ```no_run
/// # use async_std::task::block_on;
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
/// #
/// use std::sync::Arc;
/// use tide::Request;
///
/// /// The shared application state.
/// struct State {
/// name: String,
/// }
///
/// // Define a new instance of the state.
/// let state = Arc::new(State {
/// name: "Nori".to_string()
/// });
///
/// // Initialize the application with state.
/// let mut app1 = tide::with_shared_state(state.clone());
/// let mut app2 = tide::with_shared_state(state.clone());
/// app2.at("/name").get(|req: Request<State>| async move {
/// Ok(format!("Hello, {}!", &req.state().name))
/// });
/// app1.at("/hello").nest(app2);
/// app1.listen("127.0.0.1:8080/hello/name").await?;
/// #
/// # Ok(()) }) }
/// ```
pub fn with_shared_state<State>(state: Arc<State>) -> server::Server<State>
where
State: Send + Sync + 'static,
{
Server::with_shared_state(state)
}

/// A specialized Result type for Tide.
pub type Result<T = Response> = std::result::Result<T, Error>;
47 changes: 43 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Default for Server<()> {
}

impl<State: Send + Sync + 'static> Server<State> {
/// Create a new Tide server with shared application scoped state.
/// Create a new Tide server with application scoped state.
Copy link
Member

Choose a reason for hiding this comment

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

Application state is and always was shared within the application. Now that State: Clone it's true both locally to applications, and between applications.

Suggested change
/// Create a new Tide server with application scoped state.
/// Create a new Tide server shared with application scoped state.

///
/// Application scoped state is useful for storing items
///
Expand All @@ -177,7 +177,7 @@ impl<State: Send + Sync + 'static> Server<State> {
/// # use async_std::task::block_on;
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
/// #
/// use tide::Request;
/// use tide::{Server, Request};
///
/// /// The shared application state.
/// struct State {
Expand All @@ -190,7 +190,7 @@ impl<State: Send + Sync + 'static> Server<State> {
/// };
///
/// // Initialize the application with state.
/// let mut app = tide::with_state(state);
/// let mut app = Server::with_state(state);
/// app.at("/").get(|req: Request<State>| async move {
/// Ok(format!("Hello, {}!", &req.state().name))
/// });
Expand All @@ -199,10 +199,49 @@ impl<State: Send + Sync + 'static> Server<State> {
/// # Ok(()) }) }
/// ```
pub fn with_state(state: State) -> Self {
Self::with_shared_state(Arc::new(state))
}

/// Create a new Tide server with shared application scoped state.
///
/// Shared application scoped state is useful for storing items,
/// including across multiple tide applications.
///
/// # Examples
///
/// ```no_run
/// # use async_std::task::block_on;
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
/// #
/// use std::sync::Arc;
/// use tide::{Server, Request};
///
/// /// The shared application state.
/// struct State {
/// name: String,
/// }
///
/// // Define a new instance of the state.
/// let state = Arc::new(State {
/// name: "Nori".to_string()
/// });
///
/// // Initialize the application with state.
/// let mut app1 = Server::with_shared_state(state.clone());
/// let mut app2 = Server::with_shared_state(state.clone());
/// app2.at("/name").get(|req: Request<State>| async move {
/// Ok(format!("Hello, {}!", &req.state().name))
/// });
/// app1.at("/hello").nest(app2);
/// app1.listen("127.0.0.1:8080/hello/name").await?;
/// #
/// # Ok(()) }) }
/// ```
pub fn with_shared_state(state: Arc<State>) -> Self {
let mut server = Self {
router: Arc::new(Router::new()),
middleware: Arc::new(vec![]),
state: Arc::new(state),
state,
};
server.middleware(cookies::CookiesMiddleware::new());
server.middleware(log::LogMiddleware::new());
Expand Down