per host configurable static site configs

This commit is contained in:
John Chilton
2021-08-25 12:01:40 -04:00
parent 1a0cc6272d
commit 1765ed7c6d
3 changed files with 21 additions and 3 deletions
+12 -2
View File
@@ -8,9 +8,10 @@ from paste.urlparser import StaticURLParser
class CacheableStaticURLParser(StaticURLParser):
def __init__(self, directory, cache_seconds=None):
def __init__(self, directory, cache_seconds=None, directory_per_host=None):
StaticURLParser.__init__(self, directory)
self.cache_seconds = cache_seconds
self.directory_per_host = directory_per_host
def __call__(self, environ, start_response):
path_info = environ.get('PATH_INFO', '')
@@ -27,7 +28,16 @@ class CacheableStaticURLParser(StaticURLParser):
filename = 'index.html'
else:
filename = request.path_info_pop(environ)
full = os.path.join(self.directory, filename)
directory = self.directory
host = environ.get('HTTP_HOST')
if self.directory_per_host and host:
for host_key, host_val in self.directory_per_host.items():
if host_key in host:
directory = host_val
break
full = os.path.join(directory, filename)
if not os.path.exists(full):
return self.not_found(environ, start_response)
if os.path.isdir(full):
+3 -1
View File
@@ -1011,7 +1011,9 @@ def build_url_map(app, global_conf, local_conf):
def get_static_from_config(option_name, default_path):
config_val = conf.get(option_name, default_url_path(default_path))
return Static(config_val, cache_time)
per_host_config_option = f"{option_name}_by_host"
per_host_config = conf.get(per_host_config_option)
return Static(config_val, cache_time, directory_per_host=per_host_config)
# Define static mappings from config
urlmap["/static"] = get_static_from_config("static_dir", "static/")
@@ -1479,6 +1479,7 @@ mapping:
static_dir:
type: str
default: static/
per_host: true
required: false
desc: |
Serve static content, which must be enabled if you're not serving it via a
@@ -1489,6 +1490,7 @@ mapping:
static_images_dir:
type: str
default: static/images
per_host: true
required: false
desc: |
Serve static content, which must be enabled if you're not serving it via a
@@ -1499,6 +1501,7 @@ mapping:
static_favicon_dir:
type: str
default: static/favicon.ico
per_host: true
required: false
desc: |
Serve static content, which must be enabled if you're not serving it via a
@@ -1509,6 +1512,7 @@ mapping:
static_scripts_dir:
type: str
default: static/scripts/
per_host: true
required: false
desc: |
Serve static content, which must be enabled if you're not serving it via a
@@ -1519,6 +1523,7 @@ mapping:
static_style_dir:
type: str
default: static/style
per_host: true
required: false
desc: |
Serve static content, which must be enabled if you're not serving it via a
@@ -1529,6 +1534,7 @@ mapping:
static_robots_txt:
type: str
default: static/robots.txt
per_host: true
required: false
desc: |
Serve static content, which must be enabled if you're not serving it via a