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

feat: introduce a disable openapi flag #929

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions docs_src/src/pages/documentation/api_reference/openapi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Out of the box, the following endpoints are setup for you:
- `/docs` The Swagger UI
- `/openapi.json` The JSON Specification


However, if you don't want to generate the OpenAPI docs, you can disable it by passing `--disable-openapi` flag while starting the application.

```bash
python app.py --disable-openapi
```

## How to use?

- Query Params: The typing for query params can be added as `def get(r: Request, query_params=GetRequestParams)` where `GetRequestParams` is a `TypedDict`
Expand Down
53 changes: 9 additions & 44 deletions docs_src/src/pages/documentation/example_app/openapi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Out of the box, the following endpoints are setup for you:
- `/docs` The Swagger UI
- `/openapi.json` The JSON Specification

However, if you don't want to generate the OpenAPI docs, you can disable it by passing `--disable-openapi` flag while starting the application.

```bash
python app.py --disable-openapi
```

## How to use?

- Query Params: The typing for query params can be added as `def get(r: Request, query_params=GetRequestParams)` where `GetRequestParams` is a `TypedDict`
Expand All @@ -24,28 +30,8 @@ from typing import TypedDict
from robyn import Robyn, OpenAPI
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components

app = Robyn(
file_object=__file__,
openapi=OpenAPI(
info=OpenAPIInfo(
title="Sample App",
description="This is a sample server application.",
termsOfService="https://example.com/terms/",
version="1.0.0",
contact=Contact(
name="API Support",
url="https://www.example.com/support",
email="[email protected]",
),
license=License(
name="BSD2.0",
url="https://opensource.org/license/bsd-2-clause",
),
externalDocs=ExternalDocumentation(description="Find more info here", url="https://example.com/"),
components=Components(),
),
),
)

app = Robyn(file_object=__file__)


@app.get("/")
Expand Down Expand Up @@ -81,28 +67,7 @@ from typing import TypedDict
from robyn import Robyn, OpenAPI, Request
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components

app: Robyn = Robyn(
file_object=__file__,
openapi=OpenAPI(
info=OpenAPIInfo(
title="Sample App",
description="This is a sample server application.",
termsOfService="https://example.com/terms/",
version="1.0.0",
contact=Contact(
name="API Support",
url="https://www.example.com/support",
email="[email protected]",
),
license=License(
name="BSD2.0",
url="https://opensource.org/license/bsd-2-clause",
),
externalDocs=ExternalDocumentation(description="Find more info here", url="https://example.com/"),
components=Components(),
),
),
)
app: Robyn = Robyn(file_object=__file__)


@app.get("/")
Expand Down
8 changes: 4 additions & 4 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ 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
"""

self.add_route(route_type=HttpMethod.GET, endpoint="/openapi.json", handler=self.openapi.get_openapi_config_string, is_const=True)
self.add_route(route_type=HttpMethod.GET, endpoint="/docs", handler=self.openapi.get_openapi_docs_page, is_const=True)
if not self.config.disable_openapi:
self.add_route(route_type=HttpMethod.GET, endpoint="/openapi.json", handler=self.openapi.get_openapi_config_string, 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 @@ -244,7 +245,6 @@ def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = T

logger.info("Robyn version: %s", __version__)
logger.info("Starting server at http://%s:%s", host, port)
logger.info("Docs hosted at http://%s:%s/docs", host, port)

mp.allow_connection_pickling()

Expand Down
8 changes: 8 additions & 0 deletions robyn/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def __init__(self) -> None:
default=None,
help="Create a rust file with the given name.",
)
parser.add_argument(
"--disable-openapi",
dest="disable_openapi",
action="store_true",
default=False,
help="Disable the OpenAPI documentation.",
)

args, unknown_args = parser.parse_known_args()

Expand All @@ -82,6 +89,7 @@ def __init__(self) -> None:
self.compile_rust_path = args.compile_rust_path
self.create_rust_file = args.create_rust_file
self.file_path = None
self.disable_openapi = args.disable_openapi

# find something that ends with .py in unknown_args
for arg in unknown_args:
Expand Down
Loading