Explorar el Código

List implemented

Fred Damstra (Macbook 2015) hace 2 años
padre
commit
3a648f6aa3

+ 6 - 6
README.md

@@ -43,14 +43,14 @@ From this point forward, only POST is supported unless otherwise specified
 `/list`:
 Supported Methods: GET / POST
 Purpose: Returns a list of games
-Returns: `[ { gameid: STR, description: STR } , ... ]`
+Returns: `[ { game_id: STR, num_players: bool, tie_possible: bool, description: STR } , ... ]`
 
-`/join`:
+TODO `/join`:
 Purpose: Request to join a game.
 Input: `{ gameid: NUM }`
 Returns: `{ player_id: GUID, secret: GUID }`
 
-`/join/status`:
+TODO `/join/status`:
 Purpose: Request Status of a join request
 Input: `{ player_id: GUID, secret: GUID }`
 Returns: `{ status: pending/active/finished, game_id: GUID/null, recommended_refresh: NUM }`
@@ -58,13 +58,13 @@ Returns: `{ status: pending/active/finished, game_id: GUID/null, recommended_ref
 
 ### Tic Tac Toe
 
-`/ttt/state`:
+TODO `/ttt/state`:
 Purpose: Returns the state of the game
 Input: `{ game_id: GUID }`
-Returns: `{ current_turn: GUID(player_id), 'X': GUID(player_id), 'O': GUID(player_id), current_board: 3x3array }`
+Returns: `{ current_turn: GUID(player_id)|DONE, 'X': GUID(player_id), 'O': GUID(player_id), current_board: 3x3array, winner: GUID(if done) }`
 The `3x3array` is an array of characters of either '', 'X', or 'O'.
 
-`/ttt/action`:
+TODO `/ttt/action`:
 Purpose: Plays a turn
 Input: `{ game_id: GUID, player_id: GUID, secret: GUID, x: NUM, y: NUM }`
 x and y are the array positions you'd like to play

+ 110 - 0
event.sample.json

@@ -0,0 +1,110 @@
+{
+  "version": "1.0",
+  "resource": "/echo",
+  "path": "/test/echo",
+  "httpMethod": "POST",
+  "headers": {
+    "Content-Length": "27",
+    "Content-Type": "application/json",
+    "Host": "int8ruy9ij.execute-api.us-east-2.amazonaws.com",
+    "User-Agent": "curl/7.85.0",
+    "X-Amzn-Trace-Id": "Root=1-6392a861-13f6d9830252243828c1d2d5",
+    "X-Forwarded-For": "67.167.142.200",
+    "X-Forwar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BJDvgW5rHrqR6b4P-Iyeh6QWfHw"
+  },
+  "multiValueHeaders": {
+    "Content-Length": [
+      "27"
+    ],
+    "Content-Type": [
+      "application/json"
+    ],
+    "Host": [
+      "int8ruy9ij.execute-api.us-east-2.amazonaws.com"
+    ],
+    "User-Agent": [
+      "curl/7.85.0"
+    ],
+    "X-Amzn-Trace-Id": [
+      "Root=1-6392a861-13f6d9830252243828c1d2d5"
+    ],
+    "X-Forwarded-For": [
+      "67.167.142.200"
+    ],
+    "X-Forwarded-Port": [
+      "443"
+    ],
+    "X-Forwarded-Proto": [
+      "https"
+    ],
+    "accept": [
+      "*/*"
+    ],
+    "authorization": [
+      "Bearer eyJraWQiOiIreHdLTVRhbUluS1VwQTZRaUpnVktUQXVBMjdcLzRUZ01JZzlqTVE3RGl4VT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJkYTliODUxMS1iY2QwLTRmOTMtODk4NC1mMDI5ZjdkOTJhNjYiLCJpc3MiOiJodHRwczpcL1wvY29nbml0by1pZHAudXMtZWFzdC0yLmFtYXpvbmF3cy5jb21cL3VzLWVhc3QtMl8xc28xS2pWdGYiLCJjbGllbnRfaWQiOiIxaDc3YTRqM2JyMXJuZ3Bxbm0wc2lkNTAyZCIsIm9yaWdpbl9qdGkiOiIwMGQ0YTU5Yy1lNmVlLTRjMWItOWMzNi1mYTk0YzlkZTg5MDkiLCJldmVudF9pZCI6IjJjMjlmNzNhLTg1OWYtNDc2Zi1hODBlLTllOWUzOTVlMGI1ZSIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoiYXdzLmNvZ25pdG8uc2lnbmluLnVzZXIuYWRtaW4iLCJhdXRoX3RpbWUiOjE2NzA1NTU3MDMsImV4cCI6MTY3MDU1OTMwMywiaWF0IjoxNjcwNTU1NzAzLCJqdGkiOiI2OTExOWU5MS01YmVjLTRiMmItYmMxNC04YWIxZjY5NWE0OGEiLCJ1c2VybmFtZSI6ImZkYW1zdHJhIn0.burZ72OIcuWed2-B2etaqTVS6rssbFJzr7JcXgGwa-2Unzp7SvIP2cnVvbi_R0VqLPVS_h8bsfKi4sD6xveLVFVXzdStUiL07h0jbo4FNxCSue9Cd_okpJ3q6UdnIW3xcBUMt2XA1LE0qpwKZe3CKOB5k7ENCFdipzB7-gsUXrscpQ-KZ7Lag3eXYoyDSSDafWCgBIQF2eXh0XgWeMbWN7B8Me6UgO_QWi5_DNWTzKLGpfrT9YepzYNsnK2TwVjqd-bkQ56HJ9qPaCIJK4FgDlHLT5VO1pgvzaqQjN3VkGWT2QmGWko19uac9-FBJDvgW5rHrqR6b4P-Iyeh6QWfHw"
+    ]
+  },
+  "queryStringParameters": {
+    "fredwashere": "true"
+  },
+  "multiValueQueryStringParameters": {
+    "fredwashere": [
+      "true"
+    ]
+  },
+  "requestContext": {
+    "accountId": "082012130604",
+    "apiId": "int8ruy9ij",
+    "authorizer": {
+      "claims": {
+        "auth_time": "1670555703",
+        "client_id": "1h77a4j3br1rngpqnm0sid502d",
+        "event_id": "2c29f73a-859f-476f-a80e-9e9e395e0b5e",
+        "exp": "1670559303",
+        "iat": "1670555703",
+        "iss": "https://cognito-idp.us-east-2.amazonaws.com/us-east-2_1so1KjVtf",
+        "jti": "69119e91-5bec-4b2b-bc14-8ab1f695a48a",
+        "origin_jti": "00d4a59c-e6ee-4c1b-9c36-fa94c9de8909",
+        "scope": "aws.cognito.signin.user.admin",
+        "sub": "da9b8511-bcd0-4f93-8984-f029f7d92a66",
+        "token_use": "access",
+        "username": "fdamstra"
+      },
+      "scopes": null
+    },
+    "domainName": "int8ruy9ij.execute-api.us-east-2.amazonaws.com",
+    "domainPrefix": "int8ruy9ij",
+    "extendedRequestId": "c28_RjKBiYcEZCw=",
+    "httpMethod": "POST",
+    "identity": {
+      "accessKey": null,
+      "accountId": null,
+      "caller": null,
+      "cognitoAmr": null,
+      "cognitoAuthenticationProvider": null,
+      "cognitoAuthenticationType": null,
+      "cognitoIdentityId": null,
+      "cognitoIdentityPoolId": null,
+      "principalOrgId": null,
+      "sourceIp": "67.167.142.200",
+      "user": null,
+      "userAgent": "curl/7.85.0",
+      "userArn": null
+    },
+    "path": "/test/echo",
+    "protocol": "HTTP/1.1",
+    "requestId": "c28_RjKBiYcEZCw=",
+    "requestTime": "09/Dec/2022:03:15:45 +0000",
+    "requestTimeEpoch": 1670555745573,
+    "resourceId": "POST /echo",
+    "resourcePath": "/echo",
+    "stage": "test"
+  },
+  "pathParameters": null,
+  "stageVariables": null,
+  "body": "{\"fredwasalsohere\": \"true\"}",
+  "isBase64Encoded": false
+}

+ 2 - 2
terraform/lambda_echo.tf

@@ -2,8 +2,8 @@
 # Returns the event, and the context
 data "archive_file" "lambda_echo" {
   type        = "zip"
-  source_file = "${path.module}/lambda_scripts/echo.py"
-  output_path = "${path.module}/lambda_scripts/echo.zip"
+  source_file = "${path.module}/scripts/echo.py"
+  output_path = "${path.module}/tmp/echo.zip"
 }
 
 resource "aws_lambda_function" "lambda_echo" {

+ 2 - 2
terraform/lambda_example.tf

@@ -2,8 +2,8 @@
 # always returns {"a": "b"}
 data "archive_file" "lambda_example" {
   type        = "zip"
-  source_file = "${path.module}/lambda_scripts/example.py"
-  output_path = "${path.module}/lambda_scripts/example.zip"
+  source_file = "${path.module}/scripts/example.py"
+  output_path = "${path.module}/tmp/example.zip"
 }
 
 resource "aws_lambda_function" "lambda_example" {

+ 74 - 0
terraform/lambda_list.tf

@@ -0,0 +1,74 @@
+# /list
+# Returns the event, and the context
+data "archive_file" "lambda_list" {
+  type        = "zip"
+  source_file = "${path.module}/scripts/list.py"
+  output_path = "${path.module}/tmp/list.zip"
+}
+
+resource "aws_lambda_function" "lambda_list" {
+  # If the file is not in the current working directory you will need to include a
+  # path.module in the filename.
+  filename      = data.archive_file.lambda_list.output_path
+  function_name = "game_server_list"
+  role          = aws_iam_role.lambda_role.arn
+  handler       = "list.lambda_handler"
+
+  # The filebase64sha256() function is available in Terraform 0.11.12 and later
+  # For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
+  # source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
+  source_code_hash = data.archive_file.lambda_list.output_base64sha256
+
+  runtime = "python3.9"
+
+  environment {
+    variables = {
+      foo = "bar"
+    }
+  }
+
+  vpc_config {
+    subnet_ids         = module.vpc.public_subnets # using public for now, to allow for Internets
+    security_group_ids = [aws_security_group.allow_all.id]
+  }
+
+  tags = local.tags
+}
+
+resource "aws_lambda_permission" "api_lambda_list" {
+  statement_id  = "AllowExecutionFromAPI"
+  action        = "lambda:InvokeFunction"
+  function_name = aws_lambda_function.lambda_list.function_name
+  principal     = "apigateway.amazonaws.com"
+
+  # The /*/*/* part allows invocation from any stage, method and resource path
+  # within API Gateway REST API.
+  #source_arn = "${aws_apigatewayv2_stage.test.execution_arn}/*/*/*"
+  source_arn = "${aws_apigatewayv2_api.gateway.execution_arn}/*/*/*"
+  #qualifier     = aws_lambda_alias.test_alias.name
+}
+
+# The API Gateway Route
+resource "aws_apigatewayv2_integration" "lambda_list" {
+  api_id             = aws_apigatewayv2_api.gateway.id
+  integration_type   = "AWS_PROXY"
+  connection_type    = "INTERNET"
+  integration_method = "POST"
+  integration_uri    = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:function:${aws_lambda_function.lambda_list.function_name}/invocations"
+}
+
+resource "aws_apigatewayv2_route" "lambda_list_GET" {
+  api_id             = aws_apigatewayv2_api.gateway.id
+  route_key          = "GET /list"
+  target             = "integrations/${aws_apigatewayv2_integration.lambda_list.id}"
+  authorization_type = "JWT"
+  authorizer_id      = aws_apigatewayv2_authorizer.auth.id
+}
+
+resource "aws_apigatewayv2_route" "lambda_list_POST" {
+  api_id             = aws_apigatewayv2_api.gateway.id
+  route_key          = "POST /list"
+  target             = "integrations/${aws_apigatewayv2_integration.lambda_list.id}"
+  authorization_type = "JWT"
+  authorizer_id      = aws_apigatewayv2_authorizer.auth.id
+}

+ 2 - 2
terraform/lambda_storeit.tf

@@ -2,8 +2,8 @@
 # Returns the event, and the context
 data "archive_file" "lambda_storeit" {
   type        = "zip"
-  source_file = "${path.module}/lambda_scripts/storeit.py"
-  output_path = "${path.module}/lambda_scripts/storeit.zip"
+  source_file = "${path.module}/scripts/storeit.py"
+  output_path = "${path.module}/tmp/storeit.zip"
 }
 
 resource "aws_lambda_function" "lambda_storeit" {

+ 0 - 0
terraform/lambda_scripts/echo.py → terraform/scripts/echo.py


+ 0 - 0
terraform/lambda_scripts/example.py → terraform/scripts/example.py


+ 24 - 0
terraform/scripts/list.py

@@ -0,0 +1,24 @@
+#! /usr/bin/env python3
+
+import json
+
+GAMES = [
+    {
+        "description": "Old Fashioned Tic Tac Toe",
+        "game_id": "ttt",
+        "num_players": 2,
+        "tie_possible": True,
+    },
+]
+
+
+def lambda_handler(event, context):
+    return {"statusCode": 200, "isBase64Encoded": False, "body": json.dumps(GAMES)}
+
+
+def main():
+    print("No CLI support.")
+
+
+if __name__ == "__main__":
+    main()

+ 0 - 0
terraform/lambda_scripts/storeit.py → terraform/scripts/storeit.py


+ 0 - 0
terraform/tmp/.keep