-
Notifications
You must be signed in to change notification settings - Fork 27
Sv 140 - add FastAPI to lambda and a GET route for stac urls #141
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2fef537
add fastapi to lambda
jonhealy1 ec2fb67
fastapi works in lambda
jonhealy1 dcc6aed
fastapi works in lambda
jonhealy1 bb284ea
add GET route for stac url
jonhealy1 72e79ca
POST and GET requests working
jonhealy1 d83d9b1
remove hello world path
jonhealy1 bc46634
change get endpoint name to url
jonhealy1 0d45a33
add url POST route
jonhealy1 cf84624
streamline lambda
jonhealy1 1c192bf
clean up setup
jonhealy1 fe6ea99
update for 2.0.0 pypi release
jonhealy1 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
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,28 @@ | ||
| __pycache__ | ||
| *.pyc | ||
| *.pyo | ||
| *.pyd | ||
| .Python | ||
| env | ||
| pip-log.txt | ||
| pip-delete-this-directory.txt | ||
| .tox | ||
| .coverage | ||
| .coverage.* | ||
| .cache | ||
| nosetests.xml | ||
| coverage.xml | ||
| *,cover | ||
| *.log | ||
| .git | ||
| *.swp | ||
| package-lock.json | ||
| __pycache__ | ||
| .pytest_cache | ||
| .env | ||
| .venv | ||
| *.egg-info | ||
|
|
||
| # CDK asset staging directory | ||
| .cdk.staging | ||
| cdk.out |
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 |
|---|---|---|
| @@ -1,39 +1,55 @@ | ||
| import json | ||
| import tempfile | ||
|
|
||
| from fastapi import FastAPI, Request | ||
| from fastapi.middleware.cors import CORSMiddleware | ||
| from mangum import Mangum | ||
|
|
||
| from stac_validator import stac_validator | ||
|
|
||
| app = FastAPI(title="STAC Validator", version=2.0, root_path="/prod/") | ||
|
|
||
| app.add_middleware( | ||
| CORSMiddleware, | ||
| allow_origins=["*"], | ||
| allow_credentials=True, | ||
| allow_methods=["*"], | ||
| allow_headers=["*"], | ||
| ) | ||
|
|
||
| def handler(event, context): | ||
| body = json.loads(event["body"]) | ||
| if body.get("stac_url"): | ||
| body = body["stac_url"] | ||
| else: | ||
| # TODO: Look at alains devops template. store in mem | ||
| temp_stac_file = tempfile.NamedTemporaryFile( | ||
| delete=False, | ||
| mode="w+", | ||
| ) | ||
| json.dump(body, temp_stac_file) | ||
| temp_stac_file.flush() | ||
| temp_stac_file.close() | ||
| body = temp_stac_file.name | ||
| stac = stac_validator.StacValidate(body) | ||
| stac.run() | ||
|
|
||
| def validate(stac_object): | ||
| stac = stac_validator.StacValidate(stac_object) | ||
| stac.run() | ||
| output = stac.message[0] | ||
| return output | ||
|
|
||
|
|
||
| @app.get("/url") | ||
| async def validate_url_get_request(stac_url): | ||
| output = validate(str(stac_url)) | ||
| return {"body": output} | ||
|
|
||
|
|
||
| @app.post("/url") | ||
| async def validate_url_post_request(stac_url): | ||
| output = validate(str(stac_url)) | ||
| return {"body": output} | ||
|
|
||
|
|
||
| @app.post("/json") | ||
| async def validate_json(request: Request): | ||
| stac_file = await request.json() | ||
| temp_stac_file = tempfile.NamedTemporaryFile( | ||
| delete=False, | ||
| mode="w+", | ||
| ) | ||
| json.dump(stac_file, temp_stac_file) | ||
| temp_stac_file.flush() | ||
| temp_stac_file.close() | ||
| body = temp_stac_file.name | ||
| output = validate(body) | ||
| return {"body": output} | ||
|
|
||
|
|
||
| if "validation_method" in output: | ||
| output.pop("validation_method") | ||
|
|
||
| return { | ||
| "statusCode": 200, | ||
| "isBase64Encoded": False, | ||
| "headers": { | ||
| "Content-Type": "application/json", | ||
| "Access-Control-Allow-Headers": "Content-Type", | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Methods": "OPTIONS,POST,GET", | ||
| }, | ||
| "body": json.dumps(output), | ||
| } | ||
| handler = Mangum(app) |
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 |
|---|---|---|
|
|
@@ -11,22 +11,22 @@ class ValidatorCdkStack(cdk.Stack): | |
| def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None: | ||
| super().__init__(scope, construct_id, **kwargs) | ||
|
|
||
| stac_lib = _lambda.LayerVersion( | ||
| val_lib = _lambda.LayerVersion( | ||
| self, | ||
| "stac-lib-layer", | ||
| code=_lambda.AssetCode("lambda/libraries.zip"), | ||
| "validator-library-layer", | ||
| code=_lambda.AssetCode("build-libraries/libraries.zip"), | ||
| compatible_runtimes=[_lambda.Runtime.PYTHON_3_8], | ||
| ) | ||
|
|
||
| # Defines an AWS Lambda resource | ||
| validator_lambda = _lambda.Function( | ||
| self, | ||
| "STACHandler", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. STACValidator |
||
| "STACValidator", | ||
| runtime=_lambda.Runtime.PYTHON_3_8, | ||
| code=_lambda.Code.asset("lambda"), | ||
| handler="lambda.handler", | ||
| timeout=cdk.Duration.seconds(30), | ||
| layers=[stac_lib], | ||
| layers=[val_lib], | ||
| ) | ||
|
|
||
| cors = apigw.CorsOptions(allow_origins=["*"]) | ||
|
|
||
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,8 @@ | ||
| [build-system] | ||
| requires = [ | ||
| "requests", | ||
| "jsonschema", | ||
| "pystac", | ||
| "click", | ||
| ] | ||
| build-backend = "setuptools.build_meta" |
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 @@ | ||
| "pytest" | ||
| "pytest-mypy" | ||
| "pre-commit" |
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
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.
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.
Let's leave this as validator lambda.