Mostly working index, database and table pages

This commit is contained in:
Simon Willison 2018-07-26 06:07:24 -07:00
commit 563fab27cc
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
3 changed files with 46 additions and 10 deletions

View file

@ -431,13 +431,45 @@ class Datasette:
) )
def asgi_app(self): 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([ return Router([
Path('/', app=IndexView(self).asgi_app(), methods=['GET']), Path(
'/(?P<as_format>\.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<db_name>[^/]+?)(?P<as_format>(\.jsono?|\.csv))?$",
app=DatabaseView(self).asgi_app(),
),
Path(
"/(?P<db_name>[^/]+?)/(?P<table_and_format>[^/]+?)$",
app=TableView(self).asgi_app(),
),
]) ])
# app.add_route(
# DatabaseView.as_view(self), "/<db_name:[^/]+?><as_format:(\.jsono?|\.csv)?$>"
# )
# app.add_route(
# TableView.as_view(self),
# "/<db_name:[^/]+>/<table_and_format:[^/]+?$>",
# )
def app(self): def configure_jinja(self):
app = Sanic(__name__)
default_templates = str(app_root / "datasette" / "templates") default_templates = str(app_root / "datasette" / "templates")
template_paths = [] template_paths = []
if self.template_dir: if self.template_dir:
@ -465,6 +497,10 @@ class Datasette:
self.jinja_env.filters["escape_sqlite"] = escape_sqlite self.jinja_env.filters["escape_sqlite"] = escape_sqlite
self.jinja_env.filters["to_css_class"] = to_css_class self.jinja_env.filters["to_css_class"] = to_css_class
pm.hook.prepare_jinja2_environment(env=self.jinja_env) 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), "/<as_format:(\.jsono?)?$>") app.add_route(IndexView.as_view(self), "/<as_format:(\.jsono?)?$>")
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires # TODO: /favicon.ico and /-/static/ deserve far-future cache expires
app.add_route(favicon, "/favicon.ico") app.add_route(favicon, "/favicon.ico")

View file

@ -377,8 +377,8 @@ def serve(
# Force initial hashing/table counting # Force initial hashing/table counting
ds.inspect() ds.inspect()
if asgi: if asgi:
from uvicorn.run import UvicornServer import uvicorn
app = ds.asgi_app() app = ds.asgi_app()
UvicornServer().run(app, host=host, port=port) uvicorn.run(app, host, port, log_level="info")
else: else:
ds.app().run(host=host, port=port, debug=debug) ds.app().run(host=host, port=port, debug=debug)

View file

@ -56,21 +56,21 @@ class RequestWrapper:
@property @property
def query_string(self): def query_string(self):
return str(self._request.query_string) return str(self._request._scope["query_string"])
@property @property
def args(self): def args(self):
# Key/list-of-values # Key/list-of-values
# There's probably a better way to do this: # There's probably a better way to do this:
d = {} d = {}
for key, value in self._request.query_string: for key, value in self._request.query_params:
d.setdefault(key, []).append(value) d.setdefault(key, []).append(value)
return d return d
@property @property
def raw_args(self): def raw_args(self):
# Flat key/first-value dictionary # Flat key/first-value dictionary
return self._request.query_string._dict return dict(self.args)
class RenderMixin(HTTPMethodView): class RenderMixin(HTTPMethodView):