diff --git a/.github/workflows/ghpages.yml b/.github/workflows/ghpages.yml index 1ab82c898..18825dc9a 100644 --- a/.github/workflows/ghpages.yml +++ b/.github/workflows/ghpages.yml @@ -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: diff --git a/packages/api-server/api_server/app.py b/packages/api-server/api_server/app.py index 0176b9b13..00ac20d55 100644 --- a/packages/api-server/api_server/app.py +++ b/packages/api-server/api_server/app.py @@ -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", ) @@ -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", ) diff --git a/packages/api-server/package.json b/packages/api-server/package.json index ed1fc3ac0..9f4555634 100644 --- a/packages/api-server/package.json +++ b/packages/api-server/package.json @@ -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:*" diff --git a/packages/api-server/scripts/docs_config.py b/packages/api-server/scripts/docs_config.py new file mode 100644 index 000000000..41306534f --- /dev/null +++ b/packages/api-server/scripts/docs_config.py @@ -0,0 +1,11 @@ +import os + +from api_server.default_config import config + +here = os.path.dirname(__file__) + +config.update( + { + "public_url": "/rmf-web", + } +) diff --git a/packages/api-server/scripts/extract_docs.py b/packages/api-server/scripts/extract_docs.py index de2ca7da4..bf02a33e3 100644 --- a/packages/api-server/scripts/extract_docs.py +++ b/packages/api-server/scripts/extract_docs.py @@ -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"] = [ { @@ -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())