Skip to content

Commit

Permalink
feat: Set sane defaults for all URIs (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Nov 26, 2024
1 parent 25a0d49 commit bb3f43d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ pnpm start

```sh
export N8N_LAUNCHER_LOG_LEVEL=debug
export N8N_MAIN_URI=... # e.g. http://127.0.0.1:5678
export N8N_TASK_BROKER_URI=... # e.g. http://127.0.0.1:5679
export N8N_RUNNER_URI=... # e.g. http://127.0.0.1:5680
export N8N_RUNNERS_AUTH_TOKEN=... # i.e. same string as in step 4

make run
Expand Down
11 changes: 7 additions & 4 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const (
)

const (
defaultIdleTimeoutValue = "15" // seconds
defaultIdleTimeoutValue = "15" // seconds
DefaultMainServerURI = "http://127.0.0.1:5678"
DefaultTaskBrokerServerURI = "http://127.0.0.1:5679"
DefaultRunnerServerURI = "http://127.0.0.1:5680"
)

// AllowedOnly filters the current environment down to only those
Expand Down Expand Up @@ -143,19 +146,19 @@ func FromEnv() (*EnvConfig, error) {
}

if mainServerURI == "" {
errs = append(errs, fmt.Errorf("%s is required", EnvVarMainServerURI))
mainServerURI = DefaultMainServerURI
} else if err := validateURL(mainServerURI, EnvVarMainServerURI); err != nil {
errs = append(errs, err)
}

if runnerServerURI == "" {
errs = append(errs, fmt.Errorf("%s is required", EnvVarRunnerServerURI))
runnerServerURI = DefaultRunnerServerURI
} else if err := validateURL(runnerServerURI, EnvVarRunnerServerURI); err != nil {
errs = append(errs, err)
}

if taskBrokerServerURI == "" {
errs = append(errs, fmt.Errorf("%s is required", EnvVarTaskBrokerServerURI))
taskBrokerServerURI = DefaultTaskBrokerServerURI
} else if err := validateURL(taskBrokerServerURI, EnvVarTaskBrokerServerURI); err != nil {
errs = append(errs, err)
}
Expand Down
53 changes: 31 additions & 22 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,23 @@ func TestFromEnv(t *testing.T) {
name string
envVars map[string]string
expectError bool
expected *EnvConfig
}{
{
name: "valid configuration",
name: "valid custom configuration",
envVars: map[string]string{
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5680",
EnvVarMainServerURI: "http://localhost:9000",
EnvVarTaskBrokerServerURI: "http://localhost:9001",
EnvVarRunnerServerURI: "http://localhost:9002",
EnvVarIdleTimeout: "30",
},
expectError: false,
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: "http://localhost:9000",
TaskBrokerServerURI: "http://localhost:9001",
RunnerServerURI: "http://localhost:9002",
},
},
{
name: "missing auth token",
Expand Down Expand Up @@ -186,7 +192,12 @@ func TestFromEnv(t *testing.T) {
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5680",
},
expectError: true,
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: DefaultMainServerURI,
TaskBrokerServerURI: "http://localhost:5679",
RunnerServerURI: "http://localhost:5680",
},
},
{
name: "invalid task broker server URI",
Expand All @@ -205,7 +216,12 @@ func TestFromEnv(t *testing.T) {
EnvVarMainServerURI: "http://localhost:5678",
EnvVarRunnerServerURI: "http://localhost:5680",
},
expectError: true,
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: "http://localhost:5678",
TaskBrokerServerURI: DefaultTaskBrokerServerURI,
RunnerServerURI: "http://localhost:5680",
},
},
{
name: "invalid runner server URI",
Expand All @@ -224,7 +240,12 @@ func TestFromEnv(t *testing.T) {
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
},
expectError: true,
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: "http://localhost:5678",
TaskBrokerServerURI: "http://localhost:5679",
RunnerServerURI: DefaultRunnerServerURI,
},
},
{
name: "missing scheme in 127.0.0.1 URI",
Expand Down Expand Up @@ -296,20 +317,8 @@ func TestFromEnv(t *testing.T) {
return
}

if envCfg.AuthToken != tt.envVars[EnvVarAuthToken] {
t.Errorf("FromEnv() AuthToken = %v, want %v", envCfg.AuthToken, tt.envVars[EnvVarAuthToken])
}

if envCfg.MainServerURI != tt.envVars[EnvVarMainServerURI] {
t.Errorf("FromEnv() MainServerURI = %v, want %v", envCfg.MainServerURI, tt.envVars[EnvVarMainServerURI])
}

if envCfg.TaskBrokerServerURI != tt.envVars[EnvVarTaskBrokerServerURI] {
t.Errorf("FromEnv() TaskBrokerServerURI = %v, want %v", envCfg.TaskBrokerServerURI, tt.envVars[EnvVarTaskBrokerServerURI])
}

if envCfg.RunnerServerURI != tt.envVars[EnvVarRunnerServerURI] {
t.Errorf("FromEnv() RunnerServerURI = %v, want %v", envCfg.RunnerServerURI, tt.envVars[EnvVarRunnerServerURI])
if !reflect.DeepEqual(envCfg, tt.expected) {
t.Errorf("FromEnv() = %+v, want %+v", envCfg, tt.expected)
}

if os.Getenv(EnvVarRunnerServerEnabled) != "true" {
Expand Down

0 comments on commit bb3f43d

Please sign in to comment.