From 2218a6f8678a7d59403700bdaab5050eadcaa218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Rafael=20Siqueira?= Date: Sun, 23 Mar 2025 22:45:04 -0300 Subject: [PATCH] add local envs --- http-api/.env | 3 +-- http-api/Makefile | 12 ++++++------ http-api/app.py | 8 ++++---- http-api/pyproject.toml | 6 +----- http-api/samconfig.toml | 5 +++++ http-api/samlocal.env.json | 7 +++++++ http-api/settings.py | 5 ++--- http-api/template.yaml | 4 ++-- http-api/tests/conftest.py | 4 ++-- 9 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 http-api/samlocal.env.json diff --git a/http-api/.env b/http-api/.env index 4ddb4a3..a9cf1f1 100644 --- a/http-api/.env +++ b/http-api/.env @@ -1,7 +1,6 @@ # If UV does not load this file, try running `export UV_ENV_FILE=.env` # See more details at https://docs.astral.sh/uv/configuration/files/#env -ELASTIC_HOSTS=http://127.0.0.1:9200 -DYNAMODB_ENDPOINT_URL=http://127.0.0.1:8000 +ELASTIC_HOSTS=http://host.docker.internal:9200 DYNAMODB_PARTITION_KEY=id DYNAMODB_SORT_KEY=sk diff --git a/http-api/Makefile b/http-api/Makefile index 2145949..7043532 100644 --- a/http-api/Makefile +++ b/http-api/Makefile @@ -1,8 +1,8 @@ -export LOG_LEVEL = DEBUG -export TZ = America/Sao_Paulo +build: + sam build --use-container -deploy: - sam build --use-container && sam deploy +deploy: build + sam deploy --debug -start-api: - sam local start-api --port 4003 --warm-containers eager --debug +start-api: build + sam local start-api diff --git a/http-api/app.py b/http-api/app.py index 669d4da..1caea06 100644 --- a/http-api/app.py +++ b/http-api/app.py @@ -52,7 +52,7 @@ def lambda_handler(event: dict, context: LambdaContext) -> dict: return app.resolve(event, context) -if __name__ == '__main__': - print( - app.get_openapi_json_schema(), - ) +# if __name__ == '__main__': +# print( +# app.get_openapi_json_schema(), +# ) diff --git a/http-api/pyproject.toml b/http-api/pyproject.toml index a0b78b7..d2a85af 100644 --- a/http-api/pyproject.toml +++ b/http-api/pyproject.toml @@ -7,11 +7,7 @@ requires-python = ">=3.12" dependencies = ["layercake"] [dependency-groups] -dev = [ - "pytest>=8.3.4", - "pytest-cov>=6.0.0", - "ruff>=0.9.1", -] +dev = ["pytest>=8.3.4", "pytest-cov>=6.0.0", "ruff>=0.9.1"] [tool.pytest.ini_options] addopts = "--cov --cov-report html -v" diff --git a/http-api/samconfig.toml b/http-api/samconfig.toml index d9d6362..dfa087a 100644 --- a/http-api/samconfig.toml +++ b/http-api/samconfig.toml @@ -7,3 +7,8 @@ region = "sa-east-1" confirm_changeset = false capabilities = "CAPABILITY_IAM" image_repositories = [] + +[default.local_start_api.parameters] +debug = true +env_vars = "samlocal.env.json" +warm_containers = "EAGER" diff --git a/http-api/samlocal.env.json b/http-api/samlocal.env.json new file mode 100644 index 0000000..5f21839 --- /dev/null +++ b/http-api/samlocal.env.json @@ -0,0 +1,7 @@ +{ + "Parameters": { + "USER_TABLE": "test-users", + "ORDER_TABLE": "test-orders", + "ENROLLMENT_TABLE": "test-enrollments" + } +} diff --git a/http-api/settings.py b/http-api/settings.py index 4706331..3eae5e4 100644 --- a/http-api/settings.py +++ b/http-api/settings.py @@ -7,10 +7,9 @@ COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore ELASTIC_CLOUD_ID = os.getenv('ELASTIC_CLOUD_ID') ELASTIC_AUTH_PASS = os.getenv('ELASTIC_AUTH_PASS') -ELASTIC_HOSTS = os.getenv('ELASTIC_HOSTS', None) -if ELASTIC_HOSTS: - ELASTIC_CONN = {'hosts': ELASTIC_HOSTS} +if {'AWS_SAM_LOCAL', 'ELASTIC_HOSTS'}.intersection(os.environ): + ELASTIC_CONN = {'hosts': 'http://host.docker.internal:9200'} else: ELASTIC_CONN = { 'cloud_id': ELASTIC_CLOUD_ID, diff --git a/http-api/template.yaml b/http-api/template.yaml index cdebce3..a87f405 100644 --- a/http-api/template.yaml +++ b/http-api/template.yaml @@ -30,13 +30,13 @@ Globals: LOG_LEVEL: DEBUG POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1 POWERTOOLS_LOGGER_LOG_EVENT: true + DYNAMODB_PARTITION_KEY: id USER_TABLE: !Ref UserTable ORDER_TABLE: !Ref OrderTable ENROLLMENT_TABLE: !Ref EnrollmentTable COURSE_TABLE: !Ref CourseTable ELASTIC_CLOUD_ID: "{{resolve:ssm:/betaeducacao/elastic/cloud_id/str}}" ELASTIC_AUTH_PASS: "{{resolve:ssm:/betaeducacao/elastic/auth_pass/str}}" - DYNAMODB_PARTITION_KEY: id Resources: HttpLog: @@ -56,7 +56,7 @@ Resources: Authorizers: LambdaRequestAuthorizer: FunctionArn: !GetAtt AuthFunction.Arn - AuthorizerPayloadFormatVersion: 2.0 + AuthorizerPayloadFormatVersion: "2.0" EnableFunctionDefaultPermissions: true EnableSimpleResponses: true Identity: diff --git a/http-api/tests/conftest.py b/http-api/tests/conftest.py index fcbf473..9deffc0 100644 --- a/http-api/tests/conftest.py +++ b/http-api/tests/conftest.py @@ -9,8 +9,8 @@ import layercake.jsonl as jsonl import pytest from layercake.dynamodb import DynamoDBPersistenceLayer -PYTEST_TABLE_NAME = os.getenv('PYTEST_TABLE_NAME', 'pytest') -DYNAMODB_ENDPOINT_URL = os.getenv('DYNAMODB_ENDPOINT_URL') +PYTEST_TABLE_NAME = 'pytest' +DYNAMODB_ENDPOINT_URL = 'http://127.0.0.1:8000' PK = os.getenv('DYNAMODB_PARTITION_KEY', 'pk') SK = os.getenv('DYNAMODB_SORT_KEY', 'sk')