mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 08:02:28 +00:00
feat: serverless examples (#11)
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# NodeJS + Lambda + S3 Serverless Rules Engine
|
||||
|
||||
Requirements:
|
||||
* ZenEngineNodeJS Lambda Layer
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user