-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor order service from go to python (#488)
* resolving conflict * refactor orders service to python * add persistence to orders service with dynamodb * orders table cloudformation output * node-modules package-lock * add validation and more testing * search for specific table during local and add more test * explicit routes and handlers * update valid keys * remove unused imports and scripts * update valid keys for order service * remove old go scripts and make order keys consistent * remove ttl order inherits from cart * Update app.py and remove unused imports * fix linter errors * fix linter errors-too long lines * fix too long lines
- Loading branch information
1 parent
275ba37
commit e8080b3
Showing
30 changed files
with
660 additions
and
382 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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,10 +1,21 @@ | ||
FROM public.ecr.aws/s5z3t2n9/golang:1.11-alpine AS build | ||
FROM public.ecr.aws/docker/library/python:3.10-slim-bullseye | ||
|
||
WORKDIR /src/ | ||
COPY src/orders-service/*.* /src/ | ||
RUN apk add --no-cache git | ||
RUN CGO_ENABLED=0 go build -o /bin/orders-service | ||
|
||
FROM scratch | ||
COPY --from=build /bin/orders-service /bin/orders-service | ||
RUN apt-get update && apt-get install -y g++ | ||
|
||
COPY src/orders-service/requirements.txt . | ||
|
||
RUN python3 -m pip install -r requirements.txt | ||
|
||
COPY src/orders-service/server.py . | ||
COPY src/orders-service/app.py . | ||
COPY src/orders-service/routes.py . | ||
COPY src/orders-service/services.py . | ||
COPY src/orders-service/handlers.py . | ||
COPY src/orders-service/dynamo_setup.py . | ||
|
||
EXPOSE 80 | ||
ENTRYPOINT ["/bin/orders-service"] | ||
|
||
ENTRYPOINT ["python"] | ||
CMD ["app.py"] |
This file contains 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 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,25 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
import logging | ||
import os | ||
from server import app | ||
from handlers import handler_bp | ||
from routes import routes_bp | ||
|
||
|
||
# Set up logging | ||
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler()]) | ||
app.register_blueprint(handler_bp) | ||
app.register_blueprint(routes_bp) | ||
# Log a message at the start of the script | ||
app.logger.info('Starting app.py') | ||
|
||
if __name__ == '__main__': | ||
try: | ||
port = os.getenv('PORT', '80') | ||
app.run(host='0.0.0.0', port=int(port)) | ||
except Exception as e: | ||
# Log the error message and the type of the exception | ||
app.logger.error(f'Error starting server: {str(e)}') | ||
app.logger.error(f'Type of error: {type(e)}') |
This file contains 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,101 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
import os | ||
import time | ||
import boto3 | ||
from server import app | ||
|
||
def create_table(client, ddb_table_name, | ||
attribute_definitions, | ||
key_schema, | ||
global_secondary_indexes=None): | ||
try: | ||
client.create_table( | ||
TableName=ddb_table_name, | ||
KeySchema=key_schema, | ||
AttributeDefinitions=attribute_definitions, | ||
GlobalSecondaryIndexes=global_secondary_indexes or [], | ||
BillingMode="PAY_PER_REQUEST", | ||
) | ||
print(f'Created table: {ddb_table_name}') | ||
except Exception as e: | ||
if e.response["Error"]["Code"] == "ResourceInUseException": | ||
app.logger.info(f'Table {ddb_table_name} already exists; continuing...') | ||
else: | ||
raise e | ||
|
||
|
||
# DynamoDB table names passed via environment | ||
ddb_table_orders = os.getenv("DDB_TABLE_ORDERS") | ||
|
||
# Allow DDB endpoint to be overridden to support amazon/dynamodb-local | ||
ddb_endpoint_override = os.getenv("DDB_ENDPOINT_OVERRIDE") | ||
running_local = False | ||
|
||
dynamo_client = None | ||
|
||
def verify_local_ddb_running(endpoint, dynamo_client): | ||
app.logger.info(f"Verifying that local DynamoDB is running at: {endpoint}") | ||
for _ in range(5): | ||
try: | ||
response = dynamo_client.list_tables() | ||
if ddb_table_orders not in response["TableNames"]: | ||
create_table( | ||
ddb_table_name=ddb_table_orders, | ||
client=dynamo_client, | ||
attribute_definitions=[ | ||
{"AttributeName": "id", "AttributeType": "S"}, | ||
{"AttributeName": "username", "AttributeType": "S"} | ||
], | ||
key_schema=[ | ||
{"AttributeName": "id", "KeyType": "HASH"}, | ||
], | ||
global_secondary_indexes=[ | ||
{ | ||
"IndexName": "username-index", | ||
"KeySchema": [{ | ||
"AttributeName": "username", | ||
"KeyType": "HASH"}], | ||
"Projection": {"ProjectionType": "ALL"}, | ||
"ProvisionedThroughput": { | ||
"ReadCapacityUnits": 5, | ||
"WriteCapacityUnits": 5, | ||
} | ||
} | ||
] | ||
) | ||
app.logger.info("DynamoDB local is responding!") | ||
return | ||
except Exception as e: | ||
app.logger.info(e) | ||
app.logger.info( | ||
"Local DynamoDB service is not ready yet... pausing before trying again" | ||
) | ||
time.sleep(2) | ||
app.logger.error( | ||
"Local DynamoDB service not responding;\ | ||
verify that your docker-compose .env file is setup correctly" | ||
) | ||
exit(1) | ||
|
||
def setup(): | ||
global dynamo_client, running_local | ||
|
||
if ddb_endpoint_override: | ||
running_local = True | ||
app.logger.info("Creating DDB client with endpoint override: " | ||
+ ddb_endpoint_override) | ||
dynamo_client = boto3.client( | ||
'dynamodb', | ||
endpoint_url=ddb_endpoint_override, | ||
region_name='us-west-2', | ||
aws_access_key_id='XXXX', | ||
aws_secret_access_key='XXXX' | ||
) | ||
verify_local_ddb_running(ddb_endpoint_override, dynamo_client) | ||
else: | ||
running_local = False | ||
dynamo_client = boto3.client('dynamodb') | ||
|
||
setup() |
Oops, something went wrong.