stock-increase-lambda
)๋ฅผ ๋ฐฐํฌstock_lambda
์์ ๋ ๊ฑฐ์ ์์คํ
(Factory API)์ ์ ํ ์์ฐ ์์ฒญ
์์ Factory API ๋ฌธ์๋ฅผ ํ์ฉํ์ฌ, ์ฝ๋๋ฅผ ์์ฑํด์ผ ํฉ๋๋ค.
stock_lambda
ํ๋ก์ ํธ์ npm install axios
๋ช
๋ น์ผ๋ก axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค. axios๋ node.js์์ HTTP ๋ช
๋ น์ ๋ณด๋ด๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. (fetch์ ์ ์ฌ)
const axios = require('axios').default
๋ฅผ ์ถ๊ฐํฉ๋๋ค.๋ค์ ์ฝ๋๋ฅผ ํ์ฉํ์ฌ Factory API์ ์ ํ ์์ฐ์ ์์ฒญํ์ธ์.
const payload = {
// TODO:
// ์ด๋ค ํ์์ผ๋ก ๋ฃ์ด์ผ ํ ๊น์? Factory API ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
// ํ์ํ๋ค๋ฉด record.body๋ฅผ ํ์ฉํ์ธ์.
}
axios.post('<http://project3-factory.coz-devops.click/api/manufactures>', payload)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Sales API์์ ๋ฉ์์ง๋ฅผ SNS์ ๋ฐํํ ๋ ๋ Factory API์ ํ์ํ ์ ๋ณด๋ฅผ ๊ฐ์ด ๋๊ฒจ์ผ ํฉ๋๋ค. Factory API์ ๋ง๊ฒ params๋ฅผ ์์ ํด์ผ ํ ๊ฒ์ ๋๋ค.
์ฌ๊ณ ์์ ๋ฉ์์ง ์ ์ก โ ์ผ์ ์๊ฐ ์ดํ ๋ค์ ์์ฒญ ์ ์ฌ๊ณ ๊ฐ์ ์๋ ํ์ธ
Factory API ๋ฐ stock-increase-lambda
๊ฐ ๋ฐ์๋ ์ต์ข
์ํคํ
์ฒ๋ฅผ ๊ทธ๋ ค๋ด
์๋ค.
//// /stock-lambda/serverless.yaml ////
service: stock-lambda
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs14.x
region: ap-northeast-2
functions:
producer:
handler: index.consumer
events:
- sqs: arn:aws:sqs:ap-northeast-2:517061613095:no-stock-sqs
plugins:
- serverless-lift
//// /stock-lambda/index.js ////
const consumer = async (event) =>{
for (const record of event.Records) {
console.log(`body๋ก ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ ํ์ธ : ${record.body}`);
const json = JSON.parse(record.body).MessageAttributes
console.log(`ํ์ ๋ฐ์ดํฐ : ${JSON.stringify(json)}`);
console.log(`์์ฒญ์ : ${json.MessageAttributeRequester.Value}`);
console.log(`๋ฌผ๊ฑด ์์ด๋ : ${json.MessageAttributeProductId.Value}`);
console.log(`๊ณต์ฅ ์์ด๋ : ${json.MessageAttributeFactoryId.Value}`);
console.log(`์๋ : ${json.MessageAttributeProductCnt.Value}`);
}
};
module.exports = {
consumer,
};
AWS ์ฝ์์์ ๋ง๋ ๋๋คํจ์๋ฅผ ์ญ์ ํ๊ณ ๋์ผํ๊ฒ ๋ก์ปฌ์์ ํธ๋ฆฌํ ๋ฐฐํฌ์ ์์ ์ ์ํด sls๋ก ์์ฑ
npm install axios
HTTP ๋ช ๋ น์ ๋ณด๋ด๊ธฐ ์ํด axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
///// sales-api/handler.js /////
const serverless = require("serverless-http");
const express = require("express");
const app = express();
app.use(express.json())
const AWS = require("aws-sdk") // STEP 2
const sns = new AWS.SNS({ region: "ap-northeast-2" }) // STEP 2
const {
connectDb,
queries: { getProduct, setStock }
} = require('./database')
app.get("/product/donut", connectDb, async (req, res, next) => {
const [ result ] = await req.conn.query(
getProduct('CP-502101')
)
await req.conn.end()
if (result.length > 0) {
return res.status(200).json(result[0]);
} else {
return res.status(400).json({ message: "์ํ ์์" });
}
});
app.post("/checkout", connectDb, async (req, res, next) => {
const [ result ] = await req.conn.query(
getProduct('CP-502101')
)
if (result.length > 0) {
const product = result[0]
if (product.stock >= 0 && product.stock >= req.body.MessageAttributeProductCnt) {
await req.conn.query(setStock(product.product_id, product.stock - req.body.MessageAttributeProductCnt))
return res.status(200).json({ message: `๊ตฌ๋งค ์๋ฃ! ๋จ์ ์ฌ๊ณ : ${product.stock - req.body.MessageAttributeProductCnt}`});
}
else {
await req.conn.end()
////
const now = new Date().toString()
const message = `๋๋์ธ ์ฌ๊ณ ๊ฐ ์์ต๋๋ค. ์ ํ์ ์์ฐํด์ฃผ์ธ์! \\n๋ฉ์์ง ์์ฑ ์๊ฐ: ${now}`
const params = {
Message: message,
Subject: '๋๋์ธ ์ฌ๊ณ ๋ถ์กฑ',
MessageAttributes: {
MessageAttributeProductId: {
StringValue: product.product_id,
DataType: "String",
},
MessageAttributeFactoryId: {
StringValue: req.body.MessageAttributeFactoryId,
DataType: "String",
},
MessageAttributeRequester: {
StringValue: req.body.MessageAttributeRequester,
DataType: "String",
},
MessageAttributeProductCnt: {
StringValue: req.body.MessageAttributeProductCnt,
DataType: "String",
}
},
TopicArn: process.env.TOPIC_ARN
}
const result = await sns.publish(params).promise()
////
return res.status(200).json({ message: `๊ตฌ๋งค ์คํจ! ๋จ์ ์ฌ๊ณ : ${product.stock}`});
}
} else {
await req.conn.end()
return res.status(400).json({ message: "์ํ ์์" });
}
});
app.use((req, res, next) => {
return res.status(404).json({
error: "Not Found",
});
});
module.exports.handler = serverless(app);
module.exports.app = app;
//// /stock-lambda/index.js ////
const axios = require('axios').default
const consumer = async (event) =>{
for (const record of event.Records) {
console.log(`body๋ก ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ ํ์ธ : ${record.body}`);
const json = JSON.parse(record.body).MessageAttributes
console.log(`ํ์ ๋ฐ์ดํฐ : ${JSON.stringify(json)}`);
console.log(`์์ฒญ์ : ${json.MessageAttributeRequester.Value}`);
console.log(`๋ฌผ๊ฑด ์์ด๋ : ${json.MessageAttributeProductId.Value}`);
console.log(`๊ณต์ฅ ์์ด๋ : ${json.MessageAttributeFactoryId.Value}`);
console.log(`์๋ : ${json.MessageAttributeProductCnt.Value}`);
const payload = {
MessageGroupId: 'stock-arrival-group',
MessageAttributeProductId: json.MessageAttributeProductId.Value,
MessageAttributeProductCnt: json.MessageAttributeProductCnt.Value,
MessageAttributeFactoryId: json.MessageAttributeFactoryId.Value,
MessageAttributeRequester: json.MessageAttributeRequester.Value,
CallbackUrl: '<https://hk6rw1m3qg.execute-api.ap-northeast-2.amazonaws.com/product/donut>',
};
axios.post('<http://project3-factory.coz-devops.click/api/manufactures>', payload)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
};
module.exports = {
consumer,
};
API ๋ฌธ์ ์ฐธ๊ณ ํ์ฌ ์ ๋ ฅ๊ฐ๊ณผ ์ถ๋ ฅ๊ฐ์ด API๋ฌธ์์ ๊ฐ์ด ๋์ฌ ์ ์๋๋ก ์ฝ๋ ์์