Skip to content

Commit 8a7b027

Browse files
committed
add client lib
1 parent fa2c0dc commit 8a7b027

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

auth/auth.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@ type Auth interface {
1515
}
1616

1717
// NewAuth return auth obj
18-
func NewAuth(config types.Config) Auth {
19-
authConfig := config.Auth
20-
basicAuth := simple.NewBasicAuth(authConfig.Username, authConfig.Password)
21-
return basicAuth
18+
func NewAuth(auth types.AuthConfig) Auth {
19+
// TODO 这里可以组装其他的方法
20+
return simple.NewBasicAuth(auth.Username, auth.Password)
21+
}
22+
23+
// Credential for client
24+
type Credential interface {
25+
GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
26+
RequireTransportSecurity() bool
27+
}
28+
29+
// NewCredential return credential obj
30+
func NewCredential(auth types.AuthConfig) Credential {
31+
// TODO 这里可以组装其他的方法
32+
return simple.NewBasicCredential(auth.Username, auth.Password)
2233
}

auth/simple/credential.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package simple
2+
3+
import "context"
4+
5+
// BasicCredential for basic credential
6+
type BasicCredential struct {
7+
username string
8+
password string
9+
}
10+
11+
// NewBasicCredential new a basic credential
12+
func NewBasicCredential(username, password string) *BasicCredential {
13+
return &BasicCredential{username, password}
14+
}
15+
16+
// GetRequestMetadata for basic auth
17+
func (c BasicCredential) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
18+
return map[string]string{
19+
c.username: c.password,
20+
}, nil
21+
}
22+
23+
// RequireTransportSecurity for ssl require
24+
func (c BasicCredential) RequireTransportSecurity() bool {
25+
return false
26+
}

client/client.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package client
2+
3+
import (
4+
"github.com/projecteru2/core/auth"
5+
pb "github.com/projecteru2/core/rpc/gen"
6+
"github.com/projecteru2/core/types"
7+
log "github.com/sirupsen/logrus"
8+
"google.golang.org/grpc"
9+
)
10+
11+
// Client contain grpc conn
12+
type Client struct {
13+
addr string
14+
conn *grpc.ClientConn
15+
}
16+
17+
// NewClient new a client
18+
func NewClient(addr string, authConfig types.AuthConfig) *Client {
19+
conn := connect(addr, authConfig)
20+
return &Client{addr: addr, conn: conn}
21+
}
22+
23+
// GetConn return connection
24+
func (c *Client) GetConn() *grpc.ClientConn {
25+
return c.conn
26+
}
27+
28+
// GetRPCClient return rpc client
29+
func (c *Client) GetRPCClient() pb.CoreRPCClient {
30+
return pb.NewCoreRPCClient(c.conn)
31+
}
32+
33+
func connect(addr string, authConfig types.AuthConfig) *grpc.ClientConn {
34+
opts := []grpc.DialOption{grpc.WithInsecure()}
35+
if authConfig.Username != "" {
36+
opts = append(opts, grpc.WithPerRPCCredentials(auth.NewCredential(authConfig)))
37+
}
38+
39+
conn, err := grpc.Dial(addr, opts...)
40+
if err != nil {
41+
log.Fatalf("[ConnectEru] Can not connect %v", err)
42+
}
43+
log.Debugf("[ConnectEru] Init eru connection %s", addr)
44+
return conn
45+
}

core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func serve() {
7373

7474
if config.Auth.Username != "" {
7575
log.Info("[main] Cluster auth enable.")
76-
auth := auth.NewAuth(config)
76+
auth := auth.NewAuth(config.Auth)
7777
opts = append(opts, grpc.StreamInterceptor(auth.StreamInterceptor))
7878
opts = append(opts, grpc.UnaryInterceptor(auth.UnaryInterceptor))
7979
log.Infof("[main] Username %s Password %s", config.Auth.Username, config.Auth.Password)

0 commit comments

Comments
 (0)