Last active
February 20, 2017 10:31
-
-
Save ru-rocker/fa99c0524b3af9f4486459a6f79997d5 to your computer and use it in GitHub Desktop.
lorem-grpc server
This file contains 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 main | |
import ( | |
"net" | |
"flag" | |
"github.com/ru-rocker/gokit-playground/lorem-grpc" | |
context "golang.org/x/net/context" | |
"google.golang.org/grpc" | |
"github.com/ru-rocker/gokit-playground/lorem-grpc/pb" | |
"os" | |
"os/signal" | |
"syscall" | |
"fmt" | |
) | |
func main() { | |
var ( | |
gRPCAddr = flag.String("grpc", ":8081", | |
"gRPC listen address") | |
) | |
flag.Parse() | |
ctx := context.Background() | |
// init lorem service | |
var svc lorem_grpc.Service | |
svc = lorem_grpc.LoremService{} | |
errChan := make(chan error) | |
// creating Endpoints struct | |
endpoints := lorem_grpc.Endpoints{ | |
LoremEndpoint: lorem_grpc.MakeLoremEndpoint(svc), | |
} | |
//execute grpc server | |
go func() { | |
listener, err := net.Listen("tcp", *gRPCAddr) | |
if err != nil { | |
errChan <- err | |
return | |
} | |
handler := lorem_grpc.NewGRPCServer(ctx, endpoints) | |
gRPCServer := grpc.NewServer() | |
pb.RegisterLoremServer(gRPCServer, handler) | |
errChan <- gRPCServer.Serve(listener) | |
}() | |
go func() { | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | |
errChan <- fmt.Errorf("%s", <-c) | |
}() | |
go func() { | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | |
errChan <- fmt.Errorf("%s", <-c) | |
}() | |
fmt.Println(<- errChan) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment