diff --git a/datasette/app.py b/datasette/app.py index df6b6d60..011002ee 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -135,7 +135,9 @@ CONFIG_OPTIONS = ( False, "Allow display of template debug information with ?_context=1", ), + ConfigOption("base_url", "/", "Datasette URLs should use this base"), ) + DEFAULT_CONFIG = {option.name: option.default for option in CONFIG_OPTIONS} @@ -573,6 +575,7 @@ class Datasette: "format_bytes": format_bytes, "extra_css_urls": self._asset_urls("extra_css_urls", template, context), "extra_js_urls": self._asset_urls("extra_js_urls", template, context), + "base_url": self.config("base_url"), }, **extra_template_vars, } @@ -736,6 +739,13 @@ class DatasetteRouter(AsgiRouter): self.ds = datasette super().__init__(routes) + async def route_path(self, scope, receive, send, path): + # Strip off base_url if present before routing + base_url = self.ds.config("base_url") + if base_url != "/" and path.startswith(base_url): + path = "/" + path[len(base_url) :] + return await super().route_path(scope, receive, send, path) + async def handle_404(self, scope, receive, send): # If URL has a trailing slash, redirect to URL without it path = scope.get("raw_path", scope["path"].encode("utf8")) diff --git a/datasette/cli.py b/datasette/cli.py index 77ab3542..94da6ee4 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -27,7 +27,7 @@ class Config(click.ParamType): if ":" not in config: self.fail('"{}" should be name:value'.format(config), param, ctx) return - name, value = config.split(":") + name, value = config.split(":", 1) if name not in DEFAULT_CONFIG: self.fail( "{} is not a valid option (--help-config to see all)".format(name), @@ -50,6 +50,8 @@ class Config(click.ParamType): self.fail('"{}" should be an integer'.format(name), param, ctx) return return name, int(value) + elif isinstance(default, str): + return name, value else: # Should never happen: self.fail("Invalid option") diff --git a/datasette/templates/_codemirror.html b/datasette/templates/_codemirror.html index 17342be5..e450f31e 100644 --- a/datasette/templates/_codemirror.html +++ b/datasette/templates/_codemirror.html @@ -1,7 +1,7 @@ - - - - + + + +