forked from rcaa/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.
- Loading branch information
1 parent
5b4066a
commit 716f489
Showing
12 changed files
with
4,649 additions
and
87 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FROM node:18-alpine | ||
|
||
# Defina o diretório de trabalho dentro do container | ||
WORKDIR /app | ||
|
||
# Copie apenas os arquivos de dependências primeiro | ||
COPY package.json package-lock.json ./ | ||
|
||
# Instale as dependências | ||
RUN npm install | ||
|
||
# Copie todo o código-fonte para dentro do container | ||
COPY . . | ||
|
||
# Exponha a porta onde o Shipping Service está rodando | ||
EXPOSE 3001 | ||
|
||
# Comando para iniciar o serviço | ||
CMD ["node", "services/shipping/index.js"] |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Usar a imagem do Node.js | ||
FROM node:18 | ||
|
||
# Definir o diretório de trabalho no contêiner | ||
WORKDIR /app | ||
|
||
# Copiar o package.json e package-lock.json | ||
COPY package.json package-lock.json ./ | ||
|
||
# Instalar as dependências | ||
RUN npm install | ||
|
||
# Copiar o restante do código | ||
COPY . . | ||
|
||
# Expor a porta do serviço | ||
EXPOSE 3000 | ||
|
||
# Comando para iniciar a aplicação | ||
CMD ["npm", "start"] |
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,37 +1,40 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const port = 3000; | ||
|
||
// Importando os pacotes necessários | ||
const grpc = require('@grpc/grpc-js'); | ||
const grpc = require('grpc'); | ||
const protoLoader = require('@grpc/proto-loader'); | ||
|
||
// Caminho para o arquivo .proto | ||
const PROTO_PATH = './proto/inventory.proto'; | ||
|
||
// Carregando o arquivo .proto | ||
const packageDefinition = protoLoader.loadSync('proto/inventory.proto', { | ||
keepCase: true, | ||
longs: String, | ||
enums: String, | ||
arrays: true, | ||
const packageDefinition = protoLoader.loadSync(PROTO_PATH, { | ||
keepCase: true, // Mantém o nome das mensagens e serviços com a mesma capitalização do arquivo .proto | ||
longs: String, // Converte os números longos para strings | ||
enums: String, // Converte os enums para strings | ||
defaults: true, // Preenche valores ausentes com o valor padrão | ||
oneofs: true // Resolve problemas de campos "oneof" | ||
}); | ||
|
||
const inventoryProto = grpc.loadPackageDefinition(packageDefinition).InventoryService; | ||
// Carregando o pacote do Protobuf | ||
const inventoryProto = grpc.loadPackageDefinition(packageDefinition).inventory; | ||
|
||
// Criando um cliente gRPC para o InventoryService | ||
const inventory = new inventoryProto('localhost:3002', grpc.credentials.createInsecure()); | ||
// Criando o servidor gRPC | ||
const server = new grpc.Server(); | ||
|
||
// Rota para pesquisar um produto por ID | ||
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); // Retorna o produto encontrado | ||
} | ||
}); | ||
// Implementação do serviço | ||
server.addService(inventoryProto.InventoryService.service, { | ||
SearchProductByID: (call, callback) => { | ||
// Lógica para SearchProductByID | ||
callback(null, { id: '1', name: 'Produto 1', description: 'Descrição do produto 1' }); | ||
}, | ||
AddProduct: (call, callback) => { | ||
// Lógica para AddProduct | ||
callback(null, { message: 'Produto adicionado com sucesso!', success: true }); | ||
} | ||
}); | ||
|
||
// Iniciando o servidor HTTP | ||
app.listen(port, () => { | ||
console.log(`Server running at http://localhost:${port}`); | ||
// Iniciando o servidor na porta 3000 | ||
server.bind('0.0.0.0:3000', grpc.ServerCredentials.createInsecure(), () => { | ||
console.log('Servidor gRPC rodando na porta 3000'); | ||
}); | ||
|
||
// Iniciando o servidor | ||
server.start(); |
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,14 +1,18 @@ | ||
const grpc = require('@grpc/grpc-js'); | ||
const protoLoader = require('@grpc/proto-loader'); | ||
|
||
// Carregar o arquivo proto | ||
const packageDefinition = protoLoader.loadSync('proto/inventory.proto', { | ||
keepCase: true, | ||
longs: String, | ||
enums: String, | ||
arrays: true, | ||
}); | ||
|
||
const InventoryService = grpc.loadPackageDefinition(packageDefinition).InventoryService; | ||
// Carregar o serviço InventoryService do arquivo proto | ||
const InventoryService = grpc.loadPackageDefinition(packageDefinition).inventory.InventoryService; | ||
|
||
// Criar o cliente gRPC | ||
const client = new InventoryService('127.0.0.1:3002', grpc.credentials.createInsecure()); | ||
|
||
module.exports = client; |
Oops, something went wrong.