diff --git a/robyn/__init__.py b/robyn/__init__.py index 83ddbea8e..268be447d 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -224,6 +224,22 @@ def is_port_in_use(self, port: int) -> bool: except Exception: raise Exception(f"Invalid port number: {port}") + def _add_openapi_routes(self, auth_required: bool = False): + self.add_route( + route_type=HttpMethod.GET, + endpoint="/openapi.json", + handler=self.openapi.get_openapi_config, + is_const=True, + auth_required=auth_required, + ) + self.add_route( + route_type=HttpMethod.GET, + endpoint="/docs", + handler=self.openapi.get_openapi_docs_page, + is_const=True, + auth_required=auth_required, + ) + def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = True): """ Starts the server @@ -232,10 +248,6 @@ def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = T :param port int: represents the port number at which the server is listening :param _check_port bool: represents if the port should be checked if it is already in use """ - if not self.config.disable_openapi: - self.add_route(route_type=HttpMethod.GET, endpoint="/openapi.json", handler=self.openapi.get_openapi_config, is_const=True) - self.add_route(route_type=HttpMethod.GET, endpoint="/docs", handler=self.openapi.get_openapi_docs_page, is_const=True) - logger.info("Docs hosted at http://%s:%s/docs", host, port) host = os.getenv("ROBYN_HOST", host) port = int(os.getenv("ROBYN_PORT", port)) @@ -250,6 +262,10 @@ def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = T logger.error("Invalid port number. Please enter a valid port number.") continue + if not self.config.disable_openapi: + self._add_openapi_routes() + logger.info("Docs hosted at http://%s:%s/docs", host, port) + logger.info("Robyn version: %s", __version__) logger.info("Starting server at http://%s:%s", host, port)