Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local WebSocket Server implementation #2077

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ class WebsocketAPI(object):
def __init__(self, env: Optional[MutableMapping] = None) -> None:
self.session: Optional[Any] = None
self._endpoint: Optional[str] = None
self._client = None
self._client: Optional[Any] = None
if env is None:
self._env: MutableMapping = os.environ
else:
Expand Down
27 changes: 17 additions & 10 deletions chalice/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,27 @@ def _configure_cli_env_vars():
@cli.command()
@click.option('--host', default='127.0.0.1')
@click.option('--port', default=8000, type=click.INT)
@click.option('--ws-host')
@click.option('--ws-port', type=click.INT)
@click.option('--stage', default=DEFAULT_STAGE_NAME,
help='Name of the Chalice stage for the local server to use.')
@click.option('--autoreload/--no-autoreload',
default=True,
help='Automatically restart server when code changes.')
@click.pass_context
def local(ctx, host='127.0.0.1', port=8000, stage=DEFAULT_STAGE_NAME,
autoreload=True):
# type: (click.Context, str, int, str, bool) -> None
autoreload=True, ws_host=None, ws_port=None):
# type: (click.Context, str, int, str, bool, Optional[str], Optional[port]) -> None
factory = ctx.obj['factory'] # type: CLIFactory
from chalice.cli import reloader
# We don't create the server here because that will bind the
# socket and we only want to do this in the worker process.
if ws_host is None:
ws_host = host
if ws_port is None:
ws_port = port + 1
server_factory = functools.partial(
create_local_server, factory, host, port, stage)
create_local_server, factory, host, port, stage, ws_host, ws_port)
# When running `chalice local`, a stdout logger is configured
# so you'll see the same stdout logging as you would when
# running in lambda. This is configuring the root logger.
Expand All @@ -133,11 +139,11 @@ def local(ctx, host='127.0.0.1', port=8000, stage=DEFAULT_STAGE_NAME,
# recommended way to do this is to use sys.exit() directly,
# see: https://github.com/pallets/click/issues/747
sys.exit(rc)
run_local_server(factory, host, port, stage)
run_local_server(factory, host, port, stage, ws_host, ws_port)


def create_local_server(factory, host, port, stage):
# type: (CLIFactory, str, int, str) -> LocalDevServer
def create_local_server(factory, host, port, stage, ws_host, ws_port):
# type: (CLIFactory, str, int, str, str, int) -> LocalDevServer
config = factory.create_config_obj(
chalice_stage_name=stage
)
Expand All @@ -146,13 +152,14 @@ def create_local_server(factory, host, port, stage):
# there is no point in testing locally.
routes = config.chalice_app.routes
validate_routes(routes)
server = factory.create_local_server(app_obj, config, host, port)
server = factory.create_local_server(
app_obj, config, host, port, ws_host, ws_port)
return server


def run_local_server(factory, host, port, stage):
# type: (CLIFactory, str, int, str) -> None
server = create_local_server(factory, host, port, stage)
def run_local_server(factory, host, port, stage, ws_host, ws_port):
# type: (CLIFactory, str, int, str, str, int) -> None
server = create_local_server(factory, host, port, stage, ws_host, ws_port)
server.serve_forever()


Expand Down
7 changes: 5 additions & 2 deletions chalice/cli/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,12 @@ def load_project_config(self) -> Dict[str, Any]:
return json.loads(f.read())

def create_local_server(
self, app_obj: Chalice, config: Config, host: str, port: int
self, app_obj: Chalice, config: Config,
host: str, port: int,
ws_host: str, ws_port: int
) -> local.LocalDevServer:
return local.create_local_server(app_obj, config, host, port)
return local.create_local_server(
app_obj, config, host, port, ws_host, ws_port)

def create_package_options(self) -> PackageOptions:
"""Create the package options that are required to target regions."""
Expand Down
Loading