Created
September 16, 2018 06:49
-
-
Save amsokol/0c8b5fcee7c725f0e15f4eef5dfcf80c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package grpc | |
import ( | |
"context" | |
"net" | |
"os" | |
"os/signal" | |
"google.golang.org/grpc" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/api/v1" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/logger" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/protocol/grpc/middleware" | |
) | |
// RunServer runs gRPC service to publish ToDo service | |
func RunServer(ctx context.Context, v1API v1.ToDoServiceServer, port string) error { | |
listen, err := net.Listen("tcp", ":"+port) | |
if err != nil { | |
return err | |
} | |
// gRPC server statup options | |
opts := []grpc.ServerOption{} | |
// add middleware | |
opts = middleware.AddLogging(logger.Log, opts) | |
// register service | |
server := grpc.NewServer(opts...) | |
v1.RegisterToDoServiceServer(server, v1API) | |
// graceful shutdown | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt) | |
go func() { | |
for range c { | |
// sig is a ^C, handle it | |
logger.Log.Warn("shutting down gRPC server...") | |
server.GracefulStop() | |
<-ctx.Done() | |
} | |
}() | |
// start gRPC server | |
logger.Log.Info("starting gRPC server...") | |
return server.Serve(listen) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment