Skip to content

Commit

Permalink
Merge pull request #21 from uniondrug/2.x
Browse files Browse the repository at this point in the history
支持--ipaddr和--port参数启动Swoole服务
  • Loading branch information
fuyibing authored Nov 7, 2018
2 parents d493ac3 + 90c24df commit 900d3bc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
10 changes: 6 additions & 4 deletions server
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ try {
new InputOption('container', 'c', InputOption::VALUE_OPTIONAL, 'Docker container', null),
new InputOption('daemon', 'd', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
new InputOption('ipaddr', 'i', InputOption::VALUE_OPTIONAL, 'Server ip address'),
new InputOption('path', 't', InputOption::VALUE_OPTIONAL, 'Web root relative path', $root),
new InputOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Server port'),
new InputOption('path', 'x', InputOption::VALUE_OPTIONAL, 'Web root relative path', $root),
new InputOption('env', 'e', InputOption::VALUE_OPTIONAL, 'Enviroment', 'development'),
new InputOption('help', 'h', InputOption::VALUE_OPTIONAL, 'Show help', null),
]));
Expand All @@ -67,8 +68,9 @@ try {
$output->writeln(sprintf(" <info>%s</info> %s %s", "-c, --container", " ", "运行在Docker容器中, 默认: <comment>no</comment>, 接受: <comment>yes</comment>、<comment>off</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-d, --daemon", " ", "以守护进程运行, 不接受参数"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-e, --env", " ", "环境名称. 默认: <comment>development</comment>, 接受: <comment>development</comment>、<comment>testing</comment>、<comment>release</comment>、<comment>production</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-i, --ipaddr", " ", "Docker容器的IP地址"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-p, --path", " ", "Web相对路径. 默认: <comment>{$root}</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-i, --ipaddr", " ", "IP地址"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-p, --port", " ", "端口号"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-x, --path", " ", "Web相对路径. 默认: <comment>{$root}</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-h, --help", " ", "Show this help"));
$output->writeln("");
$output->writeln("<comment>Available commands:</comment>");
Expand All @@ -83,7 +85,7 @@ try {
$docker = $input->getOption('container');
putenv("APP_ENV=$env");
putenv("DOCKER_CONTAINER={$docker}");
$server = new Server(new Application($path));
$server = new Server(new Application($path), $input);
$server->run($input);
} catch(\Throwable $e) {
$output->writeln('');
Expand Down
66 changes: 36 additions & 30 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
/**
* @author jan huang <bboyjanhuang@gmail.com>
* @copyright 2016
*
* @see https://www.github.com/janhuang
* @see https://fastdlabs.com
*/

namespace Uniondrug\Server;

use swoole_server;
Expand All @@ -22,30 +20,43 @@ class Server
* @var Application
*/
protected $application;

/**
* @var \Uniondrug\Swoole\Server
*/
protected $server;
/**
* @var InputInterface
*/
protected $input;

/**
* Server constructor.
*
* @param Application $application
*/
public function __construct(Application $application)
public function __construct(Application $application, InputInterface $input)
{
$this->application = $application;

$this->input = $input;
/**
* 支持IP参数
* --ipaddr 192.168.10.117
* --port 8080
*/
$host = $application->getConfig()->path('server.host');
// 1. 指定端口号
$port = $input->getOption('port');
if ($port) {
$host = preg_replace("/:\d+/", ":{$port}", $host);
}
// 2. 指定IP地址
$ipaddr = $input->getOption('ipaddr');
if ($ipaddr) {
$host = preg_replace("/\d+\.\d+\.\d+\.\d+/", "{$ipaddr}", $host);
}
// 3. 创建实例
$server = $application->getConfig()->path('server.class', HTTPServer::class);
$this->server = $server::createServer(
$application->getName(),
$application->getConfig()->path('server.host'),
$application->getConfig()->path('server.options')->toArray(),
console()
);
$this->server = $server::createServer($application->getName(), $host, $application->getConfig()->path('server.options')->toArray(), console());
$application->setShared('server', $this->server);

$this->initListeners();
$this->initProcesses();
}
Expand All @@ -71,15 +82,15 @@ public function bootstrap()
*/
public function initListeners()
{
$ipaddr = $this->input->getOption('ipaddr');
$listeners = $this->application->getConfig()->path('server.listeners', []);
foreach ($listeners as $listener) {
$this->server->listen(new $listener['class'](
$this->application->getName() . ' ports',
$listener['host'],
isset($listener['options']) ? $listener['options'] : []
));
// 替换配置文件中的IP地址
if ($ipaddr) {
$listener['host'] = preg_replace("/\d+\.\d+\.\d+\.\d+/", "{$ipaddr}", $listener['host']);
}
$this->server->listen(new $listener['class']($this->application->getName().' ports', $listener['host'], isset($listener['options']) ? $listener['options'] : []));
}

return $this;
}

Expand All @@ -90,9 +101,8 @@ public function initProcesses()
{
$processes = $this->application->getConfig()->path('server.processes', []);
foreach ($processes as $process) {
$this->server->process(new $process($this->application->getName() . ' process'));
$this->server->process(new $process($this->application->getName().' process'));
}

return $this;
}

Expand All @@ -102,7 +112,6 @@ public function initProcesses()
public function daemon()
{
$this->server->daemon();

return $this;
}

Expand All @@ -112,7 +121,6 @@ public function daemon()
public function start()
{
$this->bootstrap();

return $this->server->start();
}

Expand Down Expand Up @@ -150,7 +158,6 @@ public function status()

/**
* @param array $dir
*
* @return int
*/
public function watch(array $dir = ['.'])
Expand All @@ -164,30 +171,29 @@ public function watch(array $dir = ['.'])
public function run(InputInterface $input)
{
// 设置守护进程
if ($input->hasParameterOption(['--daemon', '-d'], true)) {
if ($input->hasParameterOption([
'--daemon',
'-d'
], true)
) {
$this->daemon();
}

switch ($input->getArgument('action')) {
case 'start':
if ($input->hasParameterOption(['--dir'])) {
$this->watch([$input->getOption('dir')]);
} else {
$this->start();
}

break;
case 'stop':
$this->stop();

break;
case 'restart':
$this->restart();

break;
case 'reload':
$this->reload();

break;
case 'status':
default:
Expand Down

0 comments on commit 900d3bc

Please sign in to comment.