-
Notifications
You must be signed in to change notification settings - Fork 3
fix: can import multiple modules #43
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```{include} ../../../examples/multiple_modules/README.md | ||
``` | ||
|
||
## [Sources](https://github.com/scaleway/serverless-api-project/blob/main/examples/multiple_modules/app.py) | ||
|
||
```{literalinclude} ../../../examples/multiple_modules/app.py | ||
``` | ||
|
||
```{literalinclude} ../../../examples/multiple_modules/upload.py | ||
``` | ||
|
||
```{literalinclude} ../../../examples/multiple_modules/query.py | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
```{include} ../../../examples/pr_notifier/README.md | ||
``` | ||
|
||
## [Source](https://github.com/scaleway/serverless-api-project/blob/main/examples/github_actions/deploy.yml) | ||
## [Source](https://github.com/scaleway/serverless-api-project/blob/main/examples/pr_notifier/notifier.py) | ||
|
||
```{literalinclude} ../../../examples/pr_notifier/notifier.py | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# [Multiple Modules](https://github.com/scaleway/serverless-api-project/tree/main/examples/multiple_modules) | ||
|
||
An app to upload and query files to S3 Glacier on Scaleway split in multiple modules. | ||
|
||
The upload endpoint allows you to upload files to Glacier via the `file` form-data key: | ||
|
||
```console | ||
echo -e "Hello world!\n My contents will be stored in a bunker!" > myfile.dat | ||
curl -F file=@myfile.dat <upload-function-url> | ||
``` | ||
|
||
This example is there to showcase how to split handlers into different Python modules. | ||
|
||
## Deploying | ||
|
||
Deployment can be done with `scw_serverless`: | ||
|
||
```console | ||
pip install scw_serverless | ||
scw-serverless deploy app.py | ||
``` | ||
|
||
## Configuration | ||
|
||
Here's all the environments variables that needs to be passed when deploying: | ||
|
||
| Variable | Description | Required | | ||
|:----------------:|:---------------------------------------:|:------------------:| | ||
| `SCW_SECRET_KEY` | Secret key to use for S3 operations | :heavy_check_mark: | | ||
| `SCW_ACCESS_KEY` | Access key to use for S3 operations | :heavy_check_mark: | | ||
| `S3_BUCKET` | Name of the bucket to store files into. | :heavy_check_mark: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import logging | ||
import os | ||
|
||
from scw_serverless.app import Serverless | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
app = Serverless( | ||
"multiple-modules", | ||
secret={ | ||
"SCW_ACCESS_KEY": os.environ["SCW_ACCESS_KEY"], | ||
"SCW_SECRET_KEY": os.environ["SCW_SECRET_KEY"], | ||
}, | ||
env={"S3_BUCKET": os.environ["S3_BUCKET"]}, | ||
) | ||
|
||
import query # noqa | ||
import upload # pylint: disable=all # noqa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import os | ||
from typing import Any | ||
|
||
from app import app | ||
from s3 import bucket | ||
|
||
|
||
@app.func( | ||
description="List objects in S3 uploads.", | ||
privacy="public", | ||
env={"LIMIT": "100"}, | ||
min_scale=0, | ||
max_scale=2, | ||
memory_limit=128, | ||
timeout="300s", | ||
) | ||
def query(_event: dict[str, Any], _context: dict[str, Any]) -> dict[str, Any]: | ||
"""A handler to list objects in a S3 bucket.""" | ||
|
||
response = [] | ||
|
||
for obj in bucket.objects.limit(count=int(os.environ["LIMIT"])): | ||
response.append( | ||
{ | ||
"name": obj.key, | ||
"last_modified": obj.last_modified.strftime("%m/%d/%Y, %H:%M:%S"), | ||
"storage_class": obj.storage_class, | ||
} | ||
) | ||
|
||
return { | ||
"statusCode": 200, | ||
"headers": {"Content-Type": "application/json"}, | ||
"body": json.dumps(response), | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
boto3~=1.26 | ||
scw-serverless~=0.0.3 | ||
streaming_form_data~=1.11.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
|
||
import boto3 | ||
|
||
s3 = boto3.resource( | ||
"s3", | ||
region_name="fr-par", | ||
use_ssl=True, | ||
endpoint_url="https://s3.fr-par.scw.cloud", | ||
aws_access_key_id=os.environ["SCW_ACCESS_KEY"], | ||
aws_secret_access_key=os.environ["SCW_SECRET_KEY"], | ||
) | ||
|
||
bucket = s3.Bucket(os.environ["S3_BUCKET"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import logging | ||
from typing import Any | ||
|
||
from app import app | ||
from s3 import bucket | ||
from streaming_form_data import StreamingFormDataParser | ||
from streaming_form_data.targets import ValueTarget | ||
|
||
|
||
@app.func() | ||
def upload(event: dict[str, Any], _context: dict[str, Any]) -> dict[str, Any]: | ||
"""Upload form data to S3 Glacier.""" | ||
|
||
headers = event["headers"] | ||
parser = StreamingFormDataParser(headers=headers) | ||
|
||
target = ValueTarget() | ||
parser.register("file", target) | ||
|
||
body: str = event["body"] | ||
parser.data_received(body.encode("utf-8")) | ||
|
||
if not (len(target.value) > 0 and target.multipart_filename): | ||
return {"statusCode": 400} | ||
|
||
name = target.multipart_filename | ||
|
||
logging.info("Uploading file %s to Glacier on %s", name, bucket.name) | ||
bucket.put_object(Key=name, Body=target.value, StorageClass="GLACIER") | ||
|
||
return {"statusCode": 200} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import importlib.util | ||
import inspect | ||
import sys | ||
from pathlib import Path | ||
|
||
from scw_serverless.app import Serverless | ||
|
||
|
||
def get_module_name(file: Path) -> str: | ||
"""Extract the module name from the path.""" | ||
|
||
file = file.resolve() | ||
return ( | ||
str(file.relative_to(Path(".").resolve())).removesuffix(".py").replace("/", ".") | ||
) | ||
|
||
|
||
def get_app_instance(file: Path) -> Serverless: | ||
"""Load the app instance from the client module.""" | ||
|
||
module_name = get_module_name(file) | ||
parent_directory = str(file.parent.resolve()) | ||
|
||
spec = importlib.util.spec_from_file_location( | ||
module_name, | ||
str(file.resolve()), | ||
submodule_search_locations=[parent_directory], | ||
) | ||
|
||
if not spec or not spec.loader: | ||
raise ImportError( | ||
f"Can't find module {module_name} at location {file.resolve()}" | ||
) | ||
|
||
user_app = importlib.util.module_from_spec(spec) | ||
sys.path.append(parent_directory) | ||
cyclimse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sys.modules[module_name] = user_app | ||
spec.loader.exec_module(user_app) | ||
|
||
app_instance = None | ||
for member in inspect.getmembers(user_app): | ||
if isinstance(member[1], Serverless): | ||
# Ok. Found the variable now, need to use it | ||
app_instance = member[1] | ||
|
||
if not app_instance: # No variable with type "Serverless" found | ||
raise RuntimeError( | ||
f"""Unable to locate an instance of serverless App | ||
in the provided file: {file}.""" | ||
) | ||
|
||
return app_instance |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.