-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
86 lines (75 loc) · 2.79 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"net"
"time"
"github.com/sirupsen/logrus"
commentGen "github.com/sundogrd/comment-grpc/grpc_gen/comment"
commentRepo "github.com/sundogrd/comment-grpc/providers/repos/comment/repo"
"github.com/sundogrd/comment-grpc/servers/comment"
commentService "github.com/sundogrd/comment-grpc/services/comment/service"
configUtils "github.com/sundogrd/gopkg/config"
"github.com/sundogrd/gopkg/db"
grpcUtils "github.com/sundogrd/gopkg/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func main() {
config, err := configUtils.ReadConfigFromFile("./config", nil)
if err != nil {
logrus.Errorf("[comment-grpc] ReadConfigFromFile err: %s", err.Error())
panic(err)
}
instanceAddr := ":" + config.Get("grpcService.port").(string)
listen, err := net.Listen("tcp", instanceAddr)
if err != nil {
logrus.Errorf("[comment-grpc] net.Listen err: %s", err.Error())
panic(err)
}
gormDB, err := db.Connect(db.ConnectOptions{
User: config.Get("db.options.user").(string),
Password: config.Get("db.options.password").(string),
Host: config.Get("db.options.host").(string),
Port: config.Get("db.options.port").(string),
DBName: config.Get("db.options.dbname").(string),
ConnectTimeout: config.Get("db.options.connectTimeout").(string),
})
if err != nil {
logrus.Errorf("[comment-grpc] db.Connect err: %s", err.Error())
panic(err)
}
logrus.Printf("[comment-grpc] db.Connect finished")
cr, err := commentRepo.NewCommentRepo(gormDB, 2*time.Second)
if err != nil {
logrus.Errorf("[comment-grpc] NewCommentRepo err: %s", err.Error())
panic(err)
}
logrus.Printf("[comment-grpc] NewCommentRepo finished")
cs, err := commentService.NewCommentService(&cr, 2*time.Second)
if err != nil {
logrus.Errorf("[comment-grpc] NewCommentService err: %s", err.Error())
panic(err)
}
logrus.Printf("[comment-grpc] NewCommentService finished")
grpcServer := grpc.NewServer()
endpoints := config.Get("etcd.host").(string) + ":" + config.Get("etcd.port").(string)
logrus.Printf("etcd config is %s", endpoints)
resolver, err := grpcUtils.NewGrpcResolover(endpoints)
if err != nil {
logrus.Errorf("[comment-grpc] NewGrpcResolover err: %s", err.Error())
panic(err)
}
logrus.Printf("[comment-grpc] NewGrpcResolover finished")
err = grpcUtils.ResgiterServer(*resolver, "sundog.comment", instanceAddr, 5*time.Second, 5)
if err != nil {
logrus.Errorf("[comment-grpc] RegisterServer err: %s", err.Error())
panic(err)
}
logrus.Printf("[comment-grpc] ResgiterServer finished, service: %s, %s", "sundog.comment", instanceAddr)
commentGen.RegisterCommentServiceServer(grpcServer, &comment.CommentServiceServer{
GormDB: gormDB,
CommentRepo: cr,
CommentService: cs,
})
reflection.Register(grpcServer)
grpcServer.Serve(listen)
}