Struct thruster::App

source ·
pub struct App<R: ThrusterRequest, T: 'static + Context + Clone + Send + Sync, S: Send> {
    pub delete_root: Node<T>,
    pub get_root: Node<T>,
    pub options_root: Node<T>,
    pub post_root: Node<T>,
    pub put_root: Node<T>,
    pub patch_root: Node<T>,
    pub context_generator: fn(_: R, _: &S, _: &str) -> T,
    pub state: Arc<S>,
    pub connection_timeout: u64,
}
Expand description

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. Build the app future with App.build and spawn it on the executor

Examples

Subapp

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§

§delete_root: Node<T>§get_root: Node<T>§options_root: Node<T>§post_root: Node<T>§put_root: Node<T>§patch_root: Node<T>§context_generator: fn(_: R, _: &S, _: &str) -> 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.

§state: Arc<S>§connection_timeout: u64

The connection timeout for the app in milliseconds. Defaults to 3600000ms (1 hour)

Implementations§

source§

impl<R: 'static + ThrusterRequest, T: Context + Clone + Send + Sync, S: 'static + Send> App<R, T, S>

source

pub fn new_basic() -> App<Request, 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.

source

pub fn create(generate_context: fn(_: R, _: &S, _: &str) -> T, state: S) -> Self

Create a new app with the given context generator. The app does not begin listening until start is called.

source

pub fn middleware(self, path: &str, middlewares: MiddlewareTuple<T>) -> Selfwhere T: Clone,

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.

source

pub fn use_middleware(self, path: &str, middlewares: MiddlewareTuple<T>) -> Selfwhere T: Clone,

👎Deprecated: use_middleware is deprecated. Please migrate to use middleware in the future.
source

pub fn router(self, prefix: &str, app: App<R, T, S>) -> Self

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.

source

pub fn use_sub_app(self, prefix: &str, app: App<R, T, S>) -> Self

👎Deprecated: use_sub_app is deprecated. Please migrate to use router in the future.
source

pub fn get(self, path: &str, middlewares: MiddlewareTuple<T>) -> Self

Add a route that responds to GETs to a given path

source

pub fn options(self, path: &str, middlewares: MiddlewareTuple<T>) -> Self

Add a route that responds to OPTIONs to a given path

source

pub fn post(self, path: &str, middlewares: MiddlewareTuple<T>) -> Self

Add a route that responds to POSTs to a given path

source

pub fn put(self, path: &str, middlewares: MiddlewareTuple<T>) -> Self

Add a route that responds to PUTs to a given path

source

pub fn delete(self, path: &str, middlewares: MiddlewareTuple<T>) -> Self

Add a route that responds to DELETEs to a given path

source

pub fn patch(self, path: &str, middlewares: MiddlewareTuple<T>) -> Self

Add a route that responds to PATCHs to a given path

source

pub fn set404(self, middlewares: MiddlewareTuple<T>) -> Selfwhere T: Clone,

Sets the middleware if no route is successfully matched. Note, that due to type restrictions, we Context needs to implement Clone in order to call this function, even though clone will never be called.

source

pub fn set_strict_mode(self, strict_mode: bool) -> Selfwhere T: Clone,

Sets whether this app router uses strict mode for route parsing or not. Strict mode considers /a to be distinct from /a/.

source

pub fn commit(self) -> Self

Commits and locks in the route tree for usage.

source

pub fn resolve_from_method_and_path<'m>( &'m self, method: &str, path: String ) -> NodeOutput<'m, T>

source

pub fn match_and_resolve<'m>( &'m self, request: R ) -> ReusableBoxFuture<Result<T::Response, Error>> where R: RequestWithParams,

source

pub async fn resolve<'m>( &self, request: R, matched_route: NodeOutput<'m, T> ) -> Result<T::Response, Error>

Auto Trait Implementations§

§

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

§

impl<R, T, S> Send for App<R, T, S>where S: Sync,

§

impl<R, T, S> Sync for App<R, T, S>where S: Sync,

§

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

§

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

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.