Skip to content

Commit

Permalink
feat: Allow specifying remote host.
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Jun 19, 2023
1 parent a180c44 commit 2dfe8ab
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions py/h2o_wave/h2o_wave/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import time
from contextlib import closing
from pathlib import Path
from threading import Thread
from urllib import request
from urllib.parse import urlparse

Expand Down Expand Up @@ -125,15 +124,18 @@ def run(app: str, no_reload: bool, no_autostart: bool):
# Try to start Wave daemon if not running or turned off.
server_port = int(os.environ.get('H2O_WAVE_LISTEN', ':10101').split(':')[-1])
server_not_running = _scan_free_port(server_port) == server_port

waved_process = None
if no_autostart:
autostart = False
else:
autostart = os.environ.get('H2O_WAVE_NO_AUTOSTART', 'false').lower() in ['false', '0', 'f']

waved = 'waved.exe' if 'Windows' in platform.system() else './waved'
# OS agnostic wheels do not have waved - needed for HAC.
is_waved_present = os.path.isfile(os.path.join(sys.exec_prefix, waved))

try:
waved = 'waved.exe' if 'Windows' in platform.system() else './waved'
waved_process = None
# OS agnostic wheels do not have waved - needed for HAC.
is_waved_present = os.path.isfile(os.path.join(sys.exec_prefix, waved))
if no_autostart:
autostart = False
else:
autostart = os.environ.get('H2O_WAVE_NO_AUTOSTART', 'false').lower() in ['false', '0', 'f']
if autostart and is_waved_present and server_not_running:
waved_process = subprocess.Popen([waved], cwd=sys.exec_prefix, env=os.environ.copy(), shell=True)
time.sleep(1)
Expand Down Expand Up @@ -288,7 +290,8 @@ def learn():
@main.command()
@click.option('--port', default=10101, help='Port your app is running on (defaults to 10101).')
@click.option('--subdomain', default='?new', help='Subdomain to use. If not available, a random one is generated.')
def share(port: str, subdomain: str):
@click.option('--remote-host', default='h2oai.app', help='Remote host to use (defaults to h2oai.app).')
def share(port: int, subdomain: str, remote_host: str):
"""Share your locally running app with the world.
\b
Expand All @@ -311,7 +314,7 @@ async def wakeup():
loop.create_task(wakeup())

try:
loop.run_until_complete(_share(port, subdomain))
loop.run_until_complete(_share(port, subdomain, remote_host))
except KeyboardInterrupt:
tasks = asyncio.all_tasks(loop)
for task in tasks:
Expand All @@ -320,12 +323,12 @@ async def wakeup():
loop.close()


async def _share(port: str, subdomain: str):
async def _share(port: int, subdomain: str, remote_host: str):
if _scan_free_port(port) == port:
print(f'Could not connect to localhost:{port}. Please make sure your app is running.')
exit(1)

res = httpx.get(f'https://h2oai.app/{subdomain}', headers={'Content-Type': 'application/json'})
res = httpx.get(f'https://{remote_host}/{subdomain}', headers={'Content-Type': 'application/json'})
if res.status_code != 200:
print('Could not connect to the remote sharing server.')
exit(1)
Expand All @@ -341,10 +344,10 @@ async def _share(port: str, subdomain: str):
tasks = []
for _ in range(max_conn_count // step):
for _ in range(step):
tasks.append(asyncio.create_task(listen_on_socket('127.0.0.1', int(port), 'h2oai.app', res['port'])))
tasks.append(asyncio.create_task(listen_on_socket('127.0.0.1', port, remote_host, res['port'])))
await asyncio.sleep(1)
# Handle the rest if any.
for _ in range(max_conn_count % step):
tasks.append(asyncio.create_task(listen_on_socket('127.0.0.1', int(port), 'h2oai.app', res['port'])))
tasks.append(asyncio.create_task(listen_on_socket('127.0.0.1', port, remote_host, res['port'])))

await asyncio.gather(*tasks)

0 comments on commit 2dfe8ab

Please sign in to comment.