Skip to content

Commit 3e8e3ec

Browse files
committed
add support for bind flag
1 parent bc01985 commit 3e8e3ec

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

cli/testdata/cmd_serve.test.txt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
exec relay add --file license.lic --key 9E32DD-D8CC22-771926-C2D834-C506DC-V3 --public-key e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788
33

44
# set a port as environment variable
5+
env BIND=127.0.0.42
56
env PORT=65007
67

78
# start the server in the background with the environment variable
8-
exec relay serve --port $PORT &server_process&
9+
exec relay serve --bind $BIND --port $PORT &server_process_test&
910

1011
# wait for the server to start
1112
exec sleep 1
1213

13-
# claim a license using the port from the environment variable
14-
exec curl -s -X PUT http://localhost:$PORT/v1/nodes/test_fingerprint
14+
# FIXME(ezekg) sh hack because testscript exits on interrupt i.e. a non-zero exit code
15+
# should be unreachable at default bind addr
16+
exec sh -c 'curl -sS -X PUT http://0.0.0.0:65007/v1/nodes/test_fingerprint || true'
17+
stderr 'Failed to connect to 0\.0\.0\.0 port 65007: Connection refused'
1518

16-
# expect license data in the response
19+
# should be reachable at custom bind addr
20+
exec curl -s -X PUT http://$BIND:$PORT/v1/nodes/test_fingerprint
1721
stdout '"license_file":'
1822

19-
# release the license
20-
exec curl -s -X DELETE http://localhost:$PORT/v1/nodes/test_fingerprint
21-
22-
# expect no content in the response
23-
stdout ''
24-
2523
# kill the process (stop the server)
26-
kill server_process
24+
kill server_process_test

internal/server/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func (s *server) Run() error {
3939
ctx, cancel := context.WithCancel(context.Background())
4040
defer cancel()
4141

42-
address := fmt.Sprintf(":%d", s.config.ServerPort)
43-
slog.Info("starting server", "port", s.config.ServerPort)
42+
addr := fmt.Sprintf("%s:%d", s.config.ServerAddr, s.config.ServerPort)
43+
slog.Info("starting server", "addr", s.config.ServerAddr, "port", s.config.ServerPort)
4444

4545
if s.Config().EnabledHeartbeat {
4646
go s.reaper.Start(ctx)
4747
}
4848

49-
if err := http.ListenAndServe(address, s.router); err != nil && !errors.Is(err, http.ErrServerClosed) {
49+
if err := http.ListenAndServe(addr, s.router); err != nil && !errors.Is(err, http.ErrServerClosed) {
5050
slog.Error("server failed to start", "error", err)
5151

5252
cancel()

0 commit comments

Comments
 (0)