Paperclip/pkg/internal/grpc/server.go

41 lines
743 B
Go
Raw Permalink Normal View History

2024-05-17 07:59:51 +00:00
package grpc
import (
2024-07-28 13:03:56 +00:00
"net"
2024-10-27 05:13:40 +00:00
"git.solsynth.dev/hypernet/nexus/pkg/proto"
2024-05-17 07:59:51 +00:00
"github.com/spf13/viper"
"google.golang.org/grpc"
2024-06-22 04:18:54 +00:00
health "google.golang.org/grpc/health/grpc_health_v1"
2024-05-17 07:59:51 +00:00
"google.golang.org/grpc/reflection"
)
type Server struct {
2024-10-27 05:13:40 +00:00
proto.UnimplementedDirectoryServiceServer
health.UnimplementedHealthServer
srv *grpc.Server
2024-05-17 07:59:51 +00:00
}
2024-10-27 05:13:40 +00:00
func NewGrpc() *Server {
server := &Server{
srv: grpc.NewServer(),
}
2024-05-17 07:59:51 +00:00
2024-10-27 05:13:40 +00:00
proto.RegisterDirectoryServiceServer(server.srv, server)
health.RegisterHealthServer(server.srv, server)
2024-05-17 07:59:51 +00:00
2024-10-27 05:13:40 +00:00
reflection.Register(server.srv)
2024-05-17 07:59:51 +00:00
2024-10-27 05:13:40 +00:00
return server
2024-06-22 04:18:54 +00:00
}
2024-10-27 05:13:40 +00:00
func (v *Server) Listen() error {
2024-06-22 04:18:54 +00:00
listener, err := net.Listen("tcp", viper.GetString("grpc_bind"))
if err != nil {
return err
}
2024-05-17 07:59:51 +00:00
2024-10-27 05:13:40 +00:00
return v.srv.Serve(listener)
2024-05-17 07:59:51 +00:00
}