import { Server, ServerCredentials, loadPackageDefinition } from "@grpc/grpc-js";
import { loadSync } from "@grpc/proto-loader";

// Ler as definições do protobuffer
const tasksDefs = loadSync("./tasks.proto"); 
const tasksProto = loadPackageDefinition(tasksDefs);

const tasks = [
  { id: 1, title: "Tarefa 01", type: "IFMS" }  
]

// Criando nosso objeto Servidor
const grpcServer = new Server();
grpcServer.addService(tasksProto.TaskService.service, {
  findAll: (call, callBack) => {
    callBack(null, { tasks: tasks })
  } 
})

const serverAddress = '0.0.0.0:5050';
grpcServer.bindAsync(serverAddress, ServerCredentials.createInsecure(), () => {
  console.log("Servidor está funcionando bem...")
})
