-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleserver.go
46 lines (38 loc) · 1.07 KB
/
simpleserver.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"context"
"log"
"net"
"github.com/byteshiva/simplegrpc/addition"
"github.com/byteshiva/simplegrpc/subtraction"
"google.golang.org/grpc"
)
type mathServer struct {
}
func (s *mathServer) Add(c context.Context, addRequest *addition.AddRequest) (*addition.AddResponse, error) {
result := addRequest.Number + addRequest.AnotherNumber
response := addition.AddResponse{
Sum: int64(result),
}
return &response, nil
}
func (s *mathServer) Minus(c context.Context, minusRequest *subtraction.MinusRequest) (*subtraction.MinusResponse, error) {
result := minusRequest.Number - minusRequest.AnotherNumber
response := subtraction.MinusResponse{
Subtract: int64(result),
}
return &response, nil
}
func newAddServer() *mathServer {
return &mathServer{}
}
func main() {
lis, err := net.Listen("tcp", "localhost:10000")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
addition.RegisterAdditionServer(grpcServer, newAddServer())
subtraction.RegisterSubtractionServer(grpcServer, newAddServer())
grpcServer.Serve(lis)
}