Skip to content

Commit 0003768

Browse files
committed
url_prefix do not work in Glances < 4.2.0 nicolargo#2912
1 parent 69290ee commit 0003768

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

docker-compose/glances.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ max_processes_display=25
3636
# Set URL prefix for the WebUI and the API
3737
# Example: url_prefix=/glances/ => http://localhost/glances/
3838
# Note: The final / is mandatory
39-
# Default is no prefix (/)
39+
# Default is no prefix
4040
#url_prefix=/glances/
4141
# Set root path for WebUI statics files
4242
# Why ? On Debian system, WebUI statics files are not provided.

glances/outputs/glances_restful_api.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ def __init__(self, config=None, args=None):
112112
self._app = FastAPI()
113113
self._password = None
114114

115-
# Change the default root path
116-
if self.url_prefix != '/':
117-
self._app.include_router(APIRouter(prefix=self.url_prefix.rstrip('/')))
118-
119115
# Set path for WebUI
120116
webui_root_path = config.get_value(
121117
'outputs', 'webui_root_path', default=os.path.dirname(os.path.realpath(__file__))
@@ -147,11 +143,15 @@ def __init__(self, config=None, args=None):
147143
def load_config(self, config):
148144
"""Load the outputs section of the configuration file."""
149145
# Limit the number of processes to display in the WebUI
150-
self.url_prefix = '/'
146+
self.url_prefix = ''
151147
if config is not None and config.has_section('outputs'):
148+
# Max process to display in the WebUI
152149
n = config.get_value('outputs', 'max_processes_display', default=None)
153150
logger.debug(f'Number of processes to display in the WebUI: {n}')
154-
self.url_prefix = config.get_value('outputs', 'url_prefix', default='/')
151+
# URL prefix
152+
self.url_prefix = config.get_value('outputs', 'url_prefix', default='')
153+
if self.url_prefix != '':
154+
self.url_prefix = self.url_prefix.rstrip('/')
155155
logger.debug(f'URL prefix: {self.url_prefix}')
156156

157157
def __update__(self):
@@ -179,7 +179,8 @@ def authentication(self, creds: Annotated[HTTPBasicCredentials, Depends(security
179179

180180
def _router(self):
181181
"""Define a custom router for Glances path."""
182-
router = APIRouter()
182+
# Create une main router
183+
router = APIRouter(prefix=self.url_prefix)
183184

184185
# REST API
185186
router.add_api_route(
@@ -278,7 +279,7 @@ def _router(self):
278279
router.add_api_route('/', response_class=HTMLResponse, endpoint=self._index)
279280

280281
# Statics files
281-
self._app.mount("/static", StaticFiles(directory=self.STATIC_PATH), name="static")
282+
self._app.mount(urljoin(self.url_prefix, '/static'), StaticFiles(directory=self.STATIC_PATH), name="static")
282283

283284
logger.info(f"Get WebUI in {self.STATIC_PATH}")
284285

0 commit comments

Comments
 (0)