Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git add --all #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions proto/inventory.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -18,3 +22,7 @@ message ProductResponse {
message ProductsResponse {
repeated ProductResponse products = 1;
}

message Payload {
int32 id = 1;
}
17 changes: 16 additions & 1 deletion services/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
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
14 changes: 14 additions & 0 deletions shipping.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]