8000 Merge pull request #103 from ruudk/fix-port · symfony-cli/symfony-cli@01c919f · GitHub
[go: up one dir, main page]

Skip to content

Commit 01c919f

Browse files
authored
Merge pull request #103 from ruudk/fix-port
Start server with fixed port when `--port` argument is provided
2 parents d96ec9f + 3bf0894 commit 01c919f

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

local/http/http.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ type ServerCallback func(w http.ResponseWriter, r *http.Request, env map[string]
4343

4444
// Server represents a server
4545
type Server struct {
46-
DocumentRoot string
47-
Callback ServerCallback
48-
PreferedPort int
49-
PKCS12 string
50-
AllowHTTP bool
51-
Logger zerolog.Logger
52-
Appversion string
46+
DocumentRoot string
47+
Callback ServerCallback
48+
Port int
49+
PreferredPort int
50+
PKCS12 string
51+
AllowHTTP bool
52+
Logger zerolog.Logger
53+
Appversion string
5354

5455
httpserver *http.Server
5556
httpsserver *http.Server
@@ -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.Port, s.PreferredPort)
6364
if err != nil {
6465
return port, errors.WithStack(err)
6566
}

local/process/listener.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,42 @@ import (
2727
)
2828

2929
// CreateListener creates a listener on a port
30-
// Pass a prefered port (will increment by 1 if port is not available)
30+
// Pass a preferred 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(port, preferredPort int) (net.Listener, int, error) {
3333
var ln net.Listener
3434
var err error
35-
port := preferedPort
35+
tryPort := preferredPort
3636
max := 50
37+
if port > 0 {
38+
tryPort = port
39+
max = 1
40+
}
3741
for {
3842
// we really want to test availability on 127.0.0.1
39-
ln, err = net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
43+
ln, err = net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(tryPort))
4044
if err == nil {
4145
ln.Close()
4246
// but then, we want to listen to as many local IP's as possible
43-
ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
47+
ln, err = net.Listen("tcp", ":"+strconv.Itoa(tryPort))
4448
if err == nil {
4549
break
4650
}
4751
}
48-
if preferedPort == 0 {
52+
if port > 0 {
53+
return nil, 0, errors.Wrapf(err, "unable to listen on port %d", port)
54+
}
55+
if preferredPort == 0 {
4956
return nil, 0, errors.Wrap(err, "unable to find an available port")
5057
}
5158
max--
5259
if max == 0 {
53-
return nil, 0, errors.Wrapf(err, "unable to find an available port (from %d to %d)", preferedPort, port)
60+
return nil, 0, errors.Wrapf(err, "unable to find an available port (from %d to %d)", preferredPort, tryPort)
5461
}
55-
port++
62+
tryPort++
5663
}
57-
if preferedPort == 0 {
58-
port = ln.Addr().(*net.TCPAddr).Port
64+
if port == 0 && preferredPort == 0 {
65+
tryPort = ln.Addr().(*net.TCPAddr).Port
5966
}
60-
return ln, port, nil
67+
return ln, tryPort, nil
6168
}

local/project/config.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ import (
3232

3333
// Config is the struct taken by New (should not be used for anything else)
3434
type Config struct {
35-
HomeDir string
36-
ProjectDir string
37-
DocumentRoot string `yaml:"document_root"`
38-
Passthru string `yaml:"passthru"`
39-
PreferedPort int `yaml:"prefered_port"`
40-
PKCS12 string `yaml:"p12"`
41-
Logger zerolog.Logger
42-
AppVersion string
43-
AllowHTTP bool `yaml:"allow_http"`
44-
NoTLS bool `yaml:"no_tls"`
45-
Daemon bool `yaml:"daemon"`
35+
HomeDir string
36+
ProjectDir string
37+
DocumentRoot string B41A `yaml:"document_root"`
38+
Passthru string `yaml:"passthru"`
39+
Port int `yaml:"port"`
40+
PreferredPort int `yaml:"preferred_port"`
41+
PKCS12 string `yaml:"p12"`
42+
Logger zerolog.Logger
43+
AppVersion string
44+
AllowHTTP bool `yaml:"allow_http"`
45+
NoTLS bool `yaml:"no_tls"`
46+
Daemon bool `yaml:"daemon"`
4647
}
4748

4849
type FileConfig struct {
@@ -85,10 +86,10 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
8586
config.Passthru = c.String("passthru")
8687
}
8788
if c.IsSet("port") {
88-
config.PreferedPort = c.Int("port")
89+
config.Port = c.Int("port")
8990
}
90-
if config.PreferedPort == 0 {
91-
config.PreferedPort = 8000
91+
if config.Port == 0 {
92+
config.PreferredPort = 8000
9293
}
9394
if c.IsSet("allow-http") {
9495
config.AllowHTTP = c.Bool("allow-http")

local/project/project.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ func New(c *Config) (*Project, error) {
5454
homeDir: c.HomeDir,
5555
projectDir: c.ProjectDir,
5656
HTTP: &lhttp.Server{
57-
DocumentRoot: documentRoot,
58-
PreferedPort: c.PreferedPort,
59-
Logger: c.Logger,
60-
PKCS12: c.PKCS12,
61-
AllowHTTP: c.AllowHTTP,
62-
Appversion: c.AppVersion,
57+
DocumentRoot: documentRoot,
58+
Port: c.Port,
59+
PreferredPort: c.PreferredPort,
60+
Logger: c.Logger,
61+
PKCS12: c.PKCS12,
62+
AllowHTTP: c.AllowHTTP,
63+
Appversion: c.AppVersion,
6364
},
6465
}
6566
if err != nil {

0 commit comments

Comments
 (0)
0