Skip to main content

trillium_api/
before_send.rs

1use trillium::{Conn, Handler};
2use trillium_macros::Handler;
3
4/// Run a [`trillium::Handler`] in `before_send`
5///
6/// Wrap a handler with this type to call its `run` callback in `before_send` allowing it to execute
7/// even if the conn has been halted. Note that it will be called later in the handler sequence as a
8/// result of this.
9#[derive(Debug, Handler)]
10pub struct BeforeSend<H>(#[handler(except = [run, before_send])] pub H);
11impl<H: Handler> BeforeSend<H> {
12    async fn run(&self, conn: Conn) -> Conn {
13        conn
14    }
15
16    async fn before_send(&self, mut conn: Conn) -> Conn {
17        let halted = conn.is_halted();
18        conn.set_halted(false);
19        let mut conn = self.0.run(conn).await;
20        conn.set_halted(halted || conn.is_halted());
21        self.0.before_send(conn).await
22    }
23}