|
| 1 | +import pkg_resources |
| 2 | + |
| 3 | +from asphalt.core import Component, ContainerComponent, Context |
| 4 | +from jupyverse_api.app import App |
| 5 | +from jupyverse_api.auth import Auth |
| 6 | + |
| 7 | +from .routes import Voila |
| 8 | + |
| 9 | + |
| 10 | +class VoilaComponent(Component): |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + *, |
| 14 | + settings, |
| 15 | + voila_configuration, |
| 16 | + static_paths, |
| 17 | + base_url, |
| 18 | + ): |
| 19 | + super().__init__() |
| 20 | + self.settings = settings |
| 21 | + self.voila_configuration = voila_configuration |
| 22 | + self.static_paths = static_paths |
| 23 | + self.base_url = base_url |
| 24 | + |
| 25 | + async def start( |
| 26 | + self, |
| 27 | + ctx: Context, |
| 28 | + ) -> None: |
| 29 | + app = await ctx.request_resource(App) |
| 30 | + auth = await ctx.request_resource(Auth) |
| 31 | + |
| 32 | + voila = Voila( |
| 33 | + app, |
| 34 | + auth, |
| 35 | + self.settings, |
| 36 | + self.voila_configuration, |
| 37 | + self.static_paths, |
| 38 | + self.base_url, |
| 39 | + ) |
| 40 | + ctx.add_resource(voila) |
| 41 | + |
| 42 | + |
| 43 | +class RootComponent(ContainerComponent): |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + settings, |
| 47 | + voila_configuration, |
| 48 | + static_paths, |
| 49 | + base_url, |
| 50 | + ip, |
| 51 | + port, |
| 52 | + ): |
| 53 | + super().__init__() |
| 54 | + self.settings = settings |
| 55 | + self.voila_configuration = voila_configuration |
| 56 | + self.static_paths = static_paths |
| 57 | + self.base_url = base_url |
| 58 | + self.ip = ip |
| 59 | + self.port = port |
| 60 | + |
| 61 | + async def start(self, ctx: Context) -> None: |
| 62 | + asphalt_components = { |
| 63 | + ep.name: ep |
| 64 | + for ep in pkg_resources.iter_entry_points(group="asphalt.components") |
| 65 | + } |
| 66 | + |
| 67 | + self.add_component( |
| 68 | + "fastapi", |
| 69 | + asphalt_components["fastapi"].load(), |
| 70 | + host=self.ip, |
| 71 | + port=self.port, |
| 72 | + ) |
| 73 | + self.add_component( |
| 74 | + "voila", |
| 75 | + asphalt_components["voila"].load(), |
| 76 | + settings=self.settings, |
| 77 | + voila_configuration=self.voila_configuration, |
| 78 | + static_paths=self.static_paths, |
| 79 | + base_url=self.base_url, |
| 80 | + ) |
| 81 | + self.add_component( |
| 82 | + "contents", |
| 83 | + asphalt_components["contents"].load(), |
| 84 | + prefix="/voila", |
| 85 | + ) |
| 86 | + self.add_component( |
| 87 | + "auth", |
| 88 | + asphalt_components["noauth"].load(), |
| 89 | + ) |
| 90 | + self.add_component( |
| 91 | + "app", |
| 92 | + asphalt_components["app"].load(), |
| 93 | + ) |
| 94 | + await super().start(ctx) |
0 commit comments