/
server.go
203 lines (164 loc) · 5.54 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package apiserver
import (
"context"
"fmt"
pb "github.com/marmotedu/api/proto/apiserver/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"github.com/marmotedu/iam/internal/apiserver/config"
cachev1 "github.com/marmotedu/iam/internal/apiserver/controller/v1/cache"
"github.com/marmotedu/iam/internal/apiserver/store"
"github.com/marmotedu/iam/internal/apiserver/store/mysql"
genericoptions "github.com/marmotedu/iam/internal/pkg/options"
genericapiserver "github.com/marmotedu/iam/internal/pkg/server"
"github.com/marmotedu/iam/pkg/log"
"github.com/marmotedu/iam/pkg/shutdown"
"github.com/marmotedu/iam/pkg/shutdown/shutdownmanagers/posixsignal"
"github.com/marmotedu/iam/pkg/storage"
)
type apiServer struct {
gs *shutdown.GracefulShutdown
redisOptions *genericoptions.RedisOptions
gRPCAPIServer *grpcAPIServer
genericAPIServer *genericapiserver.GenericAPIServer
}
type preparedAPIServer struct {
*apiServer
}
// ExtraConfig defines extra configuration for the iam-apiserver.
type ExtraConfig struct {
Addr string
MaxMsgSize int
ServerCert genericoptions.GeneratableKeyCert
mysqlOptions *genericoptions.MySQLOptions
// etcdOptions *genericoptions.EtcdOptions
}
func createAPIServer(cfg *config.Config) (*apiServer, error) {
gs := shutdown.New()
gs.AddShutdownManager(posixsignal.NewPosixSignalManager())
genericConfig, err := buildGenericConfig(cfg)
if err != nil {
return nil, err
}
extraConfig, err := buildExtraConfig(cfg)
if err != nil {
return nil, err
}
genericServer, err := genericConfig.Complete().New()
if err != nil {
return nil, err
}
extraServer, err := extraConfig.complete().New()
if err != nil {
return nil, err
}
server := &apiServer{
gs: gs,
redisOptions: cfg.RedisOptions,
genericAPIServer: genericServer,
gRPCAPIServer: extraServer,
}
return server, nil
}
func (s *apiServer) PrepareRun() preparedAPIServer {
initRouter(s.genericAPIServer.Engine)
s.initRedisStore()
s.gs.AddShutdownCallback(shutdown.ShutdownFunc(func(string) error {
mysqlStore, _ := mysql.GetMySQLFactoryOr(nil)
if mysqlStore != nil {
return mysqlStore.Close()
}
s.gRPCAPIServer.Close()
s.genericAPIServer.Close()
return nil
}))
return preparedAPIServer{s}
}
func (s preparedAPIServer) Run() error {
go s.gRPCAPIServer.Run()
// start shutdown managers
if err := s.gs.Start(); err != nil {
log.Fatalf("start shutdown manager failed: %s", err.Error())
}
return s.genericAPIServer.Run()
}
type completedExtraConfig struct {
*ExtraConfig
}
// Complete fills in any fields not set that are required to have valid data and can be derived from other fields.
func (c *ExtraConfig) complete() *completedExtraConfig {
if c.Addr == "" {
c.Addr = "127.0.0.1:8081"
}
return &completedExtraConfig{c}
}
// New create a grpcAPIServer instance.
func (c *completedExtraConfig) New() (*grpcAPIServer, error) {
creds, err := credentials.NewServerTLSFromFile(c.ServerCert.CertKey.CertFile, c.ServerCert.CertKey.KeyFile)
if err != nil {
log.Fatalf("Failed to generate credentials %s", err.Error())
}
opts := []grpc.ServerOption{grpc.MaxRecvMsgSize(c.MaxMsgSize), grpc.Creds(creds)}
grpcServer := grpc.NewServer(opts...)
storeIns, _ := mysql.GetMySQLFactoryOr(c.mysqlOptions)
// storeIns, _ := etcd.GetEtcdFactoryOr(c.etcdOptions, nil)
store.SetClient(storeIns)
cacheIns, err := cachev1.GetCacheInsOr(storeIns)
if err != nil {
log.Fatalf("Failed to get cache instance: %s", err.Error())
}
pb.RegisterCacheServer(grpcServer, cacheIns)
reflection.Register(grpcServer)
return &grpcAPIServer{grpcServer, c.Addr}, nil
}
func buildGenericConfig(cfg *config.Config) (genericConfig *genericapiserver.Config, lastErr error) {
genericConfig = genericapiserver.NewConfig()
if lastErr = cfg.GenericServerRunOptions.ApplyTo(genericConfig); lastErr != nil {
return
}
if lastErr = cfg.FeatureOptions.ApplyTo(genericConfig); lastErr != nil {
return
}
if lastErr = cfg.SecureServing.ApplyTo(genericConfig); lastErr != nil {
return
}
if lastErr = cfg.InsecureServing.ApplyTo(genericConfig); lastErr != nil {
return
}
return
}
//nolint: unparam
func buildExtraConfig(cfg *config.Config) (*ExtraConfig, error) {
return &ExtraConfig{
Addr: fmt.Sprintf("%s:%d", cfg.GRPCOptions.BindAddress, cfg.GRPCOptions.BindPort),
MaxMsgSize: cfg.GRPCOptions.MaxMsgSize,
ServerCert: cfg.SecureServing.ServerCert,
mysqlOptions: cfg.MySQLOptions,
// etcdOptions: cfg.EtcdOptions,
}, nil
}
func (s *apiServer) initRedisStore() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
config := &storage.Config{
Host: s.redisOptions.Host,
Port: s.redisOptions.Port,
Addrs: s.redisOptions.Addrs,
MasterName: s.redisOptions.MasterName,
Username: s.redisOptions.Username,
Password: s.redisOptions.Password,
Database: s.redisOptions.Database,
MaxIdle: s.redisOptions.MaxIdle,
MaxActive: s.redisOptions.MaxActive,
Timeout: s.redisOptions.Timeout,
EnableCluster: s.redisOptions.EnableCluster,
UseSSL: s.redisOptions.UseSSL,
SSLInsecureSkipVerify: s.redisOptions.SSLInsecureSkipVerify,
}
// try to connect to redis
go storage.ConnectToRedis(ctx, config)
}