{"id":15492012,"url":"https://github.com/tarsil/starlette-bridge","last_synced_at":"2025-08-21T07:40:41.242Z","repository":{"id":143290147,"uuid":"615255593","full_name":"tarsil/starlette-bridge","owner":"tarsil","description":"A simple and clean event bridge of the old Starlette events to the new lifespan","archived":false,"fork":false,"pushed_at":"2024-07-01T10:07:10.000Z","size":109,"stargazers_count":4,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-23T22:09:05.088Z","etag":null,"topics":["async","esmerald","http","python","python3","starlette","websockets"],"latest_commit_sha":null,"homepage":"https://starlette-bridge.tarsild.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tarsil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["tarsil"]}},"created_at":"2023-03-17T09:41:00.000Z","updated_at":"2024-10-18T22:34:26.000Z","dependencies_parsed_at":"2023-05-11T03:45:08.759Z","dependency_job_id":"0ac25359-5de5-4b11-9569-3c00b155753b","html_url":"https://github.com/tarsil/starlette-bridge","commit_stats":{"total_commits":49,"total_committers":2,"mean_commits":24.5,"dds":"0.36734693877551017","last_synced_commit":"0502f2fe986d052fa8e3c518873b50174b85665d"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/tarsil/starlette-bridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarsil%2Fstarlette-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarsil%2Fstarlette-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarsil%2Fstarlette-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarsil%2Fstarlette-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tarsil","download_url":"https://codeload.github.com/tarsil/starlette-bridge/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarsil%2Fstarlette-bridge/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262685245,"owners_count":23348413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","esmerald","http","python","python3","starlette","websockets"],"created_at":"2024-10-02T07:58:21.346Z","updated_at":"2025-07-05T18:33:16.786Z","avatar_url":"https://github.com/tarsil.png","language":"Python","funding_links":["https://github.com/sponsors/tarsil"],"categories":["Extensions"],"sub_categories":["Core Utilities"],"readme":"# Starlette Bridge\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://starlette-bridge.tarsild.io\"\u003e\u003cimg width=\"420px\" src=\"https://www.starlette.io/img/starlette.png\" alt='Starlette Bridge'\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cem\u003e🚀 The Starlette events bridge that you need. 🚀\u003c/em\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/tarsil/starlette-bridge/workflows/Test%20Suite/badge.svg?event=push\u0026branch=main\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://github.com/tarsil/starlette-bridge/workflows/Test%20Suite/badge.svg?event=push\u0026branch=main\" alt=\"Test Suite\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://pypi.org/project/starlette-bridge\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/v/starlette-bridge?color=%2334D058\u0026label=pypi%20package\" alt=\"Package version\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://pypi.org/project/starlette-bridge\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/pyversions/starlette-bridge.svg?color=%2334D058\" alt=\"Supported Python versions\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n**Documentation**: [https://starlette-bridge.tarsild.io](https://starlette-bridge.tarsild.io) 📚\n\n**Source Code**: [https://github.com/tarsil/starlette-bridge](https://github.com/tarsil/starlette-bridge)\n\n---\n\n## Motivation\n\nStarlette has evolved and it will keep on growing. In the release 0.26+ it was announced the\n`on_startup` and `on_shutdown` events would be removed in favour of the newly `lifepsan` and those\nwould be officially removed in the release 1.0.\n\nThe problem is the fact that there are a lof od packages out there still using the old ways of\nusing the events and this change will introduce breaking changes.\n\nThis bridge will make sure that doesn't happen and you can still use the old form of using the\n`on_startup` and `on_shutdown` without breaking the `lifespan` from Starlette.\n\nWhat `Starlette Bridge` does for you is simple. It gets the `on_startup` and `on_shutdown` events\nin the same fashion you could do before and internally generates the newly `lifespan` for starlette.\n\nThat simple. Keeping the old syntax intact and using the newly `lifespan`.\n\n## Installation\n\nTo install Starlette Bridge, simply run:\n\n```shell\n$ pip install starlette-bridge\n```\n\n## How to use\n\nThis is actually very simple to do it. You don't need to do anything particularly difficult, in\nfact, you only need to update where your Starlette object comes from.\n\n```python hl_lines=\"1\"\nfrom starlette_bridge import Starlette\n\napp = Starlette()\n```\n\nAnd that is pretty much it.\n\n### How does it work\n\nStarlette bridge simply maps your `on_startup` and `on_shutdown` events and converts them into\nthe new `lifespan` async generator from `Starlette`.\n\nThis way you can continue to use your preferred way of assembling the events while maintaining\nthe new structure required by Starlette for managing events.\n\n### on_event and add_event_handler\n\nThese two pieces of functionality are also supported by the bridge making sure that what you had\nin the past, still remains working as is without changing the syntax.\n\nLet us see an example how it works. We will be using [Starlette Bridge](https://saffier.tarsild.io) because\nalready contains events we want to use.\n\n#### on_startup/on_shutdown\n\nUsing the `on_startup` and `on_shutdown`.\n\n```python hl_lines=\"3 10-11\"\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\napp = Starlette(\n    on_startup=[database.connect],\n    on_shutdown=[database.disconnect],\n)\n```\n\n#### Lifespan\n\nYou can, of course, use the lifespan as well.\n\n```python hl_lines=\"5 20\"\nfrom contextlib import asynccontextmanager\n\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\n@asynccontextmanager\nasync def lifespan(app: Starlette):\n    # On startup\n    await database.connect()\n    yield\n    # On shutdown\n    await database.disconnect()\n\n\napp = Starlette(lifespan=lifespan)\n```\n\n#### on_event and add_event_handler\n\nAs mentioned before, those two functionalities are also available.\n\n##### on_event\n\n```python hl_lines=\"3 12 17\"\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\napp = Starlette()\n\n\n@app.on_event(\"startup\")\nasync def start_database():\n    await database.connect()\n\n\n@app.on_event(\"shutdown\")\nasync def close_database():\n    await database.disconnect()\n```\n\n##### add_event_handler\n\n```python hl_lines=\"3 10-11\"\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\napp = Starlette()\napp.add_event_handler(\"startup\", database.connect)\napp.add_event_handler(\"shutdown\", database.disconnect)\n```\n\n## Notes\n\nThis is from the same author of [Esmerald](https://esmerald.dev),\n[Saffier](https://saffier.tarsild.io) and [Asyncz](https://asyncz.tarsild.io). Have a look around\nthose techologies as well 😄.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarsil%2Fstarlette-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftarsil%2Fstarlette-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarsil%2Fstarlette-bridge/lists"}