Skip to content

Commit

Permalink
refactor: revamp add OpenAPI route (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas authored Oct 12, 2024
1 parent 028f161 commit 6cd93ac
Showing 1 changed file with 20 additions and 4 deletions.
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

0 comments on commit 6cd93ac

Please sign in to comment.