Skip to content

Commit

Permalink
customizable index files
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Nov 28, 2024
1 parent 81400e1 commit 2dc603d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 8 additions & 1 deletion httpout/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def usage(**context):
print(' E.g. "/path/to/fullchain.pem"')
print(' --ssl-key SSL private key location')
print(' E.g. "/path/to/privkey.pem"')
print(' --directory-index Index files to be served on directory-based URLs') # noqa: E501
print(' Must be separated by commas. E.g. "index.py,index.html"') # noqa: E501
print(' --debug Enable debug mode')
print(' Intended for development')
print(' --log-level Defaults to "DEBUG". See')
Expand Down Expand Up @@ -85,9 +87,14 @@ def threads(value, **context):
return 1


def indexes(value, **context):
context['options']['directory_index'] = value.split(',')


if __name__ == '__main__':
options = tremolo.utils.parse_args(
help=usage, bind=bind, version=version, thread_pool_size=threads
help=usage, bind=bind, version=version, thread_pool_size=threads,
directory_index=indexes
)

if sys.argv[-1] != sys.argv[0] and not sys.argv[-1].startswith('-'):
Expand Down
7 changes: 4 additions & 3 deletions httpout/httpout.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
is_safe_path, new_module, exec_module, cleanup_modules, mime_types
)

_INDEX_FILES = ('index.py', 'index.html')


class HTTPOut:
def __init__(self, app):
Expand All @@ -38,6 +36,9 @@ async def _on_worker_start(self, **worker):
g.options.get('document_root', os.getcwd())
)
g.options['document_root'] = document_root
g.options['directory_index'] = g.options.get(
'directory_index', ['index.py', 'index.html']
)

logger.info('entering directory: %s', document_root)
os.chdir(document_root)
Expand Down Expand Up @@ -206,7 +207,7 @@ async def _on_request(self, **server):
dirname = module_path

# no file extension in the URL, try index.py, index.html, etc.
for basename in _INDEX_FILES:
for basename in g.options['directory_index']:
module_path = os.path.join(dirname, basename)
ext = os.path.splitext(basename)[-1]

Expand Down

0 comments on commit 2dc603d

Please sign in to comment.