Skip to content

Commit

Permalink
add Falcon asgi template (#1139)
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
sinisaos authored Jan 26, 2025
1 parent fc7e44d commit 55c2dbc
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
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 %}

0 comments on commit 55c2dbc

Please sign in to comment.