Skip to content

Commit

Permalink
api-server: fix docs page (#907)
Browse files Browse the repository at this point in the history
* include swagger bundle

Signed-off-by: Teo Koon Peng <[email protected]>

* temp enable for all push

Signed-off-by: Teo Koon Peng <[email protected]>

* temp enable for all push

Signed-off-by: Teo Koon Peng <[email protected]>

* fix docs pathing

Signed-off-by: Teo Koon Peng <[email protected]>

* fix openapi.json path

Signed-off-by: Teo Koon Peng <[email protected]>

* fix openapi.json path

Signed-off-by: Teo Koon Peng <[email protected]>

* Revert "temp enable for all push"

This reverts commit 0d4201d.

Signed-off-by: Teo Koon Peng <[email protected]>

* Revert "temp enable for all push"

This reverts commit 28017be.

Signed-off-by: Teo Koon Peng <[email protected]>

* fix e2e

Signed-off-by: Teo Koon Peng <[email protected]>

---------

Signed-off-by: Teo Koon Peng <[email protected]>
  • Loading branch information
Teo Koon Peng committed Feb 28, 2024
1 parent 9567672 commit 936e290
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ghpages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Extract docs
run: |
. /rmf_demos_ws/install/setup.bash
pipenv run python3 scripts/extract_docs.py -o docs
pnpm run generate-docs
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
Expand Down
10 changes: 5 additions & 5 deletions packages/api-server/api_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ async def on_shutdown():

@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
openapi_url = app.openapi_url if app.openapi_url is not None else "/openapi.json"
openapi_url = f"{app_config.public_url.geturl()}{app.openapi_url}"
return get_swagger_ui_html(
openapi_url=openapi_url,
title=app.title + " - Swagger UI",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
swagger_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
swagger_js_url=f"{app_config.public_url.geturl()}/static/swagger-ui-bundle.js",
swagger_css_url=f"{app_config.public_url.geturl()}/static/swagger-ui.css",
)


Expand All @@ -234,11 +234,11 @@ async def swagger_ui_redirect():

@app.get("/redoc", include_in_schema=False)
async def redoc_html():
openapi_url = app.openapi_url if app.openapi_url is not None else "/openapi.json"
openapi_url = f"{app_config.public_url.geturl()}{app.openapi_url}"
return get_redoc_html(
openapi_url=openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
redoc_js_url=f"{app_config.public_url.geturl()}/static/redoc.standalone.js",
)


Expand Down
5 changes: 3 additions & 2 deletions packages/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"scripts": {
"prepack": "../../scripts/pipenv run python setup.py bdist_wheel",
"restart": "RMF_API_SERVER_CONFIG=sqlite_local_config.py ../../scripts/pipenv run python -m api_server",
"start": "rm -rf run && mkdir -p run/cache && RMF_API_SERVER_CONFIG=sqlite_local_config.py ../../scripts/pipenv run python -m api_server",
"start": "rm -rf run && mkdir -p run/cache && RMF_API_SERVER_CONFIG=${RMF_API_SERVER_CONFIG:-sqlite_local_config.py} ../../scripts/pipenv run python -m api_server",
"start:psql": "rm -rf run && mkdir -p run/cache && RMF_API_SERVER_CONFIG=psql_local_config.py ../../scripts/pipenv run python -m api_server",
"test": "../../scripts/pipenv run python scripts/test.py",
"test:cov": "../../scripts/pipenv run python -m coverage run scripts/test.py",
"test:report": "../../scripts/pipenv run python -m coverage html && xdg-open htmlcov/index.html",
"lint": "../../scripts/pipenv run pyright && ../../scripts/pipenv run pylint api_server --ignore=ros_pydantic,rmf_api",
"generate-models": "./generate-models.sh"
"generate-models": "./generate-models.sh",
"generate-docs": "RMF_API_SERVER_CONFIG=scripts/docs_config.py ../../scripts/pipenv run python scripts/extract_docs.py -o docs"
},
"devDependencies": {
"pipenv-install": "workspace:*"
Expand Down
11 changes: 11 additions & 0 deletions packages/api-server/scripts/docs_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

from api_server.default_config import config

here = os.path.dirname(__file__)

config.update(
{
"public_url": "/rmf-web",
}
)
24 changes: 19 additions & 5 deletions packages/api-server/scripts/extract_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ def cleanup():

atexit.register(cleanup)
server_proc = subprocess.Popen(
("pnpm", "start"), cwd=f"{os.path.dirname(__file__)}/..", start_new_session=True
("python", "-m", "api_server"),
cwd=f"{os.path.dirname(__file__)}/..",
start_new_session=True,
)

time.sleep(5) # wait for server to be ready
outdir = f"{args.output}/docs/api-server/"
outdir = f"{args.output}"
os.makedirs(outdir, exist_ok=True)

with urlopen("http://127.0.0.1:8000/docs") as resp:
base_url = "http://localhost:8000/rmf-web"
with urlopen(f"{base_url}/docs") as resp:
html: bytes = resp.read()
html = html.replace(b"/openapi.json", b"/rmf-web/docs/api-server/openapi.json")
with open(f"{outdir}/index.html", "bw") as f:
f.write(html)

with urlopen("http://127.0.0.1:8000/openapi.json") as resp:
with urlopen(f"{base_url}/openapi.json") as resp:
openapi = json.loads(resp.read())
openapi["servers"] = [
{
Expand All @@ -48,3 +50,15 @@ def cleanup():
]
with open(f"{outdir}/openapi.json", "w") as f:
json.dump(openapi, f)

files_to_download = [
"/static/swagger-ui-bundle.js",
"/static/swagger-ui.css",
]

for p in files_to_download:
with urlopen(f"{base_url}{p}") as resp:
fp = f"{outdir}{p}"
os.makedirs(os.path.dirname(fp), exist_ok=True)
with open(fp, "bw") as f:
f.write(resp.read())

0 comments on commit 936e290

Please sign in to comment.