Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit add3641

Browse files
Merge branch 'production' into api_restful
2 parents 069a413 + dc82442 commit add3641

File tree

195 files changed

+8327
-140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+8327
-140
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ Example of architecture of this project stack.
1010
Example of components of the architecture of this project.
1111
![Service-Stack](docs/service-stack.png)
1212

13-
## Single projects
14-
You can find single examples for:
13+
## Build environment script workflow
14+
Example of the workflow to create the environment.
15+
![Service-Stack](docs/runenv-workflow.drawio.png)
16+
17+
## Single project examples
18+
19+
You can find light examples:
20+
* [Lambda API Light](./examples/lambda_api_light)
21+
* [Lambda CRON Light](./examples/lambda_cron_light)
22+
* [Lambda SQS Light](./examples/lambda_sqs_light)
23+
* [Lambda SNS Light](./examples/lambda_sns_light)
24+
* [Lambda S3 Light](./examples/lambda_s3_light)
25+
*
26+
You can find complex examples:
1527
* [Lambda API](./examples/lambda_api)
28+
* [Lambda API RESTful](./examples/lambda_api_restful)
1629
* [Lambda CRON](./examples/lambda_cron)
1730
* [Lambda SQS](./examples/lambda_sqs)
1831
* [Lambda SNS](./examples/lambda_sns)

docs/runenv-workflow.drawio.png

77 KB
Loading

examples/lambda_api/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@ config/integration.en
6060
/node_modules/
6161

6262
/lambda-full.zip
63+
64+
#output lambda
65+
output/response.json
66+
/output/
67+
/include/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"python.testing.unittestArgs": [
3+
"-v",
4+
"-s",
5+
"./tests",
6+
"-p",
7+
"test_*.py"
8+
],
9+
"python.testing.pytestEnabled": false,
10+
"python.testing.unittestEnabled": true
11+
}

examples/lambda_api/README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
# template-serverless-lambda-python - Lambda API
1+
# Lambda API
22
Template for build flexible API with AWS Lambda.
33

44
## Service Architecture
55
Example of architecture with Kong API Gateway.
66
![Service-Arch](docs/service-arch.png)
77

8+
## Build environment script workflow
9+
Example of the workflow to create the environment.
10+
![Service-Stack](docs/runenv-workflow.drawio.png)
11+
812
## General Service Routes Architecture
9-
Service routes map.
10-
![Service-Routes](docs/service-routes.png)
13+
Service routes.
14+
```
15+
GET / - Root
16+
GET /docs - Swagger docs
17+
GET /alive - Health Check
18+
GET /v1/event/<event_type> - Event List
19+
POST /v1/event/<event_type> - Create event
20+
```
21+
22+
[//]: # (![Service-Routes]&#40;docs/service-routes.png&#41;)
1123

1224
# Prerequisites
1325
- Python 3.6
@@ -53,6 +65,7 @@ https://docs.aws.amazon.com/pt_br/cli/latest/userguide/install-cliv2.html
5365

5466
Execute the follow command:
5567
```
68+
apt install python38-env
5669
apt install awscli
5770
apt install zip
5871
app install pip
@@ -103,9 +116,14 @@ First you need install the tests requirements:
103116

104117

105118
### Unit tests:
119+
Executing the tests:
106120
```
107121
./scripts/venv-exec.sh ./scripts/tests/unit-tests.sh
108122
```
123+
Executing a specific file:
124+
```
125+
./scripts/venv-exec.sh ./scripts/tests/unit-tests.sh /tests/unit/test_app.py
126+
```
109127
### Components tests:
110128
Start the docker containers:
111129
```
@@ -116,6 +134,10 @@ Executing the tests:
116134
```
117135
./scripts/venv-exec.sh ./scripts/tests/component-tests.sh
118136
```
137+
Executing a specific file:
138+
```
139+
./scripts/venv-exec.sh ./scripts/tests/component-tests.sh /tests/component/test_app.py
140+
```
119141
### Integration tests:
120142
Copy the file `config/integration.env.example` to
121143
`config/integration.env` and edit it with de staging parameters.
@@ -124,7 +146,10 @@ Executing the tests:
124146
```
125147
./scripts/venv-exec.sh ./scripts/tests/integration-tests.sh
126148
```
127-
149+
Executing a specific file:
150+
```
151+
./scripts/venv-exec.sh ./scripts/tests/integration-tests.sh /tests/integration/test_app.py
152+
```
128153

129154
### All tests:
130155
Executing the tests:

examples/lambda_api/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import boot
66
import os
77
import base64
8-
from lambda_app.services.v1.healthcheck import HealthCheckSchema
8+
from lambda_app.services.v1.healthcheck import HealthCheckSchema, HealthCheckResult
99
from lambda_app.services.v1.healthcheck.resources import \
1010
MysqlConnectionHealthCheck, RedisConnectionHealthCheck, \
1111
SQSConnectionHealthCheck, SelfConnectionHealthCheck
@@ -85,6 +85,7 @@ def alive():
8585
LOGGER, CONFIG), ["redis"])
8686
service.add_check("queue", SQSConnectionHealthCheck(
8787
LOGGER, CONFIG), ["queue"])
88+
service.add_check("internal", lambda: HealthCheckResult.unhealthy("connect"), ["example"])
8889

