pub struct App<R = (), CF = ()> { /* private fields */ }Expand description
composed application type with router, stateful context and default middlewares.
Implementations§
source§impl<Obj, CF> App<AppRouter<Obj>, CF>
impl<Obj, CF> App<AppRouter<Obj>, CF>
sourcepub fn at<F, C, B>(self, path: &'static str, builder: F) -> Selfwhere
F: RouterGen + Service + Send + Sync,
F::Response: for<'r> Service<WebContext<'r, C, B>>,
for<'r> WebContext<'r, C, B>: IntoObject<F::Route<F>, (), Object = Obj>,
pub fn at<F, C, B>(self, path: &'static str, builder: F) -> Selfwhere
F: RouterGen + Service + Send + Sync,
F::Response: for<'r> Service<WebContext<'r, C, B>>,
for<'r> WebContext<'r, C, B>: IntoObject<F::Route<F>, (), Object = Obj>,
insert routed service with given path to application.
source§impl<R, CF> App<R, CF>
impl<R, CF> App<R, CF>
sourcepub fn with_state<C>(
self,
state: C
) -> App<R, Box<dyn Fn() -> Pin<Box<dyn Future<Output = Result<C, Box<dyn Debug>>>>> + Send + Sync>>
pub fn with_state<C>( self, state: C ) -> App<R, Box<dyn Fn() -> Pin<Box<dyn Future<Output = Result<C, Box<dyn Debug>>>>> + Send + Sync>>
Construct App with a thread safe state that will be shared among all tasks and worker threads.
State accessing is based on generic type approach where the State type and it’s typed fields are generally opaque to middleware and routing services of the application. In order to cast concrete type from generic state type std::borrow::Borrow trait is utilized. See example below for explanation.
§Example
// our typed state.
#[derive(Clone, Default)]
struct State {
string: String,
usize: usize
}
// implement Borrow trait to enable borrowing &String type from State.
impl Borrow<String> for State {
fn borrow(&self) -> &String {
&self.string
}
}
App::new()
.with_state(State::default())// construct app with state type.
.at("/", handler_service(index)) // a function service that have access to state.
.enclosed_fn(middleware_fn); // a function middleware that have access to state
// the function service don't know what the real type of application state is.
// it only needs to know &String can be borrowed from it.
async fn index(_: StateRef<'_, String>) -> &'static str {
""
}
// similar to function service. the middleware does not need to know the real type of C.
// it only needs to know it implement according trait.
async fn middleware_fn<S, C, Res>(service: &S, ctx: WebContext<'_, C>) -> Result<Res, Error<C>>
where
S: for<'r> Service<WebContext<'r, C>, Response = Res, Error = Error<C>>,
C: Borrow<String> // annotate we want to borrow &String from generic C state type.
{
// WebContext::state would return &C then we can call Borrow::borrow on it to get &String
let _string = ctx.state().borrow();
// or use extractor manually like in function service.
let _string = StateRef::<'_, String>::from_request(&ctx).await?;
service.call(ctx).await
}sourcepub fn with_async_state<CF1, Fut, C, E>(
self,
builder: CF1
) -> App<R, Box<dyn Fn() -> Pin<Box<dyn Future<Output = Result<C, Box<dyn Debug>>>>> + Send + Sync>>
pub fn with_async_state<CF1, Fut, C, E>( self, builder: CF1 ) -> App<R, Box<dyn Fn() -> Pin<Box<dyn Future<Output = Result<C, Box<dyn Debug>>>>> + Send + Sync>>
Construct App with async closure which it’s output would be used as state.
async state is used to produce thread per core and/or non thread safe state copies.
The output state is not bound to Send and Sync auto traits.
source§impl<R, CF> App<R, CF>
impl<R, CF> App<R, CF>
sourcepub fn enclosed<T>(self, transform: T) -> App<EnclosedBuilder<R, T>, CF>
pub fn enclosed<T>(self, transform: T) -> App<EnclosedBuilder<R, T>, CF>
Enclose App with middleware type. Middleware must impl Service trait.
sourcepub fn enclosed_fn<Req, T>(
self,
transform: T
) -> App<EnclosedFnBuilder<R, T>, CF>
pub fn enclosed_fn<Req, T>( self, transform: T ) -> App<EnclosedFnBuilder<R, T>, CF>
Enclose App with function as middleware type.
source§impl<R, CF> App<R, CF>
impl<R, CF> App<R, CF>
sourcepub fn finish<C, ResB, SE>(
self
) -> impl Service<Response = impl ReadyService + Service<WebRequest, Response = WebResponse<Either<ResB, ResponseBody>>, Error = Infallible>, Error = impl Debug>where
R::Response: ReadyService + for<'r> Service<WebContext<'r, C>, Response = WebResponse<ResB>, Error = SE>,
SE: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible>,
CF: IntoCtx<C>,
C: 'static,
pub fn finish<C, ResB, SE>(
self
) -> impl Service<Response = impl ReadyService + Service<WebRequest, Response = WebResponse<Either<ResB, ResponseBody>>, Error = Infallible>, Error = impl Debug>where
R::Response: ReadyService + for<'r> Service<WebContext<'r, C>, Response = WebResponse<ResB>, Error = SE>,
SE: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible>,
CF: IntoCtx<C>,
C: 'static,
Finish App build. No other App method can be called afterwards.
sourcepub fn finish_boxed<C, ResB, SE, BE>(
self
) -> AppObject<impl ReadyService + Service<WebRequest, Response = WebResponse, Error = Infallible>>where
R: 'static,
R::Response: ReadyService + for<'r> Service<WebContext<'r, C>, Response = WebResponse<ResB>, Error = SE> + 'static,
SE: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible> + 'static,
ResB: Stream<Item = Result<Bytes, BE>> + 'static,
BE: Error + Send + Sync + 'static,
CF: IntoCtx<C> + 'static,
C: 'static,
pub fn finish_boxed<C, ResB, SE, BE>(
self
) -> AppObject<impl ReadyService + Service<WebRequest, Response = WebResponse, Error = Infallible>>where
R: 'static,
R::Response: ReadyService + for<'r> Service<WebContext<'r, C>, Response = WebResponse<ResB>, Error = SE> + 'static,
SE: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible> + 'static,
ResB: Stream<Item = Result<Bytes, BE>> + 'static,
BE: Error + Send + Sync + 'static,
CF: IntoCtx<C> + 'static,
C: 'static,
Finish App build. No other App method can be called afterwards.
sourcepub fn serve<C, ResB, SE>(
self
) -> HttpServer<impl Service<Response = impl ReadyService + Service<WebRequest, Response = WebResponse<Either<ResB, ResponseBody>>, Error = Infallible>, Error = impl Debug>>where
R: 'static,
R::Response: ReadyService + for<'r> Service<WebContext<'r, C>, Response = WebResponse<ResB>, Error = SE>,
SE: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible> + 'static,
ResB: 'static,
CF: IntoCtx<C> + 'static,
C: 'static,
pub fn serve<C, ResB, SE>(
self
) -> HttpServer<impl Service<Response = impl ReadyService + Service<WebRequest, Response = WebResponse<Either<ResB, ResponseBody>>, Error = Infallible>, Error = impl Debug>>where
R: 'static,
R::Response: ReadyService + for<'r> Service<WebContext<'r, C>, Response = WebResponse<ResB>, Error = SE>,
SE: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible> + 'static,
ResB: 'static,
CF: IntoCtx<C> + 'static,
C: 'static,
Finish App build and serve is with HttpServer. No other App method can be called afterwards.
Trait Implementations§
Auto Trait Implementations§
impl<R, CF> RefUnwindSafe for App<R, CF>where
CF: RefUnwindSafe,
R: RefUnwindSafe,
impl<R, CF> Send for App<R, CF>
impl<R, CF> Sync for App<R, CF>
impl<R, CF> Unpin for App<R, CF>
impl<R, CF> UnwindSafe for App<R, CF>where
CF: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<S, Arg> ServiceExt<Arg> for Swhere
S: Service<Arg>,
impl<S, Arg> ServiceExt<Arg> for Swhere
S: Service<Arg>,
source§fn enclosed<T>(self, build: T) -> Pipeline<Self, T, BuildEnclosed>
fn enclosed<T>(self, build: T) -> Pipeline<Self, T, BuildEnclosed>
T as Service<<Self as Service<_>>::Response>>. In other word T
would take Self’s Service::Response type as it’s generic argument of Service<_> impl.source§fn enclosed_fn<T, Req>(self, func: T) -> Pipeline<Self, T, BuildEnclosedFn>
fn enclosed_fn<T, Req>(self, func: T) -> Pipeline<Self, T, BuildEnclosedFn>
source§fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, F, BuildMap>
fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, F, BuildMap>
<<Self::Response as Service<Req>>::Future as Future>::Output type with given
closure.