Make CORS option configurable #2812

This commit is contained in:
nicolargo 2024-06-01 10:38:40 +02:00
parent 350318fb6e
commit 6410ccab9b
3 changed files with 40 additions and 7 deletions

View File

@ -23,12 +23,16 @@ history_size=1200
############################################################################## ##############################################################################
[outputs] [outputs]
# Options for all UIs
#--------------------
# Separator in the Curses and WebUI interface (between top and others plugins) # Separator in the Curses and WebUI interface (between top and others plugins)
separator=True separator=True
# Set the the Curses and WebUI interface left menu plugin list (comma-separated) # Set the the Curses and WebUI interface left menu plugin list (comma-separated)
#left_menu=network,wifi,connections,ports,diskio,fs,irq,folders,raid,smart,sensors,now #left_menu=network,wifi,connections,ports,diskio,fs,irq,folders,raid,smart,sensors,now
# Limit the number of processes to display (in the WebUI) # Limit the number of processes to display (in the WebUI)
max_processes_display=25 max_processes_display=25
# Options for WebUI
#------------------
# Set URL prefix for the WebUI and the API # Set URL prefix for the WebUI and the API
# Example: url_prefix=/glances/ => http://localhost/glances/ # Example: url_prefix=/glances/ => http://localhost/glances/
# Note: The final / is mandatory # Note: The final / is mandatory
@ -41,9 +45,22 @@ max_processes_display=25
# then configure this folder with the webui_root_path key # then configure this folder with the webui_root_path key
# Default is folder where glances_restfull_api.py is hosted # Default is folder where glances_restfull_api.py is hosted
#webui_root_path= #webui_root_path=
# CORS options
# Comma separated list of origins that should be permitted to make cross-origin requests.
# Default is *
#cors_origins=*
# Indicate that cookies should be supported for cross-origin requests.
# Default is True
#cors_credentials=True
# Comma separated list of HTTP methods that should be allowed for cross-origin requests.
# Default is *
#cors_methods=*
# Comma separated list of HTTP request headers that should be supported for cross-origin requests.
# Default is *
#cors_headers=*
############################################################################## ##############################################################################
# plugins # Plugins
############################################################################## ##############################################################################
[quicklook] [quicklook]

View File

@ -23,12 +23,16 @@ history_size=1200
############################################################################## ##############################################################################
[outputs] [outputs]
# Options for all UIs
#--------------------
# Separator in the Curses and WebUI interface (between top and others plugins) # Separator in the Curses and WebUI interface (between top and others plugins)
separator=True separator=True
# Set the the Curses and WebUI interface left menu plugin list (comma-separated) # Set the the Curses and WebUI interface left menu plugin list (comma-separated)
#left_menu=network,wifi,connections,ports,diskio,fs,irq,folders,raid,smart,sensors,now #left_menu=network,wifi,connections,ports,diskio,fs,irq,folders,raid,smart,sensors,now
# Limit the number of processes to display (in the WebUI) # Limit the number of processes to display (in the WebUI)
max_processes_display=25 max_processes_display=25
# Options for WebUI
#------------------
# Set URL prefix for the WebUI and the API # Set URL prefix for the WebUI and the API
# Example: url_prefix=/glances/ => http://localhost/glances/ # Example: url_prefix=/glances/ => http://localhost/glances/
# Note: The final / is mandatory # Note: The final / is mandatory
@ -41,6 +45,19 @@ max_processes_display=25
# then configure this folder with the webui_root_path key # then configure this folder with the webui_root_path key
# Default is folder where glances_restfull_api.py is hosted # Default is folder where glances_restfull_api.py is hosted
#webui_root_path= #webui_root_path=
# CORS options
# Comma separated list of origins that should be permitted to make cross-origin requests.
# Default is *
#cors_origins=*
# Indicate that cookies should be supported for cross-origin requests.
# Default is True
#cors_credentials=True
# Comma separated list of HTTP methods that should be allowed for cross-origin requests.
# Default is *
#cors_methods=*
# Comma separated list of HTTP request headers that should be supported for cross-origin requests.
# Default is *
#cors_headers=*
############################################################################## ##############################################################################
# plugins # plugins

View File

@ -130,12 +130,11 @@ class GlancesRestfulApi:
# https://fastapi.tiangolo.com/tutorial/cors/ # https://fastapi.tiangolo.com/tutorial/cors/
self._app.add_middleware( self._app.add_middleware(
CORSMiddleware, CORSMiddleware,
# Related to https://github.com/nicolargo/glances/discussions/2802 # Related to https://github.com/nicolargo/glances/issues/2812
# allow_origins=[self.bind_url], allow_origins=config.get_list_value('outputs', 'cors_origins', default=["*"]),
allow_origins=["*"], allow_credentials=config.get_bool_value('outputs', 'cors_credentials', default=True),
allow_credentials=True, allow_methods=config.get_list_value('outputs', 'cors_methods', default=["*"]),
allow_methods=["*"], allow_headers=config.get_list_value('outputs', 'cors_headers', default=["*"]),
allow_headers=["*"],
) )
# FastAPI Enable GZIP compression # FastAPI Enable GZIP compression