Skip to content

Commit

Permalink
Server requires an explicit host
Browse files Browse the repository at this point in the history
Using localhost as the host locally prevents annoying Mac firewall popups
  • Loading branch information
tygern committed Mar 22, 2024
1 parent 75d36bc commit 003f432
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

func main() {
host := websupport.EnvironmentVariable("HOST", "")
port := websupport.IntegerEnvironmentVariable("PORT", 8777)

server := websupport.NewServer(app.Handlers(true))

_, done := server.Start(port)
_, done := server.Start(host, port)
log.Fatal(<-done)
}
2 changes: 1 addition & 1 deletion internal/app/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestHealth(t *testing.T) {
server := websupport.NewServer(app.Handlers(false))
_, _ = server.Start(0)
_, _ = server.Start("localhost", 0)
err := server.WaitUntilHealthy("/health")
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion pkg/testsupport/test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func StartTestServer(t *testing.T, handlers websupport.Handlers) (string, *websupport.Server) {
server := websupport.NewServer(handlers)

port, _ := server.Start(0)
port, _ := server.Start("localhost", 0)
err := server.WaitUntilHealthy("/health")
if err != nil {
t.Errorf("unable to start server: %s", err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/websupport/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import (
"strconv"
)

func EnvironmentVariable(variableName string, defaultValue string) string {
value, found := os.LookupEnv(variableName)
if found {
return value
} else {
return defaultValue
}
}

func IntegerEnvironmentVariable(variableName string, defaultValue int) int {
value, err := strconv.Atoi(os.Getenv(variableName))
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/websupport/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewServer(handlers Handlers) *Server {
}
}

func (s *Server) Start(port int) (listenerPort int, done chan error) {
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
func (s *Server) Start(host string, port int) (listenerPort int, done chan error) {
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
done <- err
return 0, done
Expand All @@ -39,7 +39,7 @@ func (s *Server) Start(port int) (listenerPort int, done chan error) {
}()

listenerPort = listen.Addr().(*net.TCPAddr).Port
slog.Info("Server started", "address", fmt.Sprintf("http://localhost:%d", listenerPort))
slog.Info("Server started", "address", fmt.Sprintf("http://%s:%d", host, listenerPort))
s.port = listenerPort
return listenerPort, done
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/websupport/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCreate(t *testing.T) {
})
})

port, _ := server.Start(0)
port, _ := server.Start("localhost", 0)
err := server.WaitUntilHealthy("/")
assert.NoError(t, err)
defer func(server *websupport.Server) {
Expand Down

0 comments on commit 003f432

Please sign in to comment.