Skip to content

Commit

Permalink
feat: support custom sephirah service name
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Feb 21, 2024
1 parent bce8aba commit e5c7ea0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tuihub

import (
"context"
"os"
"sync"
"time"

Expand Down Expand Up @@ -53,7 +54,7 @@ func LoginByPassword(
for _, o := range options {
o(c)
}
client, err := internal.NewSephirahClient(ctx, c.consulConfig)
client, err := internal.NewSephirahClient(ctx, c.consulConfig, os.Getenv(sephirahServiceName))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func LoginByRefreshToken(
for _, o := range options {
o(c)
}
client, err := internal.NewSephirahClient(ctx, c.consulConfig)
client, err := internal.NewSephirahClient(ctx, c.consulConfig, os.Getenv(sephirahServiceName))
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions internal/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ import (
capi "github.com/hashicorp/consul/api"
)

func NewSephirahClient(ctx context.Context, config *capi.Config) (sephirah.LibrarianSephirahServiceClient, error) {
func NewSephirahClient(
ctx context.Context,
config *capi.Config,
serviceName string,
) (sephirah.LibrarianSephirahServiceClient, error) {
r, err := NewRegistry(config)
if err != nil {
return nil, err
}
if serviceName == "" {
serviceName = "librarian"
}
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("discovery:///sephirah"),
grpc.WithEndpoint("discovery:///"+serviceName),
grpc.WithDiscovery(r),
grpc.WithMiddleware(
recovery.Recovery(),
Expand Down
15 changes: 8 additions & 7 deletions porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
)

const (
serverNetwork = "SERVER_NETWORK"
serverAddr = "SERVER_ADDRESS"
serverTimeout = "SERVER_TIMEOUT"
consulAddr = "CONSUL_ADDRESS"
consulToken = "CONSUL_TOKEN"
serverNetwork = "SERVER_NETWORK"
serverAddr = "SERVER_ADDRESS"
serverTimeout = "SERVER_TIMEOUT"
consulAddr = "CONSUL_ADDRESS"
consulToken = "CONSUL_TOKEN"
sephirahServiceName = "SEPHIRAH_SERVICE_NAME"
)

type Porter struct {
Expand Down Expand Up @@ -93,7 +94,7 @@ func NewPorter(ctx context.Context, config PorterConfig, handler Handler, option
if p.consulConfig == nil {
p.consulConfig = defaultConsulConfig()
}
client, err := internal.NewSephirahClient(ctx, p.consulConfig)
client, err := internal.NewSephirahClient(ctx, p.consulConfig, os.Getenv(sephirahServiceName))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -180,7 +181,7 @@ func (p *Porter) AsUser(ctx context.Context, userID int64) (*LibrarianClient, er
if p.wrapper.Token == nil {
return nil, fmt.Errorf("porter not enabled")
}
client, err := internal.NewSephirahClient(ctx, p.consulConfig)
client, err := internal.NewSephirahClient(ctx, p.consulConfig, os.Getenv(sephirahServiceName))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e5c7ea0

Please sign in to comment.