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
Bloq96 committed Mar 1, 2021
1 parent 93a4c2f commit 376f724
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 109 deletions.
45 changes: 25 additions & 20 deletions proto/inventory.proto
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
syntax = "proto3";

service InventoryService {
rpc SearchAllProducts(Empty) returns (ProductsResponse) {}
}

message Empty{}

message ProductResponse {
int32 id = 1;
string name = 2;
int32 quantity = 3;
float price = 4;
string photo = 5;
string author = 6;
}

message ProductsResponse {
repeated ProductResponse products = 1;
}
syntax = "proto3";

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

message Empty{}

message Payload {
int32 id = 1;
}

message ProductResponse {
int32 id = 1;
string name = 2;
int32 quantity = 3;
float price = 4;
string photo = 5;
string author = 6;
}

message ProductsResponse {
repeated ProductResponse products = 1;
}
138 changes: 77 additions & 61 deletions services/controller/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,77 @@
const express = require('express');
const shipping = require('./shipping');
const inventory = require('./inventory');
const cors = require('cors');

const app = express();
app.use(cors());

/**
* Retorna a lista de produtos da loja via InventoryService
*/
app.get('/products', (req, res, next) => {
inventory.SearchAllProducts(null, (err, data) => {
if (err) {
console.error(err);
res.status(500).send({ error: 'something failed :(' });
} else {
res.json(data.products);
}
});
});

/**
* Consulta o frete de envio no ShippingService
*/
app.get('/shipping/:cep', (req, res, next) => {
shipping.GetShippingRate(
{
cep: req.params.cep,
},
(err, data) => {
if (err) {
console.error(err);
res.status(500).send({ error: 'something failed :(' });
} else {
res.json({
cep: req.params.cep,
value: data.value,
});
}
}
);
});

app.get('/product/:id', (req, res, next) => {
inventory.SearchProductByID({ id: req.params.id }, (err, product) => {
if (err) {
console.error(err);
res.status(500).send({ error: 'something failed :(' });
} else {
res.json(product);
}
});
});

/**
* Inicia o router
*/
app.listen(3000, () => {
console.log('Controller Service running on http://localhost:3000');
});
const express = require('express');
const shipping = require('./shipping');
const inventory = require('./inventory');
const cors = require('cors');

const app = express();
app.use(cors());

/**
* Retorna a lista de produtos da loja via InventoryService
*/
app.get('/products', (req, res, next) => {
inventory.SearchAllProducts(null, (err, data) => {
if (err) {
console.error(err);
res.status(500).send({ error: 'something failed :(' });
} else {
res.json(data.products);
}
});
});

/**
* Consulta o frete de envio no ShippingService
*/
app.get('/shipping/:cep', (req, res, next) => {
shipping.GetShippingRate(
{
cep: req.params.cep,
},
(err, data) => {
if (err) {
console.error(err);
res.status(500).send({ error: 'something failed :(' });
} else {
res.json({
cep: req.params.cep,
value: data.value,
});
}
}
);
});
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) {
console.error(err);
res.status(500).send({ error: 'something failed :(' });
} else {
res.json(product);
}
});
});

/**
* Inicia o router
*/
app.listen(3000, () => {
console.log('Controller Service running on http://localhost:3000');
});
62 changes: 34 additions & 28 deletions services/inventory/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const products = require('./products.json');

const packageDefinition = protoLoader.loadSync('proto/inventory.proto', {
keepCase: true,
longs: String,
enums: String,
arrays: true,
});

const inventoryProto = grpc.loadPackageDefinition(packageDefinition);

const server = new grpc.Server();

// implementa os métodos do InventoryService
server.addService(inventoryProto.InventoryService.service, {
searchAllProducts: (_, callback) => {
callback(null, {
products: products,
});
},
});

server.bindAsync('127.0.0.1:3002', grpc.ServerCredentials.createInsecure(), () => {
console.log('Inventory Service running at http://127.0.0.1:3002');
server.start();
});
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const products = require('./products.json');

const packageDefinition = protoLoader.loadSync('proto/inventory.proto', {
keepCase: true,
longs: String,
enums: String,
arrays: true,
});

const inventoryProto = grpc.loadPackageDefinition(packageDefinition);

const server = new grpc.Server();

// implementa os métodos do InventoryService
server.addService(inventoryProto.InventoryService.service, {
searchAllProducts: (_, callback) => {
callback(null, {
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(), () => {
console.log('Inventory Service running at http://127.0.0.1:3002');
server.start();
});

0 comments on commit 376f724

Please sign in to comment.