pub struct App<Data> { /* fields omitted */ }
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.
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)]
struct Database {
contents: Arc<Mutex<Vec<String>>>,
}
impl Database {
fn new() -> Database {
Database {
contents: Arc::new(Mutex::new(Vec::new())),
}
}
}
async fn insert(
mut db: AppData<Database>,
msg: body::Str,
) -> String {
}
fn main() {
let mut app = tide::App::new(Database::new());
app.at("/messages/insert").post(insert);
app.serve()
}
Where to go from here: Please see Router
and Endpoint
for further examples.
Set up a new app with some initial data
.
Get the top-level router.
Add a new resource at path
.
See Router.at for details.
Set the default handler for the app, a fallback function when there is no match to the route requested
Apply middleware
to the whole app. Note that the order of nesting subrouters and applying
middleware matters; see Router
for details.
Add a default configuration item
for the whole app.
Start serving the app at the given address.
Blocks the calling thread indefinitely.
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static