Created
April 20, 2021 16:09
-
-
Save karanchuri/f8034c35a15f9811e22aaf4c580e2235 to your computer and use it in GitHub Desktop.
Server File For gRPC demo
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 ( | |
"context" | |
"fmt" | |
"net" | |
"grpcdemo/proto" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/reflection" | |
) | |
type server struct{} | |
func main() { | |
listener, err := net.Listen("tcp", ":8080") | |
if err != nil { | |
panic(err) | |
} | |
srv := grpc.NewServer() | |
proto.RegisterServiceServer(srv, &server{}) | |
reflection.Register(srv) | |
if e := srv.Serve(listener); e != nil { | |
panic(err) | |
} | |
} | |
func (s *server) Ping(ctx context.Context, request *proto.PingRequest) (response *proto.PongResponse, err error) { | |
message, id := request.GetMessage(), request.GetId() | |
return &proto.PongResponse{ | |
Message: fmt.Sprintf("Reply of %s with ID: %d is OK :)", message, id), | |
}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment