Skip to content

Commit 5ae143d

Browse files
committed
feat(server): make external urls configurable
1 parent c9a7543 commit 5ae143d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

internal/server/server.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11+
"os"
1112
"os/exec"
1213

1314
"github.com/go-faster/errors"
@@ -30,12 +31,22 @@ type Server struct {
3031
trace trace.Tracer
3132
}
3233

34+
func (s Server) getEnvDefault(key, def string) string {
35+
v := os.Getenv(key)
36+
if v == "" {
37+
return def
38+
}
39+
return v
40+
}
41+
3342
func (s Server) makeExternalRequest(ctx context.Context) error {
3443
// Make external request.
3544
ctx, span := s.trace.Start(ctx, "Server.makeExternalRequest")
3645
defer span.End()
3746

38-
req, err := http.NewRequestWithContext(ctx, "GET", "https://www.google.com/", http.NoBody)
47+
uri := s.getEnvDefault("EXTERNAL_URL", "https://www.google.com/")
48+
49+
req, err := http.NewRequestWithContext(ctx, "GET", uri, http.NoBody)
3950
if err != nil {
4051
return errors.Wrap(err, "create external request")
4152
}
@@ -73,9 +84,11 @@ func (s Server) makeCurlRequest(ctx context.Context) error {
7384
ctx, span := s.trace.Start(ctx, "Server.makeCurlRequest")
7485
defer span.End()
7586

87+
uri := s.getEnvDefault("CURL_URL", "https://ifconfig.me")
88+
7689
bufErr := new(bytes.Buffer)
7790
buf := new(bytes.Buffer)
78-
cmd := exec.CommandContext(ctx, "curl", "-s", "https://ifconfig.me", "-o", "-", "--max-time", "5")
91+
cmd := exec.CommandContext(ctx, "curl", "-s", uri, "-o", "-", "--max-time", "5")
7992
cmd.Stdout = buf
8093
cmd.Stderr = bufErr
8194

0 commit comments

Comments
 (0)