8990
return service.get_response()
9091

77 KB
Loading

examples/lambda_api/env/integration.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LOG_LEVEL=info
55
REGION_NAME=us-east-2
66
SQS_ENDPOINT=https://sqs.us-east-2.amazonaws.com
77
SQS_LOCALSTACK=http://localhost:4566
8-
APP_QUEUE=https://sqs.us-east-2.amazonaws.com/683720833731/test-queue
8+
APP_QUEUE=https://sqs.us-east-2.amazonaws.com/000000000000/test-queue
99
API_SERVER=https://service.hagatus.com.br
1010
API_SERVER_DESCRIPTION=Staging server
1111
LOCAL_API_SERVER=http://localhost:5000
1212
LOCAL_API_SERVER_DESCRIPTION=Development server
13-
REDIS_HOST=sourcing-service-events-api-redis-staging.redekasa.com
13+
REDIS_HOST=test.something.0001.use2.cache.amazonaws.com
1414
REDIS_PORT=6379
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import ast
2+
import json
3+
4+
from lambda_app import helper
5+
from lambda_app.decorators import SQSEvent
6+
from lambda_app.decorators.events import SQSRecord
7+
from lambda_app.logging import get_logger
8+
9+
10+
def read_event(record, logger=None):
11+
if logger is None:
12+
logger = get_logger()
13+
logger.info('try to reading event form record: {}'.format(record))
14+
logger.info('Getting type of data: {}'.format(type(record)))
15+
try:
16+
logger.info('dump: {}'.format(json.dumps(record)))
17+
except Exception as err:
18+
logger.error(err)
19+
event_body = None
20+
try:
21+
if isinstance(record, dict):
22+
try:
23+
event_body = json.loads(record['body'])
24+
except Exception as err:
25+
logger.error(err)
26+
unescaped_str = ast.literal_eval(record['body'])
27+
event_body = json.loads(unescaped_str)
28+
elif isinstance(record, str):
29+
record = json.loads(record)
30+
event_body = json.loads(record.body)
31+
elif isinstance(record.body, str):
32+
event_body = json.loads(record.body)
33+
else:
34+
event_body = record.body
35+
except Exception as err:
36+
logger.error(err)
37+
logger.info('event_body: {}'.format(event_body))
38+
return event_body
39+
40+
41+
def get_records_from_sqs_event(sqs_event, logger=None):
42+
if logger is None:
43+
logger = get_logger()
44+
records = []
45+
try:
46+
if isinstance(sqs_event, SQSEvent):
47+
logger.info("SQSEvent instance")
48+
if not helper.empty(sqs_event.to_dict()):
49+
try:
50+
sqs_event_dict = sqs_event.to_dict()
51+
if 'Records' in sqs_event_dict:
52+
sqs_event_dict = sqs_event_dict['Records']
53+
for record in sqs_event_dict:
54+
records.append(record)
55+
except Exception as err:
56+
logger.error(err)
57+
records.append(sqs_event.to_dict())
58+
elif isinstance(sqs_event, SQSRecord):
59+
logger.info("SQSRecord instance")
60+
if not helper.empty(sqs_event.to_dict()):
61+
records.append(sqs_event)
62+
except Exception as err:
63+
logger.error(err)
64+
if isinstance(sqs_event, SQSEvent) or isinstance(sqs_event, SQSRecord):
65+
logger.error(sqs_event.__dict__)
66+
else:
67+
try:
68+
logger.error(json.dumps(sqs_event))
69+
except Exception as err:
70+
logger.error(err)
71+
logger.error(str(sqs_event))
72+
return records

examples/lambda_api/scripts/boot-db.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)