-
Notifications
You must be signed in to change notification settings - Fork 321
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Application state is and always was shared within the application. Now that
Suggested change
|
||||||
/// | ||||||
/// Application scoped state is useful for storing items | ||||||
/// | ||||||
|
@@ -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 { | ||||||
|
@@ -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)) | ||||||
/// }); | ||||||
|
@@ -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()); | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.