-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add dev command #83
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
Conversation
46196a4
to
f454967
Compare
edb93c7
to
c2f62d2
Compare
app.Serverless = local_app.ServerlessLocal | ||
scw_serverless.Serverless = local_app.ServerlessLocal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is of course very hacky but it's hard to find a better way as:
- the instantiation of the Serverless class is done in the client's code
- the binding to the handlers is done via decorators, and thus already done when we import a global instance (via loader.load_app_instance) so we need to "act" before that
My first approach was to add a handler parameter to the functions which would store the python handler as an object. This doesn't work well with multiprocessing (can't pickle a Python function) and I don't like it (may introduce side effects when deploying). Ideally, local testing should not modify the deploy command.
The second approach could have been a global Python variable LOCAL_TESTING=False
that changes the behavior of the Serverless class. But this results in some conditionals imports as we do not want to import the local testing framework in app.py to avoid packaging issues when deploying.
So finally we override the class directly with a derived class. This is not ideal but it's quite clean compared to those alternatives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yes, a bit of black magic but it works for now 🧙
README.md
Outdated
@@ -59,6 +59,12 @@ def hello_world(event, context): | |||
The configuration is done by passing arguments to the decorator. | |||
To view which arguments are supported, head over to this [documentation](https://serverless-api-framework-python.readthedocs.io/) page. | |||
|
|||
Before deploying, you can run your function locally with the dev command: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this deserves its own subsection, it's really cool!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
app.Serverless = local_app.ServerlessLocal | ||
scw_serverless.Serverless = local_app.ServerlessLocal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yes, a bit of black magic but it works for now 🧙
Summary
What's changed?
This adds a "dev" command to the CLI that relies on the Python local testing framework the run the function handlers on a local Flask instance.
Why do we need this?
Makes it easier to do local testing before deploying a function.
How have you tested it?
Works with the examples.
Checklist
Details