Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Last active January 10, 2023 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewalk153/ac2fe326a4b379ae12a5cba81e4f3856 to your computer and use it in GitHub Desktop.
Save ewalk153/ac2fe326a4b379ae12a5cba81e4f3856 to your computer and use it in GitHub Desktop.
Extracted static path method to test behavior from piku static path mapper.
import unittest
from re import sub, match
from os.path import join, relpath
def echo(message):
# print(message)
pass
PIKU_INTERNAL_NGINX_STATIC_MAPPING = """
location $static_url {
sendfile on;
sendfile_max_chunk 1m;
tcp_nopush on;
directio 8m;
aio threads;
alias $static_path;
try_files $uri $uri.html $uri/ =404;
}
"""
def expandvars(buffer, env, default=None, skip_escaped=False):
"""expand shell-style environment variables in a buffer"""
def replace_var(match):
return env.get(match.group(2) or match.group(1), match.group(0) if default is None else default)
pattern = (r'(?<!\\)' if skip_escaped else '') + r'\$(\w+|\{([^}]*)\})'
return sub(pattern, replace_var, buffer)
def assign_nginx_static_path(nginx_static_paths, static_workers, app_path):
"""Get a mapping of /prefix1:path1,/prefix2:path2"""
piku_internal_nginx_static_mappings = ''
# Get a mapping of /prefix1:path1,/prefix2:path2
static_paths = nginx_static_paths
# prepend static worker path if present
if static_workers is not None:
stripped = static_workers.strip("/").rstrip("/")
static_paths = ("/" if stripped[0:1] == ":" else "/:") + (stripped if stripped else ".") + "/" + ("," if static_paths else "") + static_paths
if len(static_paths):
try:
items = static_paths.split(',')
for item in items:
static_url, static_path = item.split(':')
if static_path[0] != '/':
static_path = relpath(join(app_path, static_path)) + "/"
echo("-----> nginx will map {} to {}.".format(static_url, static_path))
piku_internal_nginx_static_mappings = piku_internal_nginx_static_mappings + expandvars(
PIKU_INTERNAL_NGINX_STATIC_MAPPING, locals())
except Exception as e:
echo("Error {} in static path spec: should be /prefix1:path1[,/prefix2:path2], ignoring.".format(e))
piku_internal_nginx_static_mappings = ''
return piku_internal_nginx_static_mappings
class TestStringMethods(unittest.TestCase):
def test_nginx_static_path_simple_path(self):
self.assertEqual(assign_nginx_static_path('', 'public,/somepath:somedir', 'basedir'), assign_nginx_static_path('', 'public/,/somepath:somedir', 'basedir'))
def test_nginx_static_path_empty(self):
self.assertRegex(assign_nginx_static_path('', '', 'basedir'), r'basedir/')
def test_nginx_static_path_example(self):
self.assertEqual(assign_nginx_static_path('', '/:public,/somepath:somedir', 'basedir'), assign_nginx_static_path('', '/:public/,/somepath:somedir', 'basedir'))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment