Feature request: support path prefix so we can watch glances via url like http://ip/urlprefix/ #1365

This commit is contained in:
nicolargo 2023-06-04 10:44:42 +02:00
parent 513e4486fb
commit cf2dcb3d6b
6 changed files with 258 additions and 628 deletions

View File

@ -22,8 +22,13 @@ history_size=1200
[outputs]
# Theme name for the Curses interface: black or white
curse_theme=black
# Limit the number of processes to display in the WebUI
# Limit the number of processes to display (for the WebUI)
max_processes_display=30
# Set the URL prefix (for the WebUI and the API)
# Example: url_prefix=/glances/ => http://localhost/glances/
# The final / is mandatory
# Default is no prefix (/)
#url_prefix=/glances/
##############################################################################
# plugins

View File

@ -24,6 +24,11 @@ history_size=1200
curse_theme=black
# Limit the number of processes to display in the WebUI
max_processes_display=30
# Set the URL prefix (for the WebUI and the API)
# Example: url_prefix=/glances/ => http://localhost/glances/
# The final / is mandatory
# Default is no prefix (/)
#url_prefix=/glances/
##############################################################################
# plugins

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,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" "May 16, 2023" "3.4.0" "Glances"
.TH "GLANCES" "1" "Jun 04, 2023" "4.0.0_beta01" "Glances"
.SH NAME
glances \- An eye on your system
.SH SYNOPSIS

View File

@ -16,6 +16,7 @@ from io import open
import webbrowser
import zlib
import socket
from urllib.parse import urljoin
from glances.globals import b, json_dumps
from glances.timer import Timer
@ -85,8 +86,10 @@ class GlancesBottle(object):
# Load configuration file
self.load_config(config)
# Set the bind URL
self.bind_url = 'http://{}:{}/'.format(self.args.bind_address, self.args.port)
# Set the bind URL (only used for log information purpose)
self.bind_url = urljoin('http://{}:{}/'.format(self.args.bind_address,
self.args.port),
self.url_prefix)
# Init Bottle
self._app = Bottle()
@ -107,10 +110,12 @@ class GlancesBottle(object):
def load_config(self, config):
"""Load the outputs section of the configuration file."""
# Limit the number of processes to display in the WebUI
self.url_prefix = '/'
if config is not None and config.has_section('outputs'):
logger.debug('Read number of processes to display in the WebUI')
n = config.get_value('outputs', 'max_processes_display', default=None)
logger.debug('Number of processes to display in the WebUI: {}'.format(n))
self.url_prefix = config.get_value('outputs', 'url_prefix', default='/')
logger.debug('URL prefix: {}'.format(self.url_prefix))
def __update__(self):
# Never update more than 1 time per cached_time
@ -162,7 +167,7 @@ class GlancesBottle(object):
self._app.route(
'/api/%s/<plugin>/<item>/<value:path>' % self.API_VERSION, method="GET", callback=self._api_value
)
bindmsg = 'Glances RESTful API Server started on {}api/{}/'.format(self.bind_url, self.API_VERSION)
bindmsg = 'Glances RESTful API Server started on {}api/{}'.format(self.bind_url, self.API_VERSION)
logger.info(bindmsg)
# WEB UI
@ -193,13 +198,31 @@ class GlancesBottle(object):
# 2) Glances standalone mode is running on Windows OS
webbrowser.open(self.bind_url, new=2, autoraise=1)
try:
self._app.run(host=self.args.bind_address, port=self.args.port, quiet=not self.args.debug)
except socket.error as e:
logger.critical('Error: Can not ran Glances Web server ({})'.format(e))
# Run the Web application
if self.url_prefix != '/':
# Create an outer Bottle class instance to manage url_prefix
self.main_app = Bottle()
self.main_app.mount(self.url_prefix, self._app)
try:
self.main_app.run(host=self.args.bind_address,
port=self.args.port,
quiet=not self.args.debug)
except socket.error as e:
logger.critical('Error: Can not ran Glances Web server ({})'.format(e))
else:
try:
self._app.run(host=self.args.bind_address,
port=self.args.port,
quiet=not self.args.debug)
except socket.error as e:
logger.critical('Error: Can not ran Glances Web server ({})'.format(e))
def end(self):
"""End the bottle."""
logger.info("Close the Web server")
self._app.close()
if self.url_prefix != '/':
self.main_app.close()
def _index(self, refresh_time=None):
"""Bottle callback for index.html (/) file."""

View File

@ -30,7 +30,25 @@ The Glances Restfull/API server could be ran using the following command line:
# glances -w --disable-webui
Note: Change request URL api/3 by api/2 if you use Glances 2.x.
API URL
-------
The default root API URL is ``http://localhost:61208/api/3``.
The bind addresse and port could be changed using the ``--bind`` and ``--port`` command line options.
It is also possible to define an URL prefix using the ``url_prefix`` option from the [outputs] section
of the Glances configuration file. The url_prefix should always end with a slash (``/``).
For example:
.. code-block:: ini
[outputs]
url_prefix = /glances/
will change the root API URL to ``http://localhost:61208/glances/api/3`` and the Web UI URL to
``http://localhost:61208/glances/``
"""