37 lines
788 B
Go
37 lines
788 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
|
|
pb "git.strikerlulu.me/strikerlulu/grpc-mocker/pb"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
)
|
|
|
|
type CalcServer struct{
|
|
pb.UnimplementedCalcServiceServer
|
|
response *pb.AdditionResponse
|
|
err error
|
|
}
|
|
|
|
func (s *CalcServer) Add(ctx context.Context, r *pb.AdditionRequest) (*pb.AdditionResponse, error) {
|
|
result := r.X + r.Y
|
|
return &pb.AdditionResponse{Result: result}, nil
|
|
}
|
|
|
|
func main() {
|
|
lis,err := net.Listen("tcp",":3000")
|
|
if err != nil {
|
|
log.Fatalf("Failed to listen: %v", err)
|
|
}
|
|
s := grpc.NewServer()
|
|
pb.RegisterCalcServiceServer(s, &CalcServer{})
|
|
reflection.Register(s)
|
|
if err := s.Serve(lis); err != nil {
|
|
log.Fatalf("Failed to server: %v", err)
|
|
}
|
|
}
|