async def components_factory(config: BackendConfig, event_bus: EventBus): dbh = PGHandler(config.db_url, config.db_min_connections, config.db_max_connections, event_bus) organization = PGOrganizationComponent(dbh) user = PGUserComponent(dbh, event_bus) message = PGMessageComponent(dbh) realm = PGRealmComponent(dbh) vlob = PGVlobComponent(dbh) ping = PGPingComponent(dbh) blockstore = blockstore_factory(config.blockstore_config, postgresql_dbh=dbh) block = PGBlockComponent(dbh, blockstore, vlob) events = EventsComponent(realm) async with trio.open_service_nursery() as nursery: await dbh.init(nursery) try: yield { "user": user, "message": message, "realm": realm, "vlob": vlob, "ping": ping, "blockstore": blockstore, "block": block, "organization": organization, "events": events, } finally: await dbh.teardown()
async def components_factory( config: BackendConfig, event_bus: EventBus ) -> AsyncGenerator[dict, None]: dbh = PGHandler(config.db_url, config.db_min_connections, config.db_max_connections, event_bus) async def _send_event( event: BackendEvent, conn: Optional[triopg._triopg.TrioConnectionProxy] = None, **kwargs ) -> None: if conn is None: async with dbh.pool.acquire() as conn: await send_signal(conn, event, **kwargs) else: await send_signal(conn, event, **kwargs) webhooks = WebhooksComponent(config) http = HTTPComponent(config) organization = PGOrganizationComponent(dbh, webhooks, config) user = PGUserComponent(dbh, event_bus) invite = PGInviteComponent(dbh, event_bus, config) message = PGMessageComponent(dbh) realm = PGRealmComponent(dbh) vlob = PGVlobComponent(dbh) ping = PGPingComponent(dbh) blockstore = blockstore_factory(config.blockstore_config, postgresql_dbh=dbh) block = PGBlockComponent(dbh, blockstore, vlob) pki = PGPkiEnrollmentComponent(dbh) events = EventsComponent(realm, send_event=_send_event) components = { "events": events, "webhooks": webhooks, "http": http, "organization": organization, "user": user, "invite": invite, "message": message, "realm": realm, "vlob": vlob, "ping": ping, "block": block, "blockstore": blockstore, "pki": pki, } for component in components.values(): method = getattr(component, "register_components", None) if method is not None: method(**components) async with open_service_nursery() as nursery: await dbh.init(nursery) try: yield components finally: await dbh.teardown()
async def components_factory(config: BackendConfig, event_bus: EventBus): dbh = PGHandler( config.db_url, config.db_min_connections, config.db_max_connections, config.db_first_tries_number, config.db_first_tries_sleep, event_bus, ) async def _send_event( event: BackendEvent, conn: Optional[triopg._triopg.TrioConnectionProxy] = None, **kwargs): if conn is None: async with dbh.pool.acquire() as conn: await send_signal(conn, event, **kwargs) else: await send_signal(conn, event, **kwargs) webhooks = WebhooksComponent(config) http = HTTPComponent(config) organization = PGOrganizationComponent(dbh, webhooks) user = PGUserComponent(dbh, event_bus) invite = PGInviteComponent(dbh, event_bus, config) message = PGMessageComponent(dbh) realm = PGRealmComponent(dbh) vlob = PGVlobComponent(dbh) ping = PGPingComponent(dbh) blockstore = blockstore_factory(config.blockstore_config, postgresql_dbh=dbh) block = PGBlockComponent(dbh, blockstore, vlob) events = EventsComponent(realm, send_event=_send_event) async with trio.open_service_nursery() as nursery: await dbh.init(nursery) try: yield { "events": events, "webhooks": webhooks, "http": http, "organization": organization, "user": user, "invite": invite, "message": message, "realm": realm, "vlob": vlob, "ping": ping, "block": block, "blockstore": blockstore, } finally: await dbh.teardown()
async def components_factory(config: BackendConfig, event_bus: EventBus): send_events_channel, receive_events_channel = trio.open_memory_channel[ Tuple[Enum, Dict[str, object]]](math.inf) async def _send_event(event: Enum, **kwargs): await send_events_channel.send((event, kwargs)) async def _dispatch_event(): async for event, kwargs in receive_events_channel: await trio.sleep(0) event_bus.send(event, **kwargs) webhooks = WebhooksComponent(config) http = HTTPComponent(config) organization = MemoryOrganizationComponent(_send_event, webhooks, config) user = MemoryUserComponent(_send_event, event_bus) invite = MemoryInviteComponent(_send_event, event_bus, config) message = MemoryMessageComponent(_send_event) realm = MemoryRealmComponent(_send_event) vlob = MemoryVlobComponent(_send_event) ping = MemoryPingComponent(_send_event) pki = MemoryPkiEnrollmentComponent(_send_event) block = MemoryBlockComponent() blockstore = blockstore_factory(config.blockstore_config) events = EventsComponent(realm, send_event=_send_event) components = { "events": events, "webhooks": webhooks, "http": http, "organization": organization, "user": user, "invite": invite, "message": message, "realm": realm, "vlob": vlob, "ping": ping, "pki": pki, "block": block, "blockstore": blockstore, } for component in components.values(): method = getattr(component, "register_components", None) if method is not None: method(**components) async with open_service_nursery() as nursery: nursery.start_soon(_dispatch_event) try: yield components finally: nursery.cancel_scope.cancel()
async def components_factory(config: BackendConfig, event_bus: EventBus): (send_events_channel, receive_events_channel) = trio.open_memory_channel(math.inf) async def _send_event(event: str, **kwargs): await send_events_channel.send((event, kwargs)) async def _dispatch_event(): async for event, kwargs in receive_events_channel: await trio.sleep(0) event_bus.send(event, **kwargs) webhooks = WebhooksComponent(config) http = HTTPComponent(config) organization = MemoryOrganizationComponent(_send_event, webhooks) user = MemoryUserComponent(_send_event, event_bus) invite = MemoryInviteComponent(_send_event, event_bus, config) message = MemoryMessageComponent(_send_event) realm = MemoryRealmComponent(_send_event) vlob = MemoryVlobComponent(_send_event) ping = MemoryPingComponent(_send_event) block = MemoryBlockComponent() blockstore = blockstore_factory(config.blockstore_config) events = EventsComponent(realm, send_event=_send_event) components = { "events": events, "webhooks": webhooks, "http": http, "organization": organization, "user": user, "invite": invite, "message": message, "realm": realm, "vlob": vlob, "ping": ping, "block": block, "blockstore": blockstore, } for component in (organization, user, invite, message, realm, vlob, ping, block): component.register_components(**components) async with trio.open_service_nursery() as nursery: nursery.start_soon(_dispatch_event) try: yield components finally: nursery.cancel_scope.cancel()
def __init__(self, config, event_bus=None): self.event_bus = event_bus or EventBus() self.config = config self.nursery = None self.dbh = None self.events = EventsComponent(self.event_bus) if self.config.db_url == "MOCKED": self.user = MemoryUserComponent(self.event_bus) self.organization = MemoryOrganizationComponent(self.user) self.message = MemoryMessageComponent(self.event_bus) self.beacon = MemoryBeaconComponent(self.event_bus) self.vlob = MemoryVlobComponent(self.event_bus, self.beacon) self.ping = MemoryPingComponent(self.event_bus) self.blockstore = blockstore_factory(self.config.blockstore_config) else: self.dbh = PGHandler(self.config.db_url, self.event_bus) self.user = PGUserComponent(self.dbh, self.event_bus) self.organization = PGOrganizationComponent(self.dbh, self.user) self.message = PGMessageComponent(self.dbh) self.beacon = PGBeaconComponent(self.dbh) self.vlob = PGVlobComponent(self.dbh, self.beacon) self.ping = PGPingComponent(self.dbh) self.blockstore = blockstore_factory(self.config.blockstore_config, postgresql_dbh=self.dbh) self.logged_cmds = { "events_subscribe": self.events.api_events_subscribe, "events_listen": self.events.api_events_listen, "ping": self.ping.api_ping, "beacon_read": self.beacon.api_beacon_read, # Message "message_get": self.message.api_message_get, "message_send": self.message.api_message_send, # User&Device "user_get": self.user.api_user_get, "user_find": self.user.api_user_find, "user_invite": self.user.api_user_invite, "user_cancel_invitation": self.user.api_user_cancel_invitation, "user_create": self.user.api_user_create, "device_invite": self.user.api_device_invite, "device_cancel_invitation": self.user.api_device_cancel_invitation, "device_create": self.user.api_device_create, "device_revoke": self.user.api_device_revoke, # Blockstore "blockstore_create": self.blockstore.api_blockstore_create, "blockstore_read": self.blockstore.api_blockstore_read, # Vlob "vlob_group_check": self.vlob.api_vlob_group_check, "vlob_create": self.vlob.api_vlob_create, "vlob_read": self.vlob.api_vlob_read, "vlob_update": self.vlob.api_vlob_update, } self.anonymous_cmds = { "user_claim": self.user.api_user_claim, "user_get_invitation_creator": self.user.api_user_get_invitation_creator, "device_claim": self.user.api_device_claim, "device_get_invitation_creator": self.user.api_device_get_invitation_creator, "organization_bootstrap": self.organization.api_organization_bootstrap, "ping": self.ping.api_ping, } self.administration_cmds = { "organization_create": self.organization.api_organization_create, "ping": self.ping.api_ping, } for fn in self.anonymous_cmds.values(): check_anonymous_api_allowed(fn)