Improve Restful API documentation embeded in FastAPI #2632

This commit is contained in:
nicolargo 2025-06-29 14:32:58 +02:00
parent 813785ff82
commit 15a94f04da
7 changed files with 405 additions and 382 deletions

View File

@ -208,6 +208,7 @@ trivy: ## Run Trivy to find vulnerabilities in container images
# ===================================================================
docs: ## Create the documentation
$(PYTHON) ./generate_openapi.py
$(PYTHON) -m glances -C $(CONF) --api-doc > ./docs/api.rst
cd docs && ./build.sh && cd ..

File diff suppressed because it is too large Load Diff

View File

@ -28,7 +28,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.TH "GLANCES" "1" "Jun 22, 2025" "4.3.2_dev05" "Glances"
.TH "GLANCES" "1" "Jun 29, 2025" "4.3.2_dev05" "Glances"
.SH NAME
glances \- An eye on your system
.SH SYNOPSIS

1
docs/openapi.json Normal file

File diff suppressed because one or more lines are too long

30
generate_openapi.py Normal file
View File

@ -0,0 +1,30 @@
import json
from fastapi.openapi.utils import get_openapi
from glances.main import GlancesMain
# sys.path.append('./glances/outputs')
from glances.outputs.glances_restful_api import GlancesRestfulApi
# Init Glances core
core = GlancesMain(args_begin_at=2)
test_config = core.get_config()
test_args = core.get_args()
app = GlancesRestfulApi(config=test_config, args=test_args)._app
with open('./docs/openapi.json', 'w') as f:
json.dump(
get_openapi(
title=app.title,
version=app.version,
# Set the OenAPI version
# It's an hack to make openapi.json compatible with tools like https://editor.swagger.io/
# Please read https://fastapi.tiangolo.com/reference/fastapi/?h=openapi#fastapi.FastAPI.openapi_version
openapi_version="3.0.2",
description=app.description,
routes=app.routes,
),
f,
)

View File

@ -219,7 +219,7 @@ class GlancesRestfulApi:
# ==========================
# HEAD
router.add_api_route(f'{base_path}/status', self._api_status, methods=['HEAD', 'GET'])
router.add_api_route(f'{base_path}/status', self._api_status, methods=['HEAD'])
# POST
router.add_api_route(f'{base_path}/events/clear/warning', self._events_clear_warning, methods=['POST'])
@ -232,6 +232,7 @@ class GlancesRestfulApi:
)
# GET
router.add_api_route(f'{base_path}/status', self._api_status, methods=['GET'])
route_mapping = {
f'{base_path}/config': self._api_config,
f'{base_path}/config/{{section}}': self._api_config_section,
@ -266,7 +267,7 @@ class GlancesRestfulApi:
router.add_api_route(path, endpoint)
# Browser WEBUI
if self.args.browser:
if hasattr(self.args, 'browser') and self.args.browser:
# Template for the root browser.html file
router.add_api_route('/browser', self._browser, response_class=HTMLResponse)

View File

@ -26,8 +26,8 @@ API (Restfull/JSON) documentation
This documentation describes the Glances API version {__apiversion__} (Restfull/JSON) interface.
For Glances version 3, please have a look on:
``https://github.com/nicolargo/glances/blob/support/glancesv3/docs/api.rst``
An OpenAPI specification file is available at:
``https://raw.githubusercontent.com/nicolargo/glances/refs/heads/develop/docs/openapi.json``
Run the Glances API server
--------------------------