[−][src]Struct tide::App
The top-level type for setting up a Tide application.
Apps are equipped with a handle to their own state (Data
), which is available to all endpoints.
This is a "handle" because it must be Clone
, and endpoints are invoked with a fresh clone.
They also hold a top-level router.
Examples
You can start a simple Tide application that listens for GET
requests at path /hello
on 127.0.0.1:7878
with:
#![feature(async_await)] let mut app = tide::App::new(()); app.at("/hello").get(async || "Hello, world!"); app.serve()
App
state can be modeled with an underlying Data
handle for a cloneable type T
. Endpoints
can receive a fresh clone of that handle (in addition to data extracted from the request) by
defining a parameter of type AppData<T>
:
#![feature(async_await, futures_api)] use std::sync::Arc; use std::sync::Mutex; use tide::AppData; use tide::body; #[derive(Clone, Default)] struct Database { contents: Arc<Mutex<Vec<String>>>, } async fn insert( mut db: AppData<Database>, msg: body::Str, ) -> String { // insert into db } fn main() { let mut app = tide::App::new(Database::default()); app.at("/messages/insert").post(insert); app.serve() }
Where to go from here: Please see Router
and Endpoint
for further examples.
Methods
impl<Data: Clone + Send + Sync + 'static> App<Data>
[src]
pub fn new(data: Data) -> App<Data>
[src]
Set up a new app with some initial data
.
pub fn router(&mut self) -> &mut Router<Data>
[src]
Get the top-level router.
pub fn at<'a>(&'a mut self, path: &'a str) -> Resource<'a, Data>
[src]
Add a new resource at path
.
See Router.at for details.
pub fn default_handler<T: Endpoint<Data, U>, U>(
&mut self,
handler: T
) -> &mut EndpointData<Data>
[src]
&mut self,
handler: T
) -> &mut EndpointData<Data>
Set the default handler for the app, a fallback function when there is no match to the route requested
pub fn middleware(
&mut self,
middleware: impl Middleware<Data> + 'static
) -> &mut Self
[src]
&mut self,
middleware: impl Middleware<Data> + 'static
) -> &mut Self
Apply middleware
to the whole app. Note that the order of nesting subrouters and applying
middleware matters; see Router
for details.
pub fn config<T: Any + Debug + Clone + Send + Sync>(
&mut self,
item: T
) -> &mut Self
[src]
&mut self,
item: T
) -> &mut Self
Add a default configuration item
for the whole app.
pub fn get_item<T: Any + Debug + Clone + Send + Sync>(&self) -> Option<&T>
[src]
pub fn serve(self)
[src]
Start serving the app at the given address.
Blocks the calling thread indefinitely.
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.