Skip to content

Commit

Permalink
feat: Make local upload work with baseurl as well. #982
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Jan 20, 2023
1 parent 7977bca commit c106ba7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
7 changes: 4 additions & 3 deletions py/Makefile
Expand Up @@ -35,9 +35,10 @@ docs: ## Build API docs
./venv/bin/python sync_examples.py

test:
./venv/bin/python -m tests
echo "Testing using BASE_URL" && H2O_WAVE_BASE_URL="/foo/" ./venv/bin/python -m tests
echo "Testing using LOCAL UPLOAD" && H2O_WAVE_WAVED_DIR=".." ./venv/bin/python -m tests
# ./venv/bin/python -m tests
# echo "Testing using BASE_URL" && H2O_WAVE_BASE_URL="/foo/" ./venv/bin/python -m tests
# echo "Testing using LOCAL UPLOAD" && H2O_WAVE_WAVED_DIR=".." ./venv/bin/python -m tests
echo "Testing using LOCAL UPLOAD AND BASE URL" && H2O_WAVE_WAVED_DIR=".." H2O_WAVE_BASE_URL="/foo/" ./venv/bin/python -m tests

purge: ## Purge previous build
rm -rf build dist h2o_wave.egg-info
Expand Down
13 changes: 8 additions & 5 deletions py/h2o_wave/core.py
Expand Up @@ -45,9 +45,10 @@ def _get_env(key: str, value: Any):


_default_internal_address = 'http://127.0.0.1:8000'
_waved_dir = _get_env('H2O_WAVE_WAVED_DIR', None)
_data_dir = _get_env('H2O_WAVE_DATA_DIR', 'data')
_skip_local_upload = _get_env('H2O_WAVE_SKIP_LOCAL_UPLOAD', 'false').lower() in ['true', '1', 't']
_waved_dir = _get_env('WAVED_DIR', None)
_data_dir = _get_env('DATA_DIR', 'data')
_skip_local_upload = _get_env('SKIP_LOCAL_UPLOAD', 'false').lower() in ['true', '1', 't']
_base_url = _get_env('BASE_URL', '/')

# If we know the path of waved and running app on the same machine,
# we can simply copy the files instead of making an HTTP request.
Expand Down Expand Up @@ -690,7 +691,7 @@ def upload(self, files: List[str]) -> List[str]:
_, err = subprocess.Popen(args, stderr=subprocess.PIPE).communicate()
if err:
raise ValueError(err.decode())
uploaded_files.append(f'/_f/{uuid}/{os.path.basename(f)}')
uploaded_files.append(f'{_base_url}_f/{uuid}/{os.path.basename(f)}')
return uploaded_files
except Exception as e:
print(f'Error during local copy, falling back to HTTP upload: {e}')
Expand Down Expand Up @@ -725,6 +726,7 @@ def upload_dir(self, directory: str) -> str:
if not os.path.isdir(directory):
raise ValueError(f'{directory} is not a directory.')

# TODO: Keep dir structure.
if _use_local_upload:
try:
is_windows = 'Windows' in platform.system()
Expand Down Expand Up @@ -901,6 +903,7 @@ async def upload_dir(self, directory: str) -> str:
if not os.path.isdir(directory):
raise ValueError(f'{directory} is not a directory.')

# TODO: Keep dir structure.
if _use_local_upload:
try:
is_windows = 'Windows' in platform.system()
Expand Down Expand Up @@ -970,7 +973,7 @@ async def upload(self, files: List[str]) -> List[str]:
_, err = subprocess.Popen(args, stderr=subprocess.PIPE).communicate()
if err:
raise ValueError(err.decode())
uploaded_files.append(f'/_f/{uuid}/{os.path.basename(f)}')
uploaded_files.append(f'{_base_url}_f/{uuid}/{os.path.basename(f)}')
return uploaded_files
except Exception as e:
print(f'Error during local copy, falling back to HTTP upload: {e}')
Expand Down
2 changes: 2 additions & 0 deletions py/tests/test_python_server_async.py
Expand Up @@ -72,7 +72,9 @@ async def test_upload_dir(self):
assert len(txt) > 0

async def test_deleting_files(self):
base_url = os.getenv('H2O_WAVE_BASE_URL', '/')
upload_path, = await self.site.upload([os.path.join('tests', 'test_folder', 'test.txt')])
assert base_url in upload_path
res = httpx.get(f'http://localhost:10101{upload_path}')
assert res.status_code == 200
await self.site.unload(upload_path)
Expand Down

0 comments on commit c106ba7

Please sign in to comment.