Skip to content

Commit

Permalink
feat: add default server config
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Jan 10, 2024
1 parent cc2a363 commit fa66e62
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

const (
serverNetwork = "SERVER_NETWORK"
serverPort = "SERVER_PORT"
serverTimeout = "SERVER_TIMEOUT"
)

type Porter struct {
server *grpc.Server
wrapper wrapper
logger log.Logger
app *kratos.App
consulConfig *capi.Config
serverConfig ServerConfig
}

type PorterConfig struct {
Name string
Version string
GlobalName string
FeatureSummary *porter.PorterFeatureSummary
Server ServerConfig
Server *ServerConfig
}

type ServerConfig struct {
Expand Down Expand Up @@ -73,6 +80,9 @@ func NewPorter(ctx context.Context, config PorterConfig, handler Handler, option
for _, o := range options {
o(p)
}
if p.consulConfig == nil {
p.serverConfig = defaultServerConfig()
}
client, err := internal.NewSephirahClient(ctx, p.consulConfig)
if err != nil {
return nil, err
Expand All @@ -90,7 +100,7 @@ func NewPorter(ctx context.Context, config PorterConfig, handler Handler, option
}
p.wrapper = c
p.server = NewServer(
&config.Server,
config.Server,
NewService(c),
p.logger,
)
Expand All @@ -110,6 +120,28 @@ func NewPorter(ctx context.Context, config PorterConfig, handler Handler, option
return p, nil
}

func defaultServerConfig() ServerConfig {
minute := time.Minute
config := ServerConfig{
Network: "0.0.0.0",
Addr: "",
Timeout: &minute,
}
if network, exist := os.LookupEnv(serverNetwork); exist {
config.Network = network
}
if port, exist := os.LookupEnv(serverPort); exist {
config.Addr = port
}
if timeout, exist := os.LookupEnv(serverTimeout); exist {
d, err := time.ParseDuration(timeout)
if err == nil {
config.Timeout = &d
}
}
return config
}

func WellKnownToString(e protoreflect.Enum) string {
return fmt.Sprint(proto.GetExtension(
e.
Expand Down

0 comments on commit fa66e62

Please sign in to comment.