-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update readme add visitor count via ddb/cdk/aws
- Loading branch information
TIMOTHY ANDES
committed
Mar 1, 2024
1 parent
0b04010
commit 8610548
Showing
4 changed files
with
43 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
VERSION="1.4" | ||
VERSION="1.5" |
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,9 +1,32 @@ | ||
import os | ||
import boto3 | ||
|
||
def handler(event, context): | ||
# Raw event data; kill the favicon hit | ||
path = event["rawPath"] | ||
if path != "/": | ||
return {"statusCode": 404, "body": "Not found."} | ||
|
||
# get a reference to dynamoDB | ||
dynamodb = boto3.resource("dynamodb") | ||
table = dynamodb.Table(os.environ.get("TABLE_NAME")) | ||
|
||
# Read the "VISIT COUNT" key (or create it if it doesn't exist) | ||
response = table.get_item(Key={"key": "visit_count"}) | ||
if "Item" in response: | ||
visit_count = response["Item"]["value"] | ||
else: | ||
visit_count = 0 | ||
|
||
# Increment the visit count and write it back to the table. | ||
new_visit_count = visit_count + 1 | ||
table.put_item(Item={"key": "visit_count", "value": new_visit_count}) | ||
|
||
# get current version from .env | ||
version = os.environ.get("VERSION", "0.0") | ||
response_body = { | ||
"message": "Hello World", | ||
"version": version, | ||
"visit_count": new_visit_count, | ||
} | ||
return {"statusCode": 200, "body": response_body} |
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