Skip to content

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

Merged
merged 10 commits into from
May 11, 2023
Merged

feat: add dev command #83

merged 10 commits into from
May 11, 2023

Conversation

cyclimse
Copy link
Contributor

@cyclimse cyclimse commented Apr 27, 2023

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

  • I have reviewed this myself
  • There is a unit test covering every change in this PR
  • I have updated the relevant documentation

Details

@cyclimse cyclimse force-pushed the feat/add-dev-command branch from 46196a4 to f454967 Compare May 9, 2023 15:41
@cyclimse cyclimse force-pushed the feat/add-dev-command branch from edb93c7 to c2f62d2 Compare May 10, 2023 13:42
Comment on lines +171 to +172
app.Serverless = local_app.ServerlessLocal
scw_serverless.Serverless = local_app.ServerlessLocal
Copy link
Contributor Author

@cyclimse cyclimse May 10, 2023

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.

Copy link
Collaborator

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 🧙

@cyclimse cyclimse requested review from Shillaker and redanrd May 10, 2023 14:07
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:
Copy link
Collaborator

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ☺️ ! So happy to see both projects being combined 🎉

Comment on lines +171 to +172
app.Serverless = local_app.ServerlessLocal
scw_serverless.Serverless = local_app.ServerlessLocal
Copy link
Collaborator

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 🧙

@cyclimse cyclimse merged commit cd56d31 into main May 11, 2023
@cyclimse cyclimse deleted the feat/add-dev-command branch May 11, 2023 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants