generated from calmato/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
114 lines (101 loc) · 2.7 KB
/
registry.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package native
import (
"crypto/tls"
"crypto/x509"
v1 "github.com/calmato/gran-book/api/internal/gateway/native/v1/handler"
v2 "github.com/calmato/gran-book/api/internal/gateway/native/v2/handler"
"github.com/calmato/gran-book/api/internal/gateway/util"
"github.com/calmato/gran-book/api/pkg/datetime"
"github.com/calmato/gran-book/api/pkg/firebase/authentication"
"github.com/calmato/gran-book/api/proto/book"
"github.com/calmato/gran-book/api/proto/chat"
"github.com/calmato/gran-book/api/proto/user"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type registry struct {
Authenticator util.Authenticator
V1Api v1.APIV1Handler
V2Api v2.APIV2Handler
}
// params - DIコンテナ生成用のパラメータ
type params struct {
Insecure bool
FirebaseAuth *authentication.Auth
AuthServiceURL string
BookServiceURL string
ChatServiceURL string
UserServiceURL string
}
type gRPCClient struct {
auth user.AuthServiceClient
book book.BookServiceClient
chat chat.ChatServiceClient
user user.UserServiceClient
}
func newRegistry(params *params) (*registry, error) {
client, err := newGRPCClient(params)
if err != nil {
return nil, err
}
v1Params := &v1.Params{
Auth: client.auth,
User: client.user,
Chat: client.chat,
Book: client.book,
}
v2Params := &v2.Params{
Auth: client.auth,
User: client.user,
Book: client.book,
}
return ®istry{
Authenticator: util.NewAuthenticator(params.FirebaseAuth),
V1Api: v1.NewAPIV1Handler(v1Params, datetime.Now),
V2Api: v2.NewAPIV2Handler(v2Params, datetime.Now),
}, nil
}
func newGRPCClient(params *params) (*gRPCClient, error) {
opts, err := newGRPCOptios(params)
if err != nil {
return nil, err
}
authConn, err := grpc.Dial(params.AuthServiceURL, opts...)
if err != nil {
return nil, err
}
bookConn, err := grpc.Dial(params.BookServiceURL, opts...)
if err != nil {
return nil, err
}
chatConn, err := grpc.Dial(params.ChatServiceURL, opts...)
if err != nil {
return nil, err
}
userConn, err := grpc.Dial(params.UserServiceURL, opts...)
if err != nil {
return nil, err
}
return &gRPCClient{
auth: user.NewAuthServiceClient(authConn),
user: user.NewUserServiceClient(userConn),
chat: chat.NewChatServiceClient(chatConn),
book: book.NewBookServiceClient(bookConn),
}, nil
}
func newGRPCOptios(params *params) ([]grpc.DialOption, error) {
var opts []grpc.DialOption
if params.Insecure {
opts = append(opts, grpc.WithInsecure())
} else {
systemRoots, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
cred := credentials.NewTLS(&tls.Config{
RootCAs: systemRoots,
})
opts = append(opts, grpc.WithTransportCredentials(cred))
}
return opts, nil
}