Skip to content

Commit 0db74a5

Browse files
authored
Use custom protocol and user for Connections (#141)
1 parent 463e8c8 commit 0db74a5

File tree

5 files changed

+85
-17
lines changed

5 files changed

+85
-17
lines changed

internal/artifacts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ func getPrivKey(privStr string) (*ecdsa.PrivateKey, error) {
405405
return priv, nil
406406
}
407407

408-
func ConnectRaw(service, port, protocol string) string {
409-
return fmt.Sprintf(`{{Service "%s" "%s" "%s"}}`, service, port, protocol)
408+
func ConnectRaw(service, port, protocol, user string) string {
409+
return fmt.Sprintf(`{{Service "%s" "%s" "%s" "%s"}}`, service, port, protocol, user)
410410
}
411411

412412
func Connect(service, port string) string {
413-
return ConnectRaw(service, port, "http")
413+
return ConnectRaw(service, port, "http", "")
414414
}
415415

416416
type output struct {

internal/components.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ func (b *BuilderHub) Run(service *Service, ctx *ExContext) {
485485
WithImage("docker.io/flashbots/builder-hub").
486486
WithTag("latest").
487487
WithEntrypoint("/app/builder-hub").
488-
WithEnv("POSTGRES_DSN", "postgres://postgres:postgres@"+ConnectRaw(b.postgres, "postgres", "")+"/postgres?sslmode=disable").
488+
WithEnv("POSTGRES_DSN", ConnectRaw(b.postgres, "postgres", "postgres", "postgres:postgres")+"/postgres?sslmode=disable").
489489
WithEnv("LISTEN_ADDR", "0.0.0.0:"+`{{Port "http" 8080}}`).
490490
WithEnv("ADMIN_ADDR", "0.0.0.0:"+`{{Port "admin" 8081}}`).
491491
WithEnv("INTERNAL_ADDR", "0.0.0.0:"+`{{Port "internal" 8082}}`).

internal/local_runner.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,7 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er
477477
}
478478

479479
funcs := template.FuncMap{
480-
"Service": func(name string, portLabel, protocol string) string {
481-
protocolPrefix := ""
482-
if protocol == "http" {
483-
protocolPrefix = "http://"
484-
}
485-
480+
"Service": func(name string, portLabel, protocol, user string) string {
486481
// For {{Service "name" "portLabel"}}:
487482
// - Service runs on host:
488483
// A: target is inside docker: access with localhost:hostPort
@@ -497,14 +492,14 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er
497492

498493
if d.isHostService(s.Name) {
499494
// A and B
500-
return fmt.Sprintf("%slocalhost:%d", protocolPrefix, port.HostPort)
495+
return printAddr(protocol, "localhost", port.HostPort, user)
501496
} else {
502497
if d.isHostService(svc.Name) {
503498
// D
504-
return fmt.Sprintf("%shost.docker.internal:%d", protocolPrefix, port.HostPort)
499+
return printAddr(protocol, "host.docker.internal", port.HostPort, user)
505500
}
506501
// C
507-
return fmt.Sprintf("%s%s:%d", protocolPrefix, svc.Name, port.Port)
502+
return printAddr(protocol, svc.Name, port.Port, user)
508503
}
509504
},
510505
"Port": func(name string, defaultPort int) int {
@@ -552,6 +547,19 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er
552547
return argsResult, envs, nil
553548
}
554549

550+
func printAddr(protocol, serviceName string, port int, user string) string {
551+
var protocolPrefix string
552+
if protocol != "" {
553+
protocolPrefix = protocol + "://"
554+
}
555+
556+
if user != "" {
557+
return fmt.Sprintf("%s%s@%s:%s", protocolPrefix, user, serviceName, serviceName)
558+
}
559+
560+
return fmt.Sprintf("%s%s:%d", protocolPrefix, serviceName, port)
561+
}
562+
555563
func (d *LocalRunner) validateImageExists(image string) error {
556564
// check locally
557565
_, err := d.client.ImageInspect(context.Background(), image)
@@ -570,7 +578,7 @@ func (d *LocalRunner) validateImageExists(image string) error {
570578
return err
571579
}
572580

573-
return fmt.Errorf("image %s not found: %w", image)
581+
return fmt.Errorf("image %s not found", image)
574582
}
575583

576584
func (d *LocalRunner) toDockerComposeService(s *Service) (map[string]interface{}, error) {

internal/manifest.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ type Port struct {
206206
type NodeRef struct {
207207
Service string
208208
PortLabel string
209+
Protocol string
210+
User string
209211
}
210212

211213
// serviceLogs is a service to access the logs of the running service
@@ -443,7 +445,7 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
443445
// ther can be multiple port and nodere because in the case of op-geth we pass a whole string as nested command args
444446

445447
funcs := template.FuncMap{
446-
"Service": func(name string, portLabel, protocol string) string {
448+
"Service": func(name string, portLabel, protocol, user string) string {
447449
if name == "" {
448450
panic("BUG: service name cannot be empty")
449451
}
@@ -455,8 +457,8 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
455457
// here we only keep the references to the services to be checked if they are valid and an be resolved
456458
// later on for the runtime we will do the resolve stage.
457459
// TODO: this will get easier when we move away from templates and use interface and structs.
458-
nodeRef = append(nodeRef, NodeRef{Service: name, PortLabel: portLabel})
459-
return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel)
460+
nodeRef = append(nodeRef, NodeRef{Service: name, PortLabel: portLabel, Protocol: protocol, User: user})
461+
return fmt.Sprintf(`{{Service "%s" "%s" "%s" "%s"}}`, name, portLabel, protocol, user)
460462
},
461463
"Port": func(name string, defaultPort int) string {
462464
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolTCP})

internal/manifest_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package internal
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNodeRefString(t *testing.T) {
8+
var testCases = []struct {
9+
protocol string
10+
service string
11+
port int
12+
user string
13+
expected string
14+
}{
15+
{
16+
protocol: "",
17+
service: "test",
18+
port: 80,
19+
user: "",
20+
expected: "test:80",
21+
},
22+
{
23+
protocol: "",
24+
service: "test",
25+
port: 80,
26+
user: "test",
27+
expected: "test@test:test",
28+
},
29+
{
30+
protocol: "http",
31+
service: "test",
32+
port: 80,
33+
user: "",
34+
expected: "http://test:80",
35+
},
36+
{
37+
protocol: "http",
38+
service: "test",
39+
port: 80,
40+
user: "test",
41+
expected: "http://test@test:test",
42+
},
43+
{
44+
protocol: "enode",
45+
service: "test",
46+
port: 80,
47+
user: "",
48+
expected: "enode://test:80",
49+
},
50+
}
51+
52+
for _, testCase := range testCases {
53+
result := printAddr(testCase.protocol, testCase.service, testCase.port, testCase.user)
54+
if result != testCase.expected {
55+
t.Errorf("expected %s, got %s", testCase.expected, result)
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)