comparison libervia/web/server/resources.py @ 1609:f3305832f3f6

server (resources): handle external Libervia apps by adding them to menu without using a Proxy: When an external Libervia app is used, a link is added to the menu, and clicking on it will open the app in an blank page instead of embedding it with a Proxy.
author Goffi <goffi@goffi.org>
date Fri, 31 May 2024 11:10:04 +0200
parents eb00d593801d
children
comparison
equal deleted inserted replaced
1608:29eb1ea35869 1609:f3305832f3f6
25 25
26 from twisted.internet import defer 26 from twisted.internet import defer
27 from twisted.web import server 27 from twisted.web import server
28 from twisted.web import static 28 from twisted.web import static
29 from twisted.web import resource as web_resource 29 from twisted.web import resource as web_resource
30 from twisted.web.util import Redirect
30 31
31 from libervia.web.server.constants import Const as C 32 from libervia.web.server.constants import Const as C
32 from libervia.web.server.utils import quote 33 from libervia.web.server.utils import quote
33 from libervia.backend.core import exceptions 34 from libervia.backend.core import exceptions
34 from libervia.backend.core.i18n import D_, _ 35 from libervia.backend.core.i18n import D_, _
208 instance_id: str 209 instance_id: str
209 ) -> None: 210 ) -> None:
210 exposed_data = self.libervia_apps[app_name] = data_format.deserialise( 211 exposed_data = self.libervia_apps[app_name] = data_format.deserialise(
211 await self.host.bridge_call("application_exposed_get", app_name, "", "") 212 await self.host.bridge_call("application_exposed_get", app_name, "", "")
212 ) 213 )
213 214 external = exposed_data.get("web_external", False)
215 if external:
216 log.info(
217 f"App {app_name!r} (instance {instance_id!r}) has been started."
218 )
219 # we don't add any resource of proxy for external apps.
220 return
214 try: 221 try:
215 web_port = int(exposed_data['ports']['web'].split(':')[1]) 222 web_port = int(exposed_data['ports']['web'].split(':')[1])
216 except (KeyError, ValueError): 223 except (KeyError, ValueError):
217 log.warning(_( 224 log.warning(_(
218 "no web port found for application {app_name!r}, can't use it " 225 "no web port found for application {app_name!r}, can't use it "
235 "localhost", 242 "localhost",
236 web_port, 243 web_port,
237 url_prefix.encode() 244 url_prefix.encode()
238 ) 245 )
239 self.add_resource_to_path(url_prefix, res) 246 self.add_resource_to_path(url_prefix, res)
247
240 log.info( 248 log.info(
241 f"Resource for app {app_name!r} (instance {instance_id!r}) has been added" 249 f"Resource for app {app_name!r} (instance {instance_id!r}) has been added."
242 ) 250 )
243 251
244 async def _init_redirections(self, options): 252 async def _init_redirections(self, options):
245 url_redirections = options["url_redirections_dict"] 253 url_redirections = options["url_redirections_dict"]
246 254
483 page_name, url = menu 491 page_name, url = menu
484 elif menu.startswith("libervia-app:"): 492 elif menu.startswith("libervia-app:"):
485 app_name = menu[13:].strip().lower() 493 app_name = menu[13:].strip().lower()
486 app_data = await self._start_app(app_name) 494 app_data = await self._start_app(app_name)
487 exposed_data = app_data["expose"] 495 exposed_data = app_data["expose"]
488 front_url = exposed_data['front_url'] 496 external = exposed_data.get("web_external", False)
489 options = self.host.options 497 if external:
490 url_redirections = options["url_redirections_dict"].setdefault( 498 # The app is opened separately and not embedded into the Web frontend.
491 self.site_name, {} 499 try:
492 ) 500 front_url = exposed_data["front_url"]
493 if front_url in url_redirections: 501 except KeyError:
494 raise exceptions.ConflictError( 502 raise exceptions.DataError(
495 f"There is already a redirection from {front_url!r}, can't add " 503 'Missing "front_url" in app data for external app '
496 f"{app_name!r}") 504 f'{app_name!r}, it is mandatory when "external" is set. This '
497 505 f'must be set in {app_name!r} app configuration file.'
498 url_redirections[front_url] = { 506
499 "page": 'embed_app', 507 )
500 "path_args": [app_name] 508 else:
501 } 509 front_url = exposed_data['web_url_path']
510 options = self.host.options
511 url_redirections = options["url_redirections_dict"].setdefault(
512 self.site_name, {}
513 )
514 if front_url in url_redirections:
515 raise exceptions.ConflictError(
516 f"There is already a redirection from {front_url!r}, can't add "
517 f"{app_name!r}")
518
519 url_redirections[front_url] = {
520 "page": 'embed_app',
521 "path_args": [app_name]
522 }
502 523
503 page_name = exposed_data.get('web_label', app_name).title() 524 page_name = exposed_data.get('web_label', app_name).title()
504 url = front_url 525 url = front_url
505 526
506 log.debug( 527 log.debug(