/
main.go
53 lines (45 loc) · 1.08 KB
/
main.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
package main
import (
"fmt"
pb "github.com/amitsaha/apigatewaydemo/grpc-app-1/verify"
consulapi "github.com/hashicorp/consul/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
"os"
)
const (
port = "127.0.0.1:50051"
)
type server struct{}
func (s *server) VerifyUser(ctx context.Context, in *pb.VerifyRequest) (*pb.VerifyReply, error) {
msg := fmt.Sprintf("Verified: %d", in.Id)
return &pb.VerifyReply{Message: msg}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
consulConfig := consulapi.DefaultConfig()
consulClient, err := consulapi.NewClient(consulConfig)
if err != nil {
log.Fatalf("err", err)
os.Exit(1)
}
agent := consulClient.Agent()
reg := &consulapi.AgentServiceRegistration{
Name: "verification",
Port: 50051,
Tags: []string{},
}
if err := agent.ServiceRegister(reg); err != nil {
log.Fatalf("err: %v", err)
}
s := grpc.NewServer()
pb.RegisterUserVerifyServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}