diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index 18485c43..39d60c92 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -7,6 +7,7 @@ on: pull_request: paths-ignore: - 'bindings/**' + - 'examples/**' - '.github/workflows/python.yaml' - '.github/workflows/python-version-bump.yaml' - '.github/workflows/node.yaml' @@ -18,6 +19,7 @@ on: - '**' paths-ignore: - 'bindings/**' + - 'examples/**' - '.github/workflows/python.yaml' - '.github/workflows/python-version-bump.yaml' - '.github/workflows/node.yaml' diff --git a/examples/nodejs-lambda-s3/README.md b/examples/nodejs-lambda-s3/README.md new file mode 100644 index 00000000..a71cb9d8 --- /dev/null +++ b/examples/nodejs-lambda-s3/README.md @@ -0,0 +1,4 @@ +# NodeJS + Lambda + S3 Serverless Rules Engine + +Requirements: +* ZenEngineNodeJS Lambda Layer diff --git a/examples/nodejs-lambda-s3/index.mjs b/examples/nodejs-lambda-s3/index.mjs new file mode 100644 index 00000000..069b0348 --- /dev/null +++ b/examples/nodejs-lambda-s3/index.mjs @@ -0,0 +1,43 @@ +import { ZenEngine } from '@gorules/zen-engine'; +import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'; + +const client = new S3Client({}); +const { BUCKET_NAME } = process.env; + +const streamToBuffer = (stream) => new Promise((resolve, reject) => { + const chunks = []; + stream.on('data', (chunk) => chunks.push(chunk)); + stream.on('error', reject); + stream.on('end', () => resolve(Buffer.concat(chunks))); +}); + +const loader = async (key) => { + try { + const params = { + Bucket: BUCKET_NAME, + Key: key, + }; + + const command = new GetObjectCommand(params); + const response = await client.send(command); + + const { Body } = response; + + return streamToBuffer(Body); + } catch (e) { + console.error(e); + } +}; + +export const handler = async(event) => { + const { key, context } = event; + const engine = new ZenEngine({ + loader + }); + const decision = await engine.getDecision(key); + const result = await decision.evaluate(context); + return { + statusCode: 200, + body: result, + }; +}; diff --git a/examples/python-lambda-s3/README.md b/examples/python-lambda-s3/README.md new file mode 100644 index 00000000..731085be --- /dev/null +++ b/examples/python-lambda-s3/README.md @@ -0,0 +1,4 @@ +# Python + Lambda + S3 Serverless Rules Engine + +Requirements: +* ZenEnginePython Lambda Layer diff --git a/examples/python-lambda-s3/lambda_function.py b/examples/python-lambda-s3/lambda_function.py new file mode 100644 index 00000000..b4399fb3 --- /dev/null +++ b/examples/python-lambda-s3/lambda_function.py @@ -0,0 +1,21 @@ +import json +import zen +import boto3 +import os + +s3_client = boto3.client("s3") +BUCKET_NAME = os.environ["BUCKET_NAME"] + +def loader(key): + file_content = s3_client.get_object( + Bucket=BUCKET_NAME, Key=key)["Body"].read().decode("ascii") + return file_content + +def lambda_handler(event, context): + engine = zen.ZenEngine({"loader": loader}) + decision = engine.get_decision(event["key"]) + result = decision.evaluate(event["context"]) + return { + "statusCode": 200, + "body": result + }