Skip to content

Commit

Permalink
Merge pull request #1 from vt-digital-libraries-platform/LIBTD-2429
Browse files Browse the repository at this point in the history
enable CROS
  • Loading branch information
whunter authored Mar 9, 2021
2 parents 043792c + b44472d commit daf2eeb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,6 @@ packaged.yaml

*/build/*

# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
.aws-sam/

# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
18 changes: 15 additions & 3 deletions apps/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,39 @@
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:

record = {}
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),
}),
Expand Down
15 changes: 12 additions & 3 deletions apps/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("&")
Expand All @@ -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 = {}
Expand All @@ -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),
}),
Expand Down
20 changes: 20 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit daf2eeb

Please sign in to comment.