Refactored everything into a factory function

I now call a factory function to construct the Sanic app:

    app = app_factory(files)

This allows me to pass additional arguments to it, e.g. the files to serve.

Also refactored my class-based views to accept jinja as an argument, e.g:

    app.add_route(
        TableView.as_view(jinja),
        '/<db_name:[^/]+>/<table:[^/]+?><as_json:(.jsono?)?$>'
    )
This commit is contained in:
Simon Willison 2017-11-04 19:13:44 -07:00
commit 1fc75809a6
2 changed files with 61 additions and 41 deletions

View file

@ -1,6 +1,6 @@
import click
from click_default_group import DefaultGroup
from .app import app, ensure_build_metadata
from .app import app_factory, ensure_build_metadata
@click.group(cls=DefaultGroup, default='serve', default_if_no_args=True)
@ -23,4 +23,5 @@ def build():
def serve(files, host, port, debug):
"""Serve up specified database files with a web UI"""
click.echo('Serve! files={} on port {}'.format(files, port))
app = app_factory(files)
app.run(host=host, port=port, debug=debug)