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

Add port parameter to local command #220

Merged
merged 1 commit into from
Jan 26, 2017
Merged
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
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,10 @@ We can run ``chalice local`` to test this API locally:
$ chalice local
Serving on localhost:8000

We can override the port using:

$ chalice --port=8080 local

We can now test our API using ``localhost:8000``::

$ http localhost:8000/
Expand Down
1 change: 0 additions & 1 deletion chalice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
NotFoundError, ConflictError, TooManyRequestsError
)


__version__ = '0.5.1'
13 changes: 7 additions & 6 deletions chalice/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,18 @@ def cli(ctx, project_dir, debug=False):


@cli.command()
@click.option('--port', default=8000, type=click.INT)
@click.pass_context
def local(ctx):
# type: (click.Context) -> None
def local(ctx, port=8000):
# type: (click.Context, int) -> None
app_obj = load_chalice_app(ctx.obj['project_dir'])
# 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.
# The app-specific logger (app.log) will still continue
# to work.
logging.basicConfig(stream=sys.stdout)
run_local_server(app_obj)
run_local_server(app_obj, port)


@cli.command()
Expand Down Expand Up @@ -323,10 +324,10 @@ def generate_sdk(ctx, sdk_type, outdir):
sdk_type=sdk_type)


def run_local_server(app_obj):
# type: (Chalice) -> None
def run_local_server(app_obj, port):
# type: (Chalice, int) -> None
from chalice.local import LocalDevServer
server = LocalDevServer(app_obj)
server = LocalDevServer(app_obj, port)
server.serve_forever()


Expand Down
9 changes: 5 additions & 4 deletions chalice/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,20 @@ def _send_autogen_options_response(self):


class LocalDevServer(object):
def __init__(self, app_object, handler_cls=ChaliceRequestHandler,
def __init__(self, app_object, port, handler_cls=ChaliceRequestHandler,
server_cls=HTTPServer):
# type: (Chalice, HandlerCls, ServerCls) -> None
# type: (Chalice, int, HandlerCls, ServerCls) -> None
self.app_object = app_object
self.port = port
self._wrapped_handler = functools.partial(
handler_cls, app_object=app_object)
self.server = server_cls(('', 8000), self._wrapped_handler)
self.server = server_cls(('', port), self._wrapped_handler)

def handle_single_request(self):
# type: () -> None
self.server.handle_request()

def serve_forever(self):
# type: () -> None
print "Serving on localhost:8000"
print "Serving on localhost:%s" % self.port
self.server.serve_forever()