Struct thruster::App [−][src]
pub struct App<T: 'static + Context + Send> { pub context_generator: fn(_: Request) -> T, // some fields omitted }
App, the main component of Thruster. The App is the entry point for your application
and handles all incomming requests. Apps are also composeable, that is, via the subapp
method, you can use all of the methods and middlewares contained within an app as a subset
of your app's routes.
There are three main parts to creating a thruster app:
- Use
App.create
to create a new app with a custom context generator - Add routes and middleware via
.get
,.post
, etc. - Start the app with
App.start
Examples
Subapp
let mut app1 = App::<BasicContext>::new(); fn test_fn_1(context: BasicContext, _chain: &MiddlewareChain<BasicContext>) -> MiddlewareReturnValue<BasicContext> { Box::new(future::ok(BasicContext { body: context.params.get("id").unwrap().to_owned(), params: context.params, query_params: context.query_params })) }; app1.get("/:id", vec![test_fn_1]); let mut app2 = App::<BasicContext>::new(); app2.use_sub_app("/test", &app1);
In the above example, the route /test/some-id
will return some-id
in the body of the response.
Fields
context_generator: fn(_: Request) -> T
Generate context is common to all App
s. It's the function that's called upon receiving a request
that translates an acutal Request
struct to your custom Context type. It should be noted that
the context_generator should be as fast as possible as this is called with every request, including
404s.
Methods
impl<T: Context + Send> App<T>
[src]
impl<T: Context + Send> App<T>
pub fn start(app: App<T>, host: &str, port: u16)
[src]
pub fn start(app: App<T>, host: &str, port: u16)
pub fn new() -> App<BasicContext>
[src]
pub fn new() -> App<BasicContext>
Creates a new instance of app with the library supplied BasicContext
. Useful for trivial
examples, likely not a good solution for real code bases. The advantage is that the
context_generator is already supplied for the developer.
pub fn create(generate_context: fn(_: Request) -> T) -> App<T>
[src]
pub fn create(generate_context: fn(_: Request) -> T) -> App<T>
Create a new app with the given context generator. The app does not begin listening until start is called.
pub fn use_middleware(
&mut self,
path: &'static str,
middleware: Middleware<T>
) -> &mut App<T>
[src]
pub fn use_middleware(
&mut self,
path: &'static str,
middleware: Middleware<T>
) -> &mut App<T>
Add method-agnostic middleware for a route. This is useful for applying headers, logging, and anything else that might not be sensitive to the HTTP method for the endpoint.
pub fn use_sub_app(&mut self, prefix: &'static str, app: App<T>) -> &mut App<T>
[src]
pub fn use_sub_app(&mut self, prefix: &'static str, app: App<T>) -> &mut App<T>
Add an app as a predetermined set of routes and middleware. Will prefix whatever string is passed in to all of the routes. This is a main feature of Thruster, as it allows projects to be extermely modular and composeable in nature.
pub fn get_route_parser(&self) -> &RouteParser<T>
[src]
pub fn get_route_parser(&self) -> &RouteParser<T>
Return the route parser for a given app
pub fn get(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
[src]
pub fn get(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
Add a route that responds to GET
s to a given path
pub fn post(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
[src]
pub fn post(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
Add a route that responds to POST
s to a given path
pub fn put(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
[src]
pub fn put(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
Add a route that responds to PUT
s to a given path
pub fn delete(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
[src]
pub fn delete(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
Add a route that responds to DELETE
s to a given path
pub fn update(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
[src]
pub fn update(
&mut self,
path: &'static str,
middlewares: Vec<Middleware<T>>
) -> &mut App<T>
Add a route that responds to UPDATE
s to a given path
pub fn set404(&mut self, middlewares: Vec<Middleware<T>>) -> &mut App<T>
[src]
pub fn set404(&mut self, middlewares: Vec<Middleware<T>>) -> &mut App<T>
Sets the middleware if no route is successfully matched.