-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
55 lines (47 loc) · 1.19 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package syncer
import (
"errors"
"fmt"
"github.com/ahmadmuzakki29/go-syncer/pb"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
)
type Config struct {
Port string
LogLevel string
}
func Serve(cfg Config) {
LogLevel = getLogLevel(cfg.LogLevel)
lis, err := net.Listen("tcp", ":"+cfg.Port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterSyncerServer(s, &server{})
reflection.Register(s)
fmt.Println("serving on :" + cfg.Port)
fmt.Println("log level : " + cfg.LogLevel)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
type server struct{}
func (s *server) Lock(ctx context.Context, req *pb.LockRequest) (*pb.Reply, error) {
id := req.Id
if id == "" {
return &pb.Reply{Message: "id must be specified"}, errors.New("id must be specified")
}
lock(req)
return &pb.Reply{Message: "OK"}, nil
}
func (s *server) Unlock(ctx context.Context, req *pb.LockRequest) (*pb.Reply, error) {
id := req.Id
if id == "" {
return &pb.Reply{Message: "id must be specified"}, errors.New("id must be specified")
}
unlock(id)
return &pb.Reply{Message: "OK"}, nil
}