carpool-service/servers/grpc/server/server.go

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-03-27 18:54:56 +00:00
package grpcserver
import (
"fmt"
"net"
"git.coopgo.io/coopgo-platform/carpool-service/handler"
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
type CarpoolServiceServerImpl struct {
Handler *handler.CarpoolServiceHandler
proto.UnimplementedCarpoolServiceServer
}
func NewCarpoolServiceServer(handler *handler.CarpoolServiceHandler) *CarpoolServiceServerImpl {
return &CarpoolServiceServerImpl{
Handler: handler,
}
}
func Run(done chan error, cfg *viper.Viper, handler *handler.CarpoolServiceHandler) {
var (
dev_env = cfg.GetBool("dev_env")
address = ":" + cfg.GetString("services.grpc.port")
)
server := grpc.NewServer()
proto.RegisterCarpoolServiceServer(server, NewCarpoolServiceServer(handler))
l, err := net.Listen("tcp", address)
if err != nil {
log.Fatal().Err(err)
return
}
if dev_env {
reflection.Register(server)
}
if err := server.Serve(l); err != nil {
fmt.Println("gRPC service ended")
done <- err
}
}