Skip to content

Commit

Permalink
Tarefa prática aserg-ufmg#1 - Microservices
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmoade committed Mar 12, 2021
1 parent 8ad57ab commit 3cad745
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions proto/inventory.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ syntax = "proto3";

service InventoryService {
rpc SearchAllProducts(Empty) returns (ProductsResponse) {}
rpc SearchProductByID(Payload) returns (ProductResponse) {}
}

message Payload {
int32 id = 1;
}

message Empty{}
Expand Down
17 changes: 17 additions & 0 deletions services/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ 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);
}
});
});

app.get('/product/:id', (req, res, next) => {
inventory.SearchProductByID({ id: req.params.id }, (err, product) => {
if (err) {
Expand Down
6 changes: 6 additions & 0 deletions services/inventory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), () => {
Expand Down

0 comments on commit 3cad745

Please sign in to comment.