From b44472dd52af352977f8b81865622b8dfced5611 Mon Sep 17 00:00:00 2001 From: Yinlin Chen Date: Thu, 4 Mar 2021 10:07:36 -0500 Subject: [PATCH] enable CROS --- .gitignore | 4 +++- apps/app.py | 18 +++++++++++++++--- apps/update.py | 15 ++++++++++++--- template.yaml | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 1670b5d..ce8daf5 100644 --- a/.gitignore +++ b/.gitignore @@ -244,4 +244,6 @@ packaged.yaml */build/* -# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode \ No newline at end of file +.aws-sam/ + +# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode diff --git a/apps/app.py b/apps/app.py index 8d6b649..636be2c 100644 --- a/apps/app.py +++ b/apps/app.py @@ -10,12 +10,22 @@ region_name = os.getenv('Region') table_name = os.getenv('NSTable') -ddb = boto3.resource('dynamodb', region_name = region_name).Table(table_name) +ddb = boto3.resource('dynamodb', region_name=region_name).Table(table_name) + def lambda_handler(event, context): - noid = mint(template=noid_template, n=None, scheme=noid_scheme, naa=noid_naa) + noid = mint( + template=noid_template, + n=None, + scheme=noid_scheme, + naa=noid_naa) short_id = noid.split("/")[2] + headers = { + "Content-Type": "application/json", + "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", + "Access-Control-Allow-Methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", + "Access-Control-Allow-Origin": "*"} try: @@ -23,14 +33,16 @@ def lambda_handler(event, context): record["short_id"] = short_id ddb.put_item(Item=record) - except: + except BaseException: return { "statusCode": 503, + "headers": headers, "body": "Noid creation is failed.", } return { "statusCode": 200, + "headers": headers, "body": json.dumps({ "message": "New NOID: {0} is created.".format(short_id), }), diff --git a/apps/update.py b/apps/update.py index ee898b6..5afd8a0 100644 --- a/apps/update.py +++ b/apps/update.py @@ -6,11 +6,17 @@ region_name = os.getenv('Region') table_name = os.getenv('NSTable') -ddb = boto3.resource('dynamodb', region_name = region_name).Table(table_name) +ddb = boto3.resource('dynamodb', region_name=region_name).Table(table_name) def lambda_handler(event, context): + headers = { + "Content-Type": "application/json", + "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", + "Access-Control-Allow-Methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", + "Access-Control-Allow-Origin": "*"} + try: responseBody = event["body"] data = responseBody.split("&") @@ -19,7 +25,8 @@ def lambda_handler(event, context): noid = data[2].split("=")[1] create_date_str = data[3].split("=")[1] - create_date = datetime.datetime.strptime(create_date_str, '%Y-%m-%dT%H:%M:%S') + create_date = datetime.datetime.strptime( + create_date_str, '%Y-%m-%dT%H:%M:%S') insert_date_str = create_date.strftime("%Y-%m-%dT%H:%M:%S") record = {} @@ -32,14 +39,16 @@ def lambda_handler(event, context): ddb.put_item(Item=record) - except: + except BaseException: return { "statusCode": 503, + "headers": headers, "body": "Rec update is failed.", } return { "statusCode": 200, + "headers": headers, "body": json.dumps({ "message": "Rec {0} is updated.".format(noid), }), diff --git a/template.yaml b/template.yaml index 8278892..3463d76 100644 --- a/template.yaml +++ b/template.yaml @@ -50,6 +50,10 @@ Resources: Type: AWS::Serverless::Api Properties: StageName: Prod + Cors: + AllowMethods: "'GET,POST'" + AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" + AllowOrigin: "'*'" Auth: UsagePlan: CreateUsagePlan: PER_API @@ -82,6 +86,14 @@ Resources: Method: get Auth: ApiKeyRequired: true + Options: + Type: Api + Properties: + RestApiId: !Ref MintApi + Path: /mint + Method: options + Auth: + ApiKeyRequired: false Environment: Variables: NOID_NAA: !Ref NOIDNAA @@ -108,6 +120,14 @@ Resources: Method: post Auth: ApiKeyRequired: true + Options: + Type: Api + Properties: + RestApiId: !Ref MintApi + Path: /update + Method: options + Auth: + ApiKeyRequired: false Environment: Variables: NSTable: !Ref NSTableName