package api

import (
	"context"
	"fmt"
	"log"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"

	pb "deevirt.fr/compute/pkg/api/proto"
	"deevirt.fr/compute/pkg/raft"
)

func Server() {
	ctx := context.Background()

	sock, err := net.Listen("tcp", fmt.Sprintf(":%d", 4480))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	r, tm, err := raft.New(ctx, 4480)
	if err != nil {
		log.Fatalf("failed to start raft: %v", err)
	}

	s := grpc.NewServer()
	pb.RegisterDomainServer(s, nil)
	tm.Register(s)
	//leaderhealth.Setup(r, s, []string{"Example"})
	raft.Register(s, r)
	reflection.Register(s)
	if err := s.Serve(sock); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
