Skip to content

Commit

Permalink
refactor phase 2 begin
Browse files Browse the repository at this point in the history
  • Loading branch information
Adibuer-lab committed Nov 3, 2023
1 parent a83067f commit 448dea9
Show file tree
Hide file tree
Showing 31 changed files with 828 additions and 1,605 deletions.
5 changes: 3 additions & 2 deletions src/carts/src/carts-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# SPDX-License-Identifier: MIT-0

import logging
# Set up logging
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler()])
import os
from server import app
from handlers import handler_bp
from routes import route_bp

# Set up logging
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler()])

app.register_blueprint(handler_bp)
app.register_blueprint(route_bp)
# Log a message at the start of the script
Expand Down
2 changes: 0 additions & 2 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ services:
- WEB_ROOT_URL
build:
context: ./products
args:
- GOPROXY_OVERRIDE
networks:
- dev-net
ports:
Expand Down
18 changes: 9 additions & 9 deletions src/products/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ FROM public.ecr.aws/docker/library/python:3.11-slim-bullseye

WORKDIR /src/

RUN apt-get update && apt-get install -y g++
RUN apt-get update && apt-get install -y && rm -rf /var/lib/apt/lists/*

COPY src/products-service/requirements.txt .
RUN python3 -m pip install --no-cache-dir -r requirements.txt

RUN python3 -m pip install -r requirements.txt

COPY src/products-service/server.py .
COPY src/products-service/app.py .
COPY src/products-service/routes.py .
COPY src/products-service/services.py .
COPY src/products-service/aws.py .
COPY src/products-service/ .

RUN mkdir dynamo-data
COPY src/products-service/data/*.yaml dynamo-data/

EXPOSE 80

ENTRYPOINT ["python"]
CMD ["app.py"]
ENV PYTHONUNBUFFERED=0

CMD ["python", "app.py"]
17 changes: 0 additions & 17 deletions src/products/Dockerfile-go

This file was deleted.

2 changes: 1 addition & 1 deletion src/products/load_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def verify_local_ddb_running(endpoint,dynamodb):
print(f'Usage: {sys.argv[0]} --categories-table-name CATEGORIES_TABLE_NAME [--categories-file CATEGORIES_FILE] --products-table-name PRODUCTS_TABLE_NAME [--products_file PRODUCTS_FILE] [--truncate] --carts-table-name CARTS_TABLE_NAME [--carts_file CARTS_FILE] [--endpoint-url ENDPOINT_URL]')
sys.exit(1)

dynamodb = resource('dynamodb', endpoint_url=endpoint_url)
dynamodb = resource('dynamodb', endpoint_url=endpoint_url, region_name = 'us-west-2')

if categories_table_name:
print(f'Loading categories from {categories_file} into table {categories_table_name}')
Expand Down
9 changes: 3 additions & 6 deletions src/products/src/products-service/app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import logging
import os
from server import app
from routes import route_bp

# Set up logging
logging.basicConfig(filename='app.log', level=logging.INFO)

app.register_blueprint(route_bp)
# Log a message at the start of the script
app.logger.info('Starting app.py')

import routes

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)}')
app.logger.error(f'Type of error: {type(e)}')
44 changes: 0 additions & 44 deletions src/products/src/products-service/aws.go

This file was deleted.

20 changes: 0 additions & 20 deletions src/products/src/products-service/category.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def setup():
region = 'us-east-1'
dynamo_client = boto3.client('dynamodb', region_name=region)

setup()
setup()
181 changes: 181 additions & 0 deletions src/products/src/products-service/dynamo_setup2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# 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
import yaml
from decimal import Decimal

# DynamoDB table names passed via environment
ddb_table_products = os.getenv("DDB_TABLE_PRODUCTS")
ddb_table_categories = os.getenv("DDB_TABLE_CATEGORIES")
products_file = "dynamo-data/products.yaml"
categories_file = "dynamo-data/categories.yaml"

def create_table(resource, ddb_table_name, attribute_definitions, key_schema, global_secondary_indexes=None):
try:
resource.create_table(
TableName=ddb_table_name,
KeySchema=key_schema,
AttributeDefinitions=attribute_definitions,
GlobalSecondaryIndexes=global_secondary_indexes or [],
BillingMode="PAY_PER_REQUEST",
)
app.logger.info(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

def verify_local_ddb_running(endpoint, dynamo_resource):
app.logger.info(f"Verifying that local DynamoDB is running at: {endpoint}")
for _ in range(5):
try:
response = dynamo_resource.meta.client.list_tables()
if ddb_table_products not in response['TableNames']:
try:
create_table(
ddb_table_name=ddb_table_products,
resource=dynamo_resource,
attribute_definitions=[
{"AttributeName": "id", "AttributeType": "S"},
{"AttributeName": "category", "AttributeType": "S"},
{"AttributeName": "featured", "AttributeType": "S"},
],
key_schema=[
{"AttributeName": "id", "KeyType": "HASH"},
],
global_secondary_indexes=[
{
"IndexName": "category-index",
"KeySchema": [{"AttributeName": "category", "KeyType": "HASH"}],
"Projection": {"ProjectionType": "ALL"},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
}
},
{
"IndexName": "featured-index",
"KeySchema": [{"AttributeName": "featured", "KeyType": "HASH"}],
"Projection": {"ProjectionType": "ALL"},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
}
}
]
)
table = dynamo_resource.Table(ddb_table_products)
app.logger.info(f'Loading products from {products_file}')
with open(products_file, 'r') as f:
products = yaml.safe_load(f)
app.logger.info(f'Updating products in table {ddb_table_products}')
for product in products:
product['id'] = str(product['id'])
if product.get('price'):
product['price'] = Decimal(str(product['price']))
if product.get('featured'):
product['featured'] = str(product['featured']).lower()
table.put_item(Item=product)

table.load()
app.logger.info(f"Table Name: {table.table_name}")
app.logger.info(f"Key Schema: {table.key_schema}")
app.logger.info(f"Attribute Definitions: {table.attribute_definitions}")
app.logger.info(f"Provisioned Throughput: {table.provisioned_throughput}")
app.logger.info(f"Global Secondary Indexes: {getattr(table, 'global_secondary_indexes', 'None')}")
app.logger.info(f"Local Secondary Indexes: {getattr(table, 'local_secondary_indexes', 'None')}")
app.logger.info(f"Table Status: {table.table_status}")
app.logger.info(f"Item Count: {table.item_count}")
app.logger.info(f"Table Size (Bytes): {table.table_size_bytes}")
app.logger.info(f"Creation Date Time: {table.creation_date_time.isoformat()}")

app.logger.info(f'Products loaded: {len(products)}')
except Exception as e:
app.logger.error(f"Failed to initialize product table: {str(e)}")
exit(1)
if ddb_table_categories not in response['TableNames']:
try:
create_table(
resource=dynamo_resource,
ddb_table_name=ddb_table_categories,
attribute_definitions=[
{"AttributeName": "id", "AttributeType": "S"},
{"AttributeName": "name", "AttributeType": "S"},
],
key_schema=[
{"AttributeName": "id", "KeyType": "HASH"},
],
global_secondary_indexes=[
{
"IndexName": "name-index",
"KeySchema": [{"AttributeName": "name", "KeyType": "HASH"}],
"Projection": {"ProjectionType": "ALL"},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
}
}
]
)
table = dynamo_resource.Table(ddb_table_categories)
app.logger.info(f'Loading categories from {categories_file}')
with open(categories_file, 'r') as f:
categories = yaml.safe_load(f)
app.logger.info(f'Updating categories in table {ddb_table_categories}')
for category in categories:
category['id'] = str(category['id'])
table.put_item(Item=category)
table.load()
app.logger.info(f"Table Name: {table.table_name}")
app.logger.info(f"Key Schema: {table.key_schema}")
app.logger.info(f"Attribute Definitions: {table.attribute_definitions}")
app.logger.info(f"Provisioned Throughput: {table.provisioned_throughput}")
app.logger.info(f"Global Secondary Indexes: {getattr(table, 'global_secondary_indexes', 'None')}")
app.logger.info(f"Local Secondary Indexes: {getattr(table, 'local_secondary_indexes', 'None')}")
app.logger.info(f"Table Status: {table.table_status}")
app.logger.info(f"Item Count: {table.item_count}")
app.logger.info(f"Table Size (Bytes): {table.table_size_bytes}")
app.logger.info(f"Creation Date Time: {table.creation_date_time.isoformat()}")

app.logger.info(f'Categories loaded: {len(categories)}')
except Exception as e:
app.logger.error(f"Failed to initialize category table: {str(e)}")
exit(1)
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.info("Local DynamoDB service not responding; verify that your docker-compose .env file is set up correctly")
exit(1)

ddb_endpoint_override = os.getenv("DDB_ENDPOINT_OVERRIDE")
running_local = False
dynamo_resource = None

def setup():
global dynamo_resource, running_local

if ddb_endpoint_override:
running_local = True
app.logger.info("Creating DDB client with endpoint override: " + ddb_endpoint_override)
dynamo_resource = boto3.resource(
'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_resource)
else:
running_local = False
dynamo_client = boto3.client('dynamodb')

setup()
10 changes: 0 additions & 10 deletions src/products/src/products-service/go.mod

This file was deleted.

Loading

0 comments on commit 448dea9

Please sign in to comment.