diff --git a/datasette/app.py b/datasette/app.py index ae6c11a6..0307828e 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -431,13 +431,45 @@ class Datasette: ) def asgi_app(self): - from starlette import Router, Path + self.configure_jinja() + from starlette.routing import Router, Path, PathPrefix + from starlette.staticfiles import StaticFile, StaticFiles return Router([ - Path('/', app=IndexView(self).asgi_app(), methods=['GET']), + Path( + '/(?P\.jsono?)?$', + app=IndexView(self).asgi_app(), + methods=['GET'] + ), + Path( + '/favicon.ico', + app=StaticFile( + path=str(app_root / "datasette" / "static" / "favicon.ico") + ) + ), + PathPrefix( + '/-/static/', + app=StaticFiles( + directory=str(app_root / "datasette" / "static") + ) + ), + Path( + "/(?P[^/]+?)(?P(\.jsono?|\.csv))?$", + app=DatabaseView(self).asgi_app(), + ), + Path( + "/(?P[^/]+?)/(?P[^/]+?)$", + app=TableView(self).asgi_app(), + ), ]) + # app.add_route( + # DatabaseView.as_view(self), "/" + # ) + # app.add_route( + # TableView.as_view(self), + # "//", + # ) - def app(self): - app = Sanic(__name__) + def configure_jinja(self): default_templates = str(app_root / "datasette" / "templates") template_paths = [] if self.template_dir: @@ -465,6 +497,10 @@ class Datasette: self.jinja_env.filters["escape_sqlite"] = escape_sqlite self.jinja_env.filters["to_css_class"] = to_css_class pm.hook.prepare_jinja2_environment(env=self.jinja_env) + + def app(self): + app = Sanic(__name__) + self.configure_jinja() app.add_route(IndexView.as_view(self), "/") # TODO: /favicon.ico and /-/static/ deserve far-future cache expires app.add_route(favicon, "/favicon.ico") diff --git a/datasette/cli.py b/datasette/cli.py index 07c8bc99..a9e69877 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -377,8 +377,8 @@ def serve( # Force initial hashing/table counting ds.inspect() if asgi: - from uvicorn.run import UvicornServer + import uvicorn app = ds.asgi_app() - UvicornServer().run(app, host=host, port=port) + uvicorn.run(app, host, port, log_level="info") else: ds.app().run(host=host, port=port, debug=debug) diff --git a/datasette/views/base.py b/datasette/views/base.py index 6e6cb342..2e2d981d 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -56,21 +56,21 @@ class RequestWrapper: @property def query_string(self): - return str(self._request.query_string) + return str(self._request._scope["query_string"]) @property def args(self): # Key/list-of-values # There's probably a better way to do this: d = {} - for key, value in self._request.query_string: + for key, value in self._request.query_params: d.setdefault(key, []).append(value) return d @property def raw_args(self): # Flat key/first-value dictionary - return self._request.query_string._dict + return dict(self.args) class RenderMixin(HTTPMethodView):