Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Add optional flag to bind frontend ip address (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreiding authored Dec 1, 2021
1 parent 50d4a31 commit 6070027
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
18 changes: 18 additions & 0 deletions cmd/temporalite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"fmt"
goLog "log"
"net"
"os"

"github.com/urfave/cli/v2"
Expand All @@ -31,6 +32,7 @@ const (
ephemeralFlag = "ephemeral"
dbPathFlag = "filename"
portFlag = "port"
ipFlag = "ip"
logFormatFlag = "log-format"
namespaceFlag = "namespace"
)
Expand Down Expand Up @@ -87,6 +89,12 @@ func buildCLI() *cli.App {
EnvVars: nil,
Value: nil,
},
&cli.StringFlag{
Name: ipFlag,
Usage: `IPv4 address to bind the frontend service to instead of localhost`,
EnvVars: nil,
Value: "",
},
},
Before: func(c *cli.Context) error {
if c.Args().Len() > 0 {
Expand All @@ -100,6 +108,12 @@ func buildCLI() *cli.App {
default:
return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(logFormatFlag), logFormatFlag), 1)
}

// Check that ip address is valid
if c.IsSet(ipFlag) && net.ParseIP(c.String(ipFlag)) == nil {
return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(ipFlag), ipFlag), 1)
}

return nil
},
Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -128,6 +142,10 @@ func buildCLI() *cli.App {
opts = append(opts, temporalite.WithLogger(logger))
}

if c.IsSet(ipFlag) {
opts = append(opts, temporalite.WithFrontendIP(c.String(ipFlag)))
}

s, err := temporalite.NewServer(opts...)
if err != nil {
return err
Expand Down
34 changes: 21 additions & 13 deletions internal/liteconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
Logger log.Logger
UpstreamOptions []temporal.ServerOption
portProvider *portProvider
FrontendIP string
}

func NewDefaultConfig() (*Config, error) {
Expand All @@ -54,6 +55,7 @@ func NewDefaultConfig() (*Config, error) {
OutputFile: "",
})),
portProvider: &portProvider{},
FrontendIP: "",
}, nil
}

Expand Down Expand Up @@ -165,22 +167,28 @@ func Convert(cfg *Config) *config.Config {
}

func (o *Config) mustGetService(frontendPortOffset int) config.Service {
var (
grpcPort = o.FrontendPort + frontendPortOffset
membershipPort = o.FrontendPort + 100 + frontendPortOffset
)
if o.DynamicPorts {
if frontendPortOffset != 0 {
grpcPort = o.portProvider.mustGetFreePort()
}
membershipPort = o.portProvider.mustGetFreePort()
}
return config.Service{
svc := config.Service{
RPC: config.RPC{
GRPCPort: grpcPort,
MembershipPort: membershipPort,
GRPCPort: o.FrontendPort + frontendPortOffset,
MembershipPort: o.FrontendPort + 100 + frontendPortOffset,
BindOnLocalHost: true,
BindOnIP: "",
},
}

// Assign any open port when configured to use dynamic ports
if o.DynamicPorts {
if frontendPortOffset != 0 {
svc.RPC.GRPCPort = o.portProvider.mustGetFreePort()
}
svc.RPC.MembershipPort = o.portProvider.mustGetFreePort()
}

// Optionally bind frontend to IPv4 address
if frontendPortOffset == 0 && o.FrontendIP != "" {
svc.RPC.BindOnLocalHost = false
svc.RPC.BindOnIP = o.FrontendIP
}

return svc
}
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ func WithFrontendPort(port int) ServerOption {
})
}

// WithFrontendIP binds the temporal-frontend GRPC service to a specific IP (eg. `0.0.0.0`)
// Check net.ParseIP for supported syntax; only IPv4 is supported.
//
// When unspecified, the frontend service will bind to localhost.
func WithFrontendIP(address string) ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.FrontendIP = address
})
}

// WithDynamicPorts starts Temporal on system-chosen ports.
func WithDynamicPorts() ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
Expand Down

0 comments on commit 6070027

Please sign in to comment.