diff --git a/internal/artifacts.go b/internal/artifacts.go index c0b6afd..313b40b 100644 --- a/internal/artifacts.go +++ b/internal/artifacts.go @@ -405,12 +405,12 @@ func getPrivKey(privStr string) (*ecdsa.PrivateKey, error) { return priv, nil } -func ConnectRaw(service, port, protocol string) string { - return fmt.Sprintf(`{{Service "%s" "%s" "%s"}}`, service, port, protocol) +func ConnectRaw(service, port, protocol, user string) string { + return fmt.Sprintf(`{{Service "%s" "%s" "%s" "%s"}}`, service, port, protocol, user) } func Connect(service, port string) string { - return ConnectRaw(service, port, "http") + return ConnectRaw(service, port, "http", "") } type output struct { diff --git a/internal/components.go b/internal/components.go index fb6bc1d..3078720 100644 --- a/internal/components.go +++ b/internal/components.go @@ -485,7 +485,7 @@ func (b *BuilderHub) Run(service *Service, ctx *ExContext) { WithImage("docker.io/flashbots/builder-hub"). WithTag("latest"). WithEntrypoint("/app/builder-hub"). - WithEnv("POSTGRES_DSN", "postgres://postgres:postgres@"+ConnectRaw(b.postgres, "postgres", "")+"/postgres?sslmode=disable"). + WithEnv("POSTGRES_DSN", ConnectRaw(b.postgres, "postgres", "postgres", "postgres:postgres")+"/postgres?sslmode=disable"). WithEnv("LISTEN_ADDR", "0.0.0.0:"+`{{Port "http" 8080}}`). WithEnv("ADMIN_ADDR", "0.0.0.0:"+`{{Port "admin" 8081}}`). WithEnv("INTERNAL_ADDR", "0.0.0.0:"+`{{Port "internal" 8082}}`). diff --git a/internal/local_runner.go b/internal/local_runner.go index 74adaa9..af0ebb3 100644 --- a/internal/local_runner.go +++ b/internal/local_runner.go @@ -477,12 +477,7 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er } funcs := template.FuncMap{ - "Service": func(name string, portLabel, protocol string) string { - protocolPrefix := "" - if protocol == "http" { - protocolPrefix = "http://" - } - + "Service": func(name string, portLabel, protocol, user string) string { // For {{Service "name" "portLabel"}}: // - Service runs on host: // A: target is inside docker: access with localhost:hostPort @@ -497,14 +492,14 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er if d.isHostService(s.Name) { // A and B - return fmt.Sprintf("%slocalhost:%d", protocolPrefix, port.HostPort) + return printAddr(protocol, "localhost", port.HostPort, user) } else { if d.isHostService(svc.Name) { // D - return fmt.Sprintf("%shost.docker.internal:%d", protocolPrefix, port.HostPort) + return printAddr(protocol, "host.docker.internal", port.HostPort, user) } // C - return fmt.Sprintf("%s%s:%d", protocolPrefix, svc.Name, port.Port) + return printAddr(protocol, svc.Name, port.Port, user) } }, "Port": func(name string, defaultPort int) int { @@ -552,6 +547,19 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er return argsResult, envs, nil } +func printAddr(protocol, serviceName string, port int, user string) string { + var protocolPrefix string + if protocol != "" { + protocolPrefix = protocol + "://" + } + + if user != "" { + return fmt.Sprintf("%s%s@%s:%s", protocolPrefix, user, serviceName, serviceName) + } + + return fmt.Sprintf("%s%s:%d", protocolPrefix, serviceName, port) +} + func (d *LocalRunner) validateImageExists(image string) error { // check locally _, err := d.client.ImageInspect(context.Background(), image) @@ -570,7 +578,7 @@ func (d *LocalRunner) validateImageExists(image string) error { return err } - return fmt.Errorf("image %s not found: %w", image) + return fmt.Errorf("image %s not found", image) } func (d *LocalRunner) toDockerComposeService(s *Service) (map[string]interface{}, error) { diff --git a/internal/manifest.go b/internal/manifest.go index 3bd05e3..3541173 100644 --- a/internal/manifest.go +++ b/internal/manifest.go @@ -206,6 +206,8 @@ type Port struct { type NodeRef struct { Service string PortLabel string + Protocol string + User string } // serviceLogs is a service to access the logs of the running service @@ -443,7 +445,7 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) { // ther can be multiple port and nodere because in the case of op-geth we pass a whole string as nested command args funcs := template.FuncMap{ - "Service": func(name string, portLabel, protocol string) string { + "Service": func(name string, portLabel, protocol, user string) string { if name == "" { panic("BUG: service name cannot be empty") } @@ -455,8 +457,8 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) { // here we only keep the references to the services to be checked if they are valid and an be resolved // later on for the runtime we will do the resolve stage. // TODO: this will get easier when we move away from templates and use interface and structs. - nodeRef = append(nodeRef, NodeRef{Service: name, PortLabel: portLabel}) - return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel) + nodeRef = append(nodeRef, NodeRef{Service: name, PortLabel: portLabel, Protocol: protocol, User: user}) + return fmt.Sprintf(`{{Service "%s" "%s" "%s" "%s"}}`, name, portLabel, protocol, user) }, "Port": func(name string, defaultPort int) string { portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolTCP}) diff --git a/internal/manifest_test.go b/internal/manifest_test.go new file mode 100644 index 0000000..b5223f8 --- /dev/null +++ b/internal/manifest_test.go @@ -0,0 +1,58 @@ +package internal + +import ( + "testing" +) + +func TestNodeRefString(t *testing.T) { + var testCases = []struct { + protocol string + service string + port int + user string + expected string + }{ + { + protocol: "", + service: "test", + port: 80, + user: "", + expected: "test:80", + }, + { + protocol: "", + service: "test", + port: 80, + user: "test", + expected: "test@test:test", + }, + { + protocol: "http", + service: "test", + port: 80, + user: "", + expected: "http://test:80", + }, + { + protocol: "http", + service: "test", + port: 80, + user: "test", + expected: "http://test@test:test", + }, + { + protocol: "enode", + service: "test", + port: 80, + user: "", + expected: "enode://test:80", + }, + } + + for _, testCase := range testCases { + result := printAddr(testCase.protocol, testCase.service, testCase.port, testCase.user) + if result != testCase.expected { + t.Errorf("expected %s, got %s", testCase.expected, result) + } + } +}