Skip to content

Commit

Permalink
Merge pull request #6 from inhere/master
Browse files Browse the repository at this point in the history
router update, some modify for http server
  • Loading branch information
inhere authored Mar 20, 2018
2 parents 81ab4ce + cedde46 commit c2a7659
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 387 deletions.
Empty file removed master
Empty file.
3 changes: 1 addition & 2 deletions src/Bean/Annotation/RequestMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class RequestMapping
*/
private $method = [RequestMethod::GET, RequestMethod::POST];


/**
* {"id"="\d+"}
* @var array
Expand Down Expand Up @@ -76,7 +75,7 @@ public function __construct(array $values)
*
* @return string
*/
public function getRoute()
public function getRoute(): string
{
return $this->route;
}
Expand Down
36 changes: 36 additions & 0 deletions src/Bootstrap/Listener/MasterStartListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2018/3/19
* Time: 上午11:32
*/

namespace Swoft\Http\Server\Bootstrap\Listener;

use Swoft\App;
use Swoft\Bean\Annotation\ServerListener;
use Swoft\Bootstrap\Listeners\Interfaces\StartInterface;
use Swoft\Bootstrap\SwooleEvent;
use Swoole\Server;

/**
* Class MasterStartListener
* @package Swoft\Http\Server\Bootstrap\Listener
* @ServerListener(event=SwooleEvent::ON_START)
*/
class MasterStartListener implements StartInterface
{
public function onStart(Server $server)
{
\output()->writeln(
'Server has been started. ' .
"(master PID: <cyan>{$server->master_pid}</cyan>, manager PID: <cyan>{$server->manager_pid}</cyan>)"
);

// output a message before start
if (!App::$server->isDaemonize()) {
\output()->writeln('You can use <info>CTRL + C</info> to stop run.');
}
}
}
59 changes: 34 additions & 25 deletions src/Command/ServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class ServerCommand
/**
* Start http server
*
* @Usage server:{command} [arguments] [options]
* @Usage {fullCommand} [-d|--daemon]
* @Options
* -d,--d start by daemonized process
* @Example php swoft.php server:start -d -r
* -d, --daemon Run server on the background
* @Example
* {fullCommand}
* {fullCommand} -d
* @throws \Swoft\Exception\RuntimeException
* @throws \RuntimeException
*/
Expand All @@ -32,7 +34,7 @@ public function start()

// 是否正在运行
if ($httpServer->isRunning()) {
output()->writeln("<error>The server have been running!(PID: {$serverStatus['masterPid']})</error>", true, true);
\output()->writeln("<error>The server have been running!(PID: {$serverStatus['masterPid']})</error>", true, true);
}

// 启动参数
Expand All @@ -54,29 +56,30 @@ public function start()
$tcpHost = $tcpStatus['host'];
$tcpPort = $tcpStatus['port'];
$tcpType = $tcpStatus['type'];
$tcpEnable = $tcpEnable ? 1 : 0;
$tcpEnable = $tcpEnable ? '<note>Enabled</note>' : '<warning>Disabled</warning>';

// 信息面板
$lines = [
' Information Panel ',
'******************************************************************',
"* http | Host: <note>$httpHost</note>, port: <note>$httpPort</note>, Model: <note>$httpMode</note>, type: <note>$httpType</note>, Worker: <note>$workerNum</note>",
"* tcp | Enable: <note>$tcpEnable</note>, host: <note>$tcpHost</note>, port: <note>$tcpPort</note>, type: <note>$tcpType</note>, Worker: <note>$workerNum</note>",
'******************************************************************',
' Server Information ',
'********************************************************************',
"* HTTP | host: <note>$httpHost</note>, port: <note>$httpPort</note>, type: <note>$httpType</note>, worker: <note>$workerNum</note>, mode: <note>$httpMode</note>",
"* TCP | host: <note>$tcpHost</note>, port: <note>$tcpPort</note>, type: <note>$tcpType</note>, worker: <note>$workerNum</note> ($tcpEnable)",
'********************************************************************',
];

// 启动服务器
output()->writeln(implode("\n", $lines));
\output()->writeln(implode("\n", $lines));
$httpServer->start();
}

/**
* Reload worker process
* Reload worker processes
*
* @Usage server:{command} [arguments] [options]
* @Usage {fullCommand} [-t]
* @Options
* -t only to reload task processes, default to reload worker and task
* @Example php swoft.php server:reload
* -t Only to reload task processes, default to reload worker and task
* @Example {fullCommand}
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function reload()
Expand All @@ -97,10 +100,10 @@ public function reload()
}

/**
* Stop http server
* Stop the http server
*
* @Usage server:{command} [arguments] [options]
* @Example php swoft.php server:stop
* @Usage {fullCommand}
* @Example {fullCommand}
* @throws \RuntimeException
*/
public function stop()
Expand All @@ -109,31 +112,36 @@ public function stop()

