Skip to content

Commit

Permalink
fix: infer nats server name FLY_ALLOC_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Nov 2, 2023
1 parent 3f65996 commit 4b8f559
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@ func embeddedNatsCLIFlags(c *config.Config, routes *string, gateways *string) []
Destination: &c.EmbeddedNats.StoreDir,
},

&cli.StringFlag{
Name: "enats_server_name",
Usage: "Embedded NATS unique server name (required for JetStream), auto-generated by default",
Value: c.EmbeddedNats.Name,
Destination: &c.EmbeddedNats.Name,
},

&cli.BoolFlag{
Name: "enats_debug",
Usage: "Enable NATS server logs",
Expand Down
6 changes: 6 additions & 0 deletions config/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ func (c *Config) loadFlyPreset(defaults *Config) error {
return errors.New("FLY_APP_NAME env is missing")
}

appId, _ := os.LookupEnv("FLY_ALLOC_ID")

redisEnabled := (c.Redis.URL != defaults.Redis.URL)

// Use the same port for HTTP broadcasts by default
if c.HTTPBroadcast.Port == defaults.HTTPBroadcast.Port {
c.HTTPBroadcast.Port = c.Port
}

if c.EmbeddedNats.Name == defaults.EmbeddedNats.Name && appId != "" {
c.EmbeddedNats.Name = fmt.Sprintf("fly-%s-%s", region, appId)
}

if c.EmbeddedNats.ServiceAddr == defaults.EmbeddedNats.ServiceAddr {
c.EmbeddedNats.ServiceAddr = "nats://0.0.0.0:4222"
}
Expand Down
1 change: 1 addition & 0 deletions config/presets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestFlyPresets(t *testing.T) {
assert.Equal(t, true, config.EmbedNats)
assert.Equal(t, "nats", config.PubSubAdapter)
assert.Equal(t, "", config.BrokerAdapter)
assert.Equal(t, "fly-mag-1234", config.EmbeddedNats.Name)
assert.Equal(t, "nats://0.0.0.0:4222", config.EmbeddedNats.ServiceAddr)
assert.Equal(t, "nats://0.0.0.0:5222", config.EmbeddedNats.ClusterAddr)
assert.Equal(t, "any-test-mag-cluster", config.EmbeddedNats.ClusterName)
Expand Down

0 comments on commit 4b8f559

Please sign in to comment.