Automatically redirect to https in production

* General refactoring of server middleware