forked from aserg-ufmg/micro-livraria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tarefa prática aserg-ufmg#1 - Microservices
- Loading branch information
Showing
3 changed files
with
136 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |