-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
undefined
- Loading branch information
Showing
7 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
piccolo/apps/asgi/commands/templates/app/_falcon_app.py.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
piccolo/apps/asgi/commands/templates/app/home/_falcon_endpoints.py.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters