Created
October 5, 2016 07:20
-
-
Save AndreasAakesson/5823788da967616d54b3de6384fba949 to your computer and use it in GitHub Desktop.
mana::Server::process implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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