Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Falcon asgi template #1139

Merged
merged 4 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Let Piccolo scaffold you an ASGI web app, using Piccolo as the ORM:
piccolo asgi new
```

[Starlette](https://www.starlette.io/), [FastAPI](https://fastapi.tiangolo.com/), [BlackSheep](https://www.neoteroi.dev/blacksheep/), [Litestar](https://litestar.dev/), [Esmerald](https://esmerald.dev/), [Lilya](https://lilya.dev) and [Quart](https://quart.palletsprojects.com/en/latest/) are currently supported.
[Starlette](https://www.starlette.io/), [FastAPI](https://fastapi.tiangolo.com/), [BlackSheep](https://www.neoteroi.dev/blacksheep/), [Litestar](https://litestar.dev/), [Esmerald](https://esmerald.dev/), [Lilya](https://lilya.dev), [Quart](https://quart.palletsprojects.com/en/latest/) and [Falcon](https://falconframework.org/) are currently supported.

## Are you a Django user?

Expand Down
1 change: 1 addition & 0 deletions piccolo/apps/asgi/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"esmerald": ["esmerald"],
"lilya": ["lilya"],
"quart": ["quart", "quart_schema"],
"falcon": ["falcon"],
}
ROUTERS = list(ROUTER_DEPENDENCIES.keys())

Expand Down
60 changes: 60 additions & 0 deletions piccolo/apps/asgi/commands/templates/app/_falcon_app.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import typing as t

import falcon.asgi
from hypercorn.middleware import DispatcherMiddleware
from piccolo.engine import engine_finder
from piccolo_admin.endpoints import create_admin
from piccolo_api.crud.endpoints import PiccoloCRUD

from home.endpoints import HomeEndpoint
from home.piccolo_app import APP_CONFIG
from home.tables import Task


async def open_database_connection_pool():
try:
engine = engine_finder()
await engine.start_connection_pool()
except Exception:
print("Unable to connect to the database")


async def close_database_connection_pool():
try:
engine = engine_finder()
await engine.close_connection_pool()
except Exception:
print("Unable to connect to the database")


class LifespanMiddleware:
async def process_startup(
self, scope: t.Dict[str, t.Any], event: t.Dict[str, t.Any]
) -> None:
await open_database_connection_pool()

async def process_shutdown(
self, scope: t.Dict[str, t.Any], event: t.Dict[str, t.Any]
) -> None:
await close_database_connection_pool()


app: t.Any = falcon.asgi.App(middleware=LifespanMiddleware())
app.add_static_route("/static", directory=os.path.abspath("static"))
app.add_route("/", HomeEndpoint())

PICCOLO_CRUD: t.Any = PiccoloCRUD(table=Task)

# enable the Admin and PiccoloCrud app using DispatcherMiddleware
app = DispatcherMiddleware( # type: ignore
{
"/admin": create_admin(
tables=APP_CONFIG.table_classes,
# Required when running under HTTPS:
# allowed_hosts=['my_site.com']
),
"/tasks": PICCOLO_CRUD,
"": app,
}
)
2 changes: 2 additions & 0 deletions piccolo/apps/asgi/commands/templates/app/app.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
{% include '_lilya_app.py.jinja' %}
{% elif router == 'quart' %}
{% include '_quart_app.py.jinja' %}
{% elif router == 'falcon' %}
{% include '_falcon_app.py.jinja' %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

import falcon
import jinja2

ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(
searchpath=os.path.join(os.path.dirname(__file__), "templates")
)
)


class HomeEndpoint:
async def on_get(self, req, resp):
template = ENVIRONMENT.get_template("home.html.jinja")
content = template.render(title="Piccolo + ASGI",)
resp.status = falcon.HTTP_200
resp.content_type = "text/html"
resp.text = content
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
{% include '_lilya_endpoints.py.jinja' %}
{% elif router == 'quart' %}
{% include '_quart_endpoints.py.jinja' %}
{% elif router == 'falcon' %}
{% include '_falcon_endpoints.py.jinja' %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<li><a href="/admin/">Admin</a></li>
<li><a href="/docs">Swagger API</a></li>
</ul>
<h3>Falcon</h3>
<ul>
<li><a href="/admin/">Admin</a></li>
<li><a href="/tasks/">JSON endpoint</a></li>
</ul>
</section>
</div>
{% endblock content %}
Loading