Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 14a24b6

Browse files
authoredJun 2, 2020
Merge pull request #14 from cybercog/try-to-start-on-next-free-port
Start server on the next unoccupied port
2 parents b22fb76 + 63de67b commit 14a24b6

File tree

4 files changed

+84
-34
lines changed

4 files changed

+84
-34
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cache:
1515
- $HOME/.composer/cache
1616

1717
script:
18-
- ./hyper-run -S localhost:8080 -s localhost:8081 -n 5 -t examples &
18+
- ./hyper-run -S localhost:8000 -s localhost:44300 -n 5 -t examples &
1919
- vendor/bin/codecept run unit
2020
- kill $!
2121

‎README.md

+32-14
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,45 @@ Use **`vendor/bin/hyper-run`** as the execution path.
2727

2828
## Usage
2929

30+
### Quick start
31+
32+
```shell script
33+
hyper-run -S localhost -s localhost -t src/app/www
34+
```
35+
36+
2 servers will start with the directory `src/app/www` as the document root:
37+
38+
- `http://localhost:8000`
39+
- `https://localhost:44300`
40+
41+
Servers start with first unoccupied port within range depending on a scheme.
42+
43+
| Scheme | Default | Range |
44+
| ------- | ------- | ----------- |
45+
| `HTTP` | 8000 | 8000-8099 |
46+
| `HTTPS` | 44300 | 44300-44399 |
47+
48+
### Customize ports
49+
50+
```shell script
51+
hyper-run -S localhost:8080 -s localhost:4000 -t src/app/www
52+
```
53+
54+
2 servers will start with the directory `src/app/www` as the document root:
55+
56+
- `http://localhost:8080`
57+
- `https://localhost:4000`
58+
59+
### Command Reference
60+
3061
```ShellSession
3162
mpyw@localhost:~$ hyper-run -h
3263

3364
Usage:
3465
hyper-run <options>
3566

3667
Example:
37-
hyper-run -S localhost:8080 -s localhost:8081
68+
hyper-run -S localhost:8000 -s localhost:44300
3869

3970
[Required]
4071
-S "<Host>:<Port>" of an HTTP server. Multiple arguments can be accepted.
@@ -54,19 +85,6 @@ Restrictions:
5485
mpyw@localhost:~$
5586
```
5687

57-
## Example
58-
59-
```shell script
60-
hyper-run -S localhost:8080 -s localhost:8081 -t src/app/www
61-
```
62-
63-
It listens on
64-
65-
- `http://localhost:8080`
66-
- `https://localhost:8081`
67-
68-
using the directory `src/app/www` as the document root.
69-
7088
## Note for Windows users
7189

7290
Unfortunately, `cmd.exe` has no option to run via shebang `#!/usr/bin/env php`, so you need to create the following batch file in the proper directory.

‎hyper-run

+41-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Usage:
3030
$_SERVER[SCRIPT_NAME] <options>
3131
3232
Example:
33-
$_SERVER[SCRIPT_NAME] -S localhost:8080 -s localhost:8081
33+
$_SERVER[SCRIPT_NAME] -S localhost:8000 -s localhost:44300
3434
3535
[Required]
3636
-S \"<Host>:<Port>\" of an HTTP server. Multiple arguments can be accepted.
@@ -72,6 +72,35 @@ if (!$servers && !$secures) {
7272
exit(1);
7373
}
7474

