Refactor prepare routes method

This PR refactors the prepare_routes method in Endpoint, extracting two methods to make it more readable.