Refactor middleware so they are usable in the routes config as well