[][src]Struct thruster::App

pub struct App<R, T> where
    R: RequestWithParams,
    T: 'static + Send + Context
{ pub _route_parser: RouteParser<T>, pub context_generator: fn(R) -> T, }

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:

  1. Use App.create to create a new app with a custom context generator
  2. Add routes and middleware via .get, .post, etc.
  3. Start the app with App.start

Examples

Subapp

This example is not tested
let mut app1 = App::<Request, BasicContext>::new();

fn test_fn_1(context: BasicContext, next: impl Fn(BasicContext) -> MiddlewareReturnValue<BasicContext>  + Send) -> 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", middleware![BasicContext => test_fn_1]);

let mut app2 = App::<Request, 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.

The provided start methods are great places to start, but you can also simply use Thruster as a router and create your own version of an HTTP server by directly calling App.resolve with a Request object. It will then return a future with the Response object that corresponds to the request. This can be useful if trying to integrate with a different type of load balancing system within the threads of the application.

Fields

_route_parser: RouteParser<T>context_generator: fn(R) -> T

Generate context is common to all Apps. 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<R, T> App<R, T> where
    R: RequestWithParams,
    T: Send + Context
[src]

pub fn new_basic() -> App<Request, BasicContext>[src]

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(R) -> T) -> App<R, T>[src]

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: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

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<R, T>
) -> &mut App<R, T>
[src]

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]

Return the route parser for a given app

pub fn get(
    &mut self,
    path: &'static str,
    middlewares: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

Add a route that responds to GETs to a given path

pub fn options(
    &mut self,
    path: &'static str,
    middlewares: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

Add a route that responds to OPTIONs to a given path

pub fn post(
    &mut self,
    path: &'static str,
    middlewares: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

Add a route that responds to POSTs to a given path

pub fn put(
    &mut self,
    path: &'static str,
    middlewares: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

Add a route that responds to PUTs to a given path

pub fn delete(
    &mut self,
    path: &'static str,
    middlewares: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

Add a route that responds to DELETEs to a given path

pub fn update(
    &mut self,
    path: &'static str,
    middlewares: MiddlewareChain<T>
) -> &mut App<R, T>
[src]

Add a route that responds to UPDATEs to a given path

pub fn set404(&mut self, middlewares: MiddlewareChain<T>) -> &mut App<R, T>[src]

Sets the middleware if no route is successfully matched.

pub fn resolve_from_method_and_path(
    &self,
    method: &str,
    path: &str
) -> MatchedRoute<T>
[src]

pub fn resolve(
    &self,
    request: R,
    matched_route: MatchedRoute<T>
) -> impl Send + Future<Output = Result<<T as Context>::Response, Error>>
[src]

Auto Trait Implementations

impl<R, T> !RefUnwindSafe for App<R, T>

impl<R, T> Send for App<R, T>

impl<R, T> Sync for App<R, T>

impl<R, T> Unpin for App<R, T>

impl<R, T> !UnwindSafe for App<R, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.