Skip to content

Instantly share code, notes, and snippets.

@AndreasAakesson
Created October 5, 2016 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasAakesson/5823788da967616d54b3de6384fba949 to your computer and use it in GitHub Desktop.
Save AndreasAakesson/5823788da967616d54b3de6384fba949 to your computer and use it in GitHub Desktop.
mana::Server::process implementation
void Server::process(Request_ptr req, Response_ptr res) {
auto it_ptr = std::make_shared<MiddlewareStack::iterator>(middleware_.begin());
auto next = std::make_shared<next_t>();
auto weak_next = std::weak_ptr<next_t>(next);
// setup Next callback
*next = [this, it_ptr, weak_next, req, res]
{
// derefence the pointer to the iterator
auto& it = *it_ptr;
// skip those who don't match
while(it != middleware_.end() and !path_starts_with(req->uri().path(), it->path))
it++;
// while there is more to do
if(it != middleware_.end()) {
// dereference the function
auto& func = it->callback;
// advance the iterator for the next next-call
it++;
auto next = weak_next.lock(); // this should be safe since we're inside next
// execute the function
func(req, res, next);
}
// no more middleware, proceed with route processing
else {
process_route(req, res);
}
};
// get the party started..
(*next)();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment