Skip to content

Commit

Permalink
Got it working with serialized JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 20, 2023
1 parent 68a6ab4 commit 830f2da
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions datasette_granian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from granian.constants import HTTPModes, Interfaces, Loops, ThreadModes
from granian.log import LogLevels
import click
import json


def load_app(target):
from datasette import cli

ds_kwargs = json.loads(target)
ds = cli.serve.callback(**ds_kwargs)
return ds.app()


invalid_options = {
"get",
Expand All @@ -26,10 +36,10 @@ def serve_with_granian(**kwargs):
# Need to add back default kwargs for everything in invalid_options:
kwargs.update({invalid_option: None for invalid_option in invalid_options})
kwargs["return_instance"] = True
ds = cli.serve.callback(**kwargs)
app = ds.app()
Granian(
app,

srv = Granian(
# Pass kwars as serialized JSON to the subprocess
json.dumps(kwargs),
address=host,
port=port,
interface=Interfaces.ASGI,
Expand All @@ -44,7 +54,8 @@ def serve_with_granian(**kwargs):
log_level=LogLevels.info.value,
ssl_cert=None,
ssl_key=None,
).serve()
)
srv.serve(target_loader=load_app)


@hookimpl
Expand Down

0 comments on commit 830f2da

Please sign in to comment.