Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: revamp add OpenAPI route #979

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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)

Expand Down
Loading