Add a chain/sequence handler
Closed this issue · 2 comments
Something similar to what was briefly discussed in the comments to #6. It should allow multiple small handlers, like authorization or database preparation, to be chained together in a sequence of actions. Some kind of message passing to the next handler, other than (ab)using the filter storage, might also be useful. I would imagine something along the line of this:
pub trait SequenceHandler {
type State;
fn handle_request(&self, &Context, State, SecuenceResponse) -> SequenceAction;
}
pub struct Sequence<F: Fn(&Context) -> H::State, H: SequenceHandler> {
pub init: F,
pub handlers: Vec<H>
}
impl<F: Fn(&Context) -> H::State, H: SequenceHandler> Handler for Sequence<F, H> {
fn handle_request(&self, context: Context, response: Response) {
let state = self.init(&context);
let response = SequenceResponse::Response(response);
for handler in &self.handlers {
match handler.handle_request(&context, state, response) {
//...
}
}
}
}
Where SequenceResponse
is an enum containing a Response
or a ResponseWriter
and SequenceAction
can abort the sequence. This is just a sketch (F
and H
would, for example, also have to implement Send + Sync
) to illustrate what I mean. The init
function could also be replaced by SequenceHandler
implementing Default
, but that may also be too restricting.
I'm honestly not sure how to proceed with this one, or if it even belongs in this library. It could just as well have its own library. Is this kind of functionality even desired? If so, how would you want it to work? What do you want it to be able to do?
I'll close this because of inactivity. It can be reopened again if someone happens to be interested.