// 是否已启动
if (!$httpServer->isRunning()) {
output()->writeln('<error>The server is not running! cannot stop</error>', true, true);
\output()->writeln('<error>The server is not running! cannot stop</error>', true, true);
}

// pid文件
$serverStatus = $httpServer->getServerSetting();
$pidFile = $serverStatus['pfile'];

@unlink($pidFile);
output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getScript()));
\output()->writeln(sprintf('<info>Swoft %s is stopping ...</info>', input()->getScript()));

$result = $httpServer->stop();

// 停止失败
if (!$result) {
output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getScript()), true, true);
\output()->writeln(sprintf('<error>Swoft %s stop fail</error>', input()->getScript()), true, true);
}

output()->writeln(sprintf('<success>Swoft %s stop success!</success>', input()->getScript()));
}

/**
* Restart http server
* Restart the http server
*
* @Usage server:{command} [arguments] [options]
* @Example php swoft.php server:restart
* @Usage {fullCommand} [-d|--daemon]
* @Options
* -d, --daemon Run server on the background
* @Example
* {fullCommand}
* {fullCommand} -d
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function restart()
Expand All @@ -152,6 +160,7 @@ public function restart()

/**
* @return HttpServer
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
private function getHttpServer(): HttpServer
Expand All @@ -175,7 +184,7 @@ private function getHttpServer(): HttpServer
*/
private function setStartArgs(HttpServer $httpServer)
{
$daemonize = input()->hasOpt('d');
$daemonize = \input()->getSameOpt(['d', 'daemon'], false);

if ($daemonize) {
$httpServer->setDaemonize();
Expand Down
6 changes: 3 additions & 3 deletions src/Event/HttpServerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class HttpServerEvent
/**
* before request
*/
const BEFORE_REQUEST = "beforeRequest";
const BEFORE_REQUEST = 'beforeRequest';

/**
* after request
*/
const AFTER_REQUEST = "afterRequest";
}
const AFTER_REQUEST = 'afterRequest';
}
7 changes: 4 additions & 3 deletions src/Event/Listeners/ApplicationLoaderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Swoft\Http\Server\Bean\Collector\ControllerCollector;

/**
* the listener of applicatioin loader
* the listener of application loader
*
* @Listener(AppEvent::APPLICATION_LOADER)
* @uses ApplicationLoaderListener
Expand All @@ -22,6 +22,8 @@ class ApplicationLoaderListener implements EventHandlerInterface
{
/**
* @param \Swoft\Event\EventInterface $event
* @throws \LogicException
* @throws \InvalidArgumentException
*/
public function handle(EventInterface $event)
{
Expand All @@ -31,5 +33,4 @@ public function handle(EventInterface $event)
$requestMapping = ControllerCollector::getCollector();
$httpRouter->registerRoutes($requestMapping);
}

}
}
11 changes: 6 additions & 5 deletions src/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public function start()
*
* @throws \Swoft\Exception\RuntimeException
*/
private function registerRpcEvent()
protected function registerRpcEvent()
{
$swooleListeners = SwooleListenerCollector::getCollector();
if (! isset($swooleListeners[SwooleEvent::TYPE_PORT][0]) || empty($swooleListeners[SwooleEvent::TYPE_PORT][0])) {

if (!isset($swooleListeners[SwooleEvent::TYPE_PORT][0]) || empty($swooleListeners[SwooleEvent::TYPE_PORT][0])) {
throw new RuntimeException("Please use swoft/rpc-server, run 'composer require swoft/rpc-server'");
}

Expand All @@ -81,11 +82,11 @@ private function registerRpcEvent()
public function onRequest(Request $request, Response $response)
{
// Initialize Request and Response and set to RequestContent
$request = \Swoft\Http\Message\Server\Request::loadFromSwooleRequest($request);
$response = new \Swoft\Http\Message\Server\Response($response);
$psr7Request = \Swoft\Http\Message\Server\Request::loadFromSwooleRequest($request);
$psr7Response = new \Swoft\Http\Message\Server\Response($response);

/** @var \Swoft\Http\Server\ServerDispatcher $dispatcher */
$dispatcher = App::getBean('serverDispatcher');
$dispatcher->dispatch($request, $response);
$dispatcher->dispatch($psr7Request, $psr7Response);
}
}
Loading

0 comments on commit c2a7659

Please sign in to comment.