pub struct Handler { /* private fields */ }Expand description
Configurable request handler with before/after middleware hooks.
§Example
ⓘ
use dioxus_cloudflare::Handler;
use worker::*;
#[event(fetch)]
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
unsafe { __wasm_call_ctors(); }
Handler::new()
.before(|req| {
// Short-circuit OPTIONS requests for CORS preflight
if req.method() == worker::Method::Options {
let mut resp = Response::empty()?;
resp.headers_mut().set("Access-Control-Allow-Origin", "*")?;
resp.headers_mut().set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")?;
return Ok(Some(resp));
}
Ok(None) // continue to Axum dispatch
})
.after(|resp| {
resp.headers_mut().set("Access-Control-Allow-Origin", "*")?;
Ok(())
})
.handle(req, env)
.await
}Implementations§
Source§impl Handler
impl Handler
Sourcepub fn before(
self,
hook: impl Fn(&Request) -> Result<Option<Response>> + 'static,
) -> Self
pub fn before( self, hook: impl Fn(&Request) -> Result<Option<Response>> + 'static, ) -> Self
Add a before-dispatch hook.
Runs after context is set (so cf::env(), cf::d1(), etc. work),
before Axum dispatch. Return Ok(None) to continue,
Ok(Some(resp)) to short-circuit with a custom response.
Hooks run in the order they were added.
Sourcepub fn after(self, hook: impl Fn(&mut Response) -> Result<()> + 'static) -> Self
pub fn after(self, hook: impl Fn(&mut Response) -> Result<()> + 'static) -> Self
Add an after-dispatch hook.
Runs after cookies are applied, before returning the response. Use this to add headers, log, or modify the final response.
After hooks also run on short-circuited responses from before hooks. Hooks run in the order they were added.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Handler
impl !RefUnwindSafe for Handler
impl !Send for Handler
impl !Sync for Handler
impl Unpin for Handler
impl UnsafeUnpin for Handler
impl !UnwindSafe for Handler
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
Mutably borrows from an owned value. Read more
Source§impl<T> InitializeFromFunction<T> for T
impl<T> InitializeFromFunction<T> for T
Source§fn initialize_from_function(f: fn() -> T) -> T
fn initialize_from_function(f: fn() -> T) -> T
Create an instance of this type from an initialization function
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<Ret> SpawnIfAsync<(), Ret> for Ret
impl<Ret> SpawnIfAsync<(), Ret> for Ret
Source§impl<T, O> SuperFrom<T> for Owhere
O: From<T>,
impl<T, O> SuperFrom<T> for Owhere
O: From<T>,
Source§fn super_from(input: T) -> O
fn super_from(input: T) -> O
Convert from a type to another type.
Source§impl<T, O, M> SuperInto<O, M> for Twhere
O: SuperFrom<T, M>,
impl<T, O, M> SuperInto<O, M> for Twhere
O: SuperFrom<T, M>,
Source§fn super_into(self) -> O
fn super_into(self) -> O
Convert from a type to another type.