75+
function startListener($master, $listener)
76+
{
77+
$host = sprintf(
78+
'%s://%s:%s',
79+
$listener[2] ? 'https' : 'http', $listener[0], $listener[1]
80+
);
81+
fwrite(STDOUT, "Development server started: <{$host}>\n");
82+
83+
try {
84+
call_user_func_array([$master, 'addListener'], $listener);
85+
} catch (React\Socket\ConnectionException $exception) {
86+
$reason = 'Address already in use';
87+
88+
$isPortSpecified = $listener[4];
89+
90+
if ($isPortSpecified || strpos($exception->getMessage(), $reason) === false) {
91+
throw $exception;
92+
}
93+
94+
fwrite(STDERR, sprintf(
95+
"Failed to listen on %s:%s (reason: %s)\n",
96+
$listener[0], $listener[1], $reason
97+
));
98+
99+
$listener[1] = (int) $listener[1] + 1;
100+
startListener($master, $listener);
101+
}
102+
}
103+
75104
try {
76105

77106
if (!ctype_digit($number) || $number < 1 || $number > 20) {
@@ -92,8 +121,17 @@ try {
92121

93122
$listeners = [];
94123
foreach ([$servers, $secures] as $type => $group) {
124+
$isSecure = $type === 1;
95125
foreach ($group as $i => $server) {
96126
list($host, $port) = explode(':', $server, 2) + [1 => ''];
127+
128+
if ($port === '') {
129+
$port = $isSecure ? '44300' : '8000';
130+
$isPortSpecified = false;
131+
} else {
132+
$isPortSpecified = true;
133+
}
134+
97135
$ip = filter_var(gethostbyname($host), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
98136
$regex = '/\A(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\z/';
99137
if ($ip === false || !preg_match($regex, $port)) {
@@ -102,7 +140,7 @@ try {
102140
if (isset($listeners[$server])) {
103141
throw new \RuntimeException("Duplicated entry: $server");
104142
}
105-
$listeners[$server] = [$host, $port, $type === 1, $cert];
143+
$listeners[$server] = [$host, $port, $isSecure, $cert, $isPortSpecified];
106144
}
107145
}
108146

@@ -116,13 +154,7 @@ try {
116154
$usedProcesses = $processes;
117155
$master = new mpyw\HyperBuiltinServer\Master($loop, $processes);
118156
foreach ($listeners as $listener) {
119-
call_user_func_array([$master, 'addListener'], $listener);
120-
121-
$host = sprintf(
122-
'%s://%s:%s',
123-
$listener[2] ? 'https' : 'http', $listener[0], $listener[1]
124-
);
125-
fwrite(STDOUT, "Development server started: <{$host}>\n");
157+
startListener($master, $listener);
126158
}
127159
})
128160
->then(null, function ($e) use (&$usedProcesses) {

‎tests/unit/RemoteSimpleTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -53,63 +53,63 @@ private function curlsAssert200OK(array $curls)
5353

5454
public function testSimple()
5555
{
56-
Co::wait($ch = $this->curlInitWith('http://localhost:8080/upload_form.php'));
56+
Co::wait($ch = $this->curlInitWith('http://localhost:8000/upload_form.php'));
5757
$this->curlAssert200OK($ch);
5858
}
5959

6060
public function testSimpleSecure()
6161
{
62-
Co::wait($ch = $this->curlInitWith('https://localhost:8081/upload_form.php'));
62+
Co::wait($ch = $this->curlInitWith('https://localhost:44300/upload_form.php'));
6363
$this->curlAssert200OK($ch);
6464
}
6565

6666
public function testDelayed()
6767
{
68-
Co::wait($ch = $this->curlInitWith('http://localhost:8080/fast_hello.php'));
68+
Co::wait($ch = $this->curlInitWith('http://localhost:8000/fast_hello.php'));
6969
$this->curlAssert200OK($ch);
7070
}
7171

7272
public function testDelayedSecure()
7373
{
74-
Co::wait($ch = $this->curlInitWith('https://localhost:8081/fast_hello.php'));
74+
Co::wait($ch = $this->curlInitWith('https://localhost:44300/fast_hello.php'));
7575
$this->curlAssert200OK($ch);
7676
}
7777

7878
public function testDelayedGroupSingle()
7979
{
80-
Co::wait($chs = $this->curlsInitWith(5, 'http://localhost:8080/fast_hello.php'));
80+
Co::wait($chs = $this->curlsInitWith(5, 'http://localhost:8000/fast_hello.php'));
8181
$this->curlsAssert200OK($chs);
8282
}
8383

8484
public function testDelayedGroupSingleSecure()
8585
{
86-
Co::wait($chs = $this->curlsInitWith(5, 'https://localhost:8081/fast_hello.php'));
86+
Co::wait($chs = $this->curlsInitWith(5, 'https://localhost:44300/fast_hello.php'));
8787
$this->curlsAssert200OK($chs);
8888
}
8989

9090
public function testDelayedGroupDouble()
9191
{
92-
Co::wait($chs = $this->curlsInitWith(10, 'http://localhost:8080/fast_hello.php'));
92+
Co::wait($chs = $this->curlsInitWith(10, 'http://localhost:8000/fast_hello.php'));
9393
$this->curlsAssert200OK($chs);
9494
}
9595

9696
public function testDelayedGroupDoubleSecure()
9797
{
98-
Co::wait($chs = $this->curlsInitWith(10, 'https://localhost:8081/fast_hello.php'));
98+
Co::wait($chs = $this->curlsInitWith(10, 'https://localhost:44300/fast_hello.php'));
9999
$this->curlsAssert200OK($chs);
100100
}
101101

102102
public function testDelayedGroupDoubleAtOnce()
103103
{
104-
Co::wait($chs = $this->curlsInitWith(10, 'http://localhost:8080/fast_hello.php'), [
104+
Co::wait($chs = $this->curlsInitWith(10, 'http://localhost:8000/fast_hello.php'), [
105105
'concurrency' => 0,
106106
]);
107107
$this->curlsAssert200OK($chs);
108108
}
109109

110110
public function testDelayedGroupDoubleAtOnceSecure()
111111
{
112-
Co::wait($chs = $this->curlsInitWith(10, 'https://localhost:8081/fast_hello.php'), [
112+
Co::wait($chs = $this->curlsInitWith(10, 'https://localhost:44300/fast_hello.php'), [
113113
'concurrency' => 0,
114114
]);
115115
$this->curlsAssert200OK($chs);

0 commit comments

Comments
 (0)
This repository has been archived.