Skip to content

Commit 124d726

Browse files
committed
Allow to set the host for the local web server
1 parent 3074bcd commit 124d726

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

commands/local_server_start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ var localServerStartCmd = &console.Command{
6262
&console.BoolFlag{Name: "allow-http", Usage: "Prevent auto-redirection from HTTP to HTTPS"},
6363
&console.StringFlag{Name: "document-root", Usage: "Project document root (auto-configured by default)"},
6464
&console.StringFlag{Name: "passthru", Usage: "Project passthru index (auto-configured by default)"},
65+
&console.StringFlag{Name: "host", Usage: "HTTP host"},
6566
&console.IntFlag{Name: "port", DefaultValue: 8000, Usage: "Preferred HTTP port"},
6667
&console.BoolFlag{Name: "daemon", Aliases: []string{"d"}, Usage: "Run the server in the background"},
6768
&console.BoolFlag{Name: "no-humanize", Usage: "Do not format JSON logs"},

local/http/http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type ServerCallback func(w http.ResponseWriter, r *http.Request, env map[string]
4545
type Server struct {
4646
DocumentRoot string
4747
Callback ServerCallback
48+
Host string
4849
PreferedPort int
4950
PKCS12 string
5051
AllowHTTP bool
@@ -59,7 +60,7 @@ type Server struct {
5960

6061
// Start starts the server
6162
func (s *Server) Start(errChan chan error) (int, error) {
62-
ln, port, err := process.CreateListener(s.PreferedPort)
63+
ln, port, err := process.CreateListener(s.Host, s.PreferedPort)
6364
if err != nil {
6465
return port, errors.WithStack(err)
6566
}

local/process/listener.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,21 @@ import (
2929
// CreateListener creates a listener on a port
3030
// Pass a prefered port (will increment by 1 if port is not available)
3131
// or pass 0 to auto-find any available port
32-
func CreateListener(preferedPort int) (net.Listener, int, error) {
32+
func CreateListener(host string, preferedPort int) (net.Listener, int, error) {
3333
var ln net.Listener
3434
var err error
3535
port := preferedPort
3636
max := 50
37+
defaultHost := host
38+
if defaultHost == "" {
39+
defaultHost = "127.0.0.1"
40+
}
3741
for {
38-
// we really want to test availability on 127.0.0.1
39-
ln, err = net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
40-
if err == nil {
42+
// test availability on the "default" host first
43+
ln, err = net.Listen("tcp", defaultHost+":"+strconv.Itoa(port))
44+
if err == nil && host == "" {
4145
ln.Close()
42-
// but then, we want to listen to as many local IP's as possible
46+
// but then, we want to listen to as many local IP's as possible if no specific host has been given
4347
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
4448
if err == nil {
4549
break

local/project/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Config struct {
3636
ProjectDir string
3737
DocumentRoot string `yaml:"document_root"`
3838
Passthru string `yaml:"passthru"`
39+
Host string `yaml:"host"`
3940
PreferedPort int `yaml:"prefered_port"`
4041
PKCS12 string `yaml:"p12"`
4142
Logger zerolog.Logger
@@ -84,6 +85,9 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
8485
if c.IsSet("passthru") {
8586
config.Passthru = c.String("passthru")
8687
}
88+
if c.IsSet("host") {
89+
config.Host = c.String("host")
90+
}
8791
if c.IsSet("port") {
8892
config.PreferedPort = c.Int("port")
8993
}

0 commit comments

Comments
 (0)