-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
32 lines (27 loc) · 822 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"context"
"log"
"net"
calculator "github.com/rvarun11/grpc-calc/go_calculator"
"google.golang.org/grpc"
)
type myCalculatorServer struct {
calculator.UnimplementedCalculatorServer
}
func (s *myCalculatorServer) Add(ctx context.Context, req *calculator.AddRequest) (*calculator.AddResponse, error) {
result := req.X + req.Y
return &calculator.AddResponse{Result: result}, nil
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
calculator.RegisterCalculatorServer(grpcServer, &myCalculatorServer{})
log.Println("Server is running on port 50051...")
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}