From 15c43c04d34485a6eb24c4ecb874d5b0d985f18a Mon Sep 17 00:00:00 2001 From: YasminEmily <119274946+YasminEmily@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:30:44 -0300 Subject: [PATCH 1/3] =?UTF-8?q?git=20add=20--all=20git=20commit=20-m=20"Ta?= =?UTF-8?q?refa=20pr=C3=A1tica=20#1=20-=20Microservices"=20git=20push=20or?= =?UTF-8?q?igin=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proto/inventory.proto | 8 ++++++++ services/controller/index.js | 17 ++++++++++++++++- services/inventory/index.js | 6 ++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/proto/inventory.proto b/proto/inventory.proto index c819783e..0b31cc3f 100644 --- a/proto/inventory.proto +++ b/proto/inventory.proto @@ -2,6 +2,10 @@ syntax = "proto3"; service InventoryService { rpc SearchAllProducts(Empty) returns (ProductsResponse) {} +service InventoryService { + rpc SearchAllProducts(Empty) returns (ProductsResponse) {} + rpc SearchProductByID(Payload) returns (ProductResponse) {} +} } message Empty{} @@ -18,3 +22,7 @@ message ProductResponse { message ProductsResponse { repeated ProductResponse products = 1; } + +message Payload { + int32 id = 1; +} \ No newline at end of file diff --git a/services/controller/index.js b/services/controller/index.js index dd77a4db..3287083f 100644 --- a/services/controller/index.js +++ b/services/controller/index.js @@ -41,7 +41,22 @@ app.get('/shipping/:cep', (req, res, next) => { } ); }); - +app.get('/product/:id', (req, res, next) => { + // Chama método do microsserviço. + inventory.SearchProductByID({ id: req.params.id }, (err, product) => { + // Se ocorrer algum erro de comunicação + // com o microsserviço, retorna para o navegador. + if (err) { + console.error(err); + res.status(500).send({ error: 'something failed :(' }); + } else { + // Caso contrário, retorna resultado do + // microsserviço (um arquivo JSON) com os dados + // do produto pesquisado + res.json(product); + } + }); +}); /** * Inicia o router */ diff --git a/services/inventory/index.js b/services/inventory/index.js index 2eeabc9d..9fec4693 100644 --- a/services/inventory/index.js +++ b/services/inventory/index.js @@ -20,6 +20,12 @@ server.addService(inventoryProto.InventoryService.service, { products: products, }); }, +SearchProductByID: (payload, callback) => { + callback( + null, + products.find((product) => product.id == payload.request.id) + ); +}, }); server.bindAsync('127.0.0.1:3002', grpc.ServerCredentials.createInsecure(), () => { From 4203d9cb1fa29e5e0055e994a82de60c7e74b196 Mon Sep 17 00:00:00 2001 From: YasminEmily <119274946+YasminEmily@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:36:51 -0300 Subject: [PATCH 2/3] Create shipping.Dockerfile --- shipping.Dockerfile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 shipping.Dockerfile diff --git a/shipping.Dockerfile b/shipping.Dockerfile new file mode 100644 index 00000000..b23e7fdf --- /dev/null +++ b/shipping.Dockerfile @@ -0,0 +1,14 @@ +# Imagem base derivada do Node +FROM node + +# Diretório de trabalho +WORKDIR /app + +# Comando para copiar os arquivos para a pasta /app da imagem +COPY . /app + +# Comando para instalar as dependências +RUN npm install + +# Comando para inicializar (executar) a aplicação +CMD ["node", "/app/services/shipping/index.js"] From 160b629ce96cb58730cfc0dab9bf02afa1b17f56 Mon Sep 17 00:00:00 2001 From: YasminEmily <119274946+YasminEmily@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:44:11 -0300 Subject: [PATCH 3/3] =?UTF-8?q?git=20add=20--all=20git=20commit=20-m=20"Ta?= =?UTF-8?q?refa=20pr=C3=A1tica=20#2=20-=20Docker"=20git=20push=20origin=20?= =?UTF-8?q?main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c7f465f6..f23223e8 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Toy example of microservice", "main": "", "scripts": { - "start": "run-p start-frontend start-controller start-shipping start-inventory", + "start": "run-p start-frontend start-controller start-inventory", "start-controller": "nodemon services/controller/index.js", "start-shipping": "nodemon services/shipping/index.js", "start-inventory": "nodemon services/inventory/index.js",