Skip to content

Commit

Permalink
新增获取应用请求地址方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Oct 10, 2023
1 parent 04d3676 commit f6c7d50
Show file tree
Hide file tree
Showing 29 changed files with 198 additions and 13 deletions.
24 changes: 24 additions & 0 deletions dev/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Imi\Event\Event;
use Imi\Util\Uri;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use SebastianBergmann\CodeCoverage\CodeCoverage;
Expand All @@ -12,6 +13,29 @@

ini_set('date.timezone', 'Asia/Shanghai');

const TEST_APP_URI_CONFIG = [
'host' => 'imi-test',
'port' => 1234,
'scheme' => 'https',
'user' => 'root',
'pass' => '123',
'path' => '/test',
'query' => 'id=666',
'fragment' => 'test',
];
const TEST_APP_URI = 'https://root:123@imi-test:1234/test?id=666#test';
function testAppCallbackUriConfig(Uri $uri)
{
return $uri->withHost('imi-test-callback')
->withPort(6666)
->withScheme('https')
->withUserInfo('admin', '123456')
->withPath('/testCallback')
->withQuery('id=999')
->withFragment('testCallback');
}
const TEST_APP_CALLBACK_URI = 'https://admin:123456@imi-test-callback:6666/testCallback?id=999#testCallback';

function getRectorConfigCallback(string $path): callable
{
// @phpstan-ignore-next-line
Expand Down
8 changes: 8 additions & 0 deletions dev/generate-request-context-proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ __DIR__=$(cd `dirname $0`; pwd)

cd $__DIR__/../

rm -f src/Server/Http/Message/Proxy/RequestProxy.php \
&& rm -f src/Server/Http/Message/Proxy/RequestProxyObject.php \
&& rm -f src/Server/Http/Message/Proxy/ResponseProxy.php \
&& rm -f src/Server/Http/Message/Proxy/ResponseProxyObject.php \
&& rm -f src/Server/TcpServer/Message/Proxy/ReceiveDataProxy.php \
&& rm -f src/Server/UdpServer/Message/Proxy/PacketDataProxy.php \
&& rm -f src/Server/WebSocket/Message/Proxy/FrameProxy.php

src/Cli/bin/imi-cli --app-namespace "Imi" generate/requestContextProxy --target "Imi\Server\Http\Message\Proxy\RequestProxy" --class "Imi\Server\Http\Message\Contract\IHttpRequest" --name request && \

src/Cli/bin/imi-cli --app-namespace "Imi" generate/requestContextProxy --target "Imi\Server\Http\Message\Proxy\RequestProxyObject" --class "Imi\Server\Http\Message\Contract\IHttpRequest" --name request --bean HttpRequestProxy --interface "Imi\Server\Http\Message\Contract\IHttpRequest" --recursion=false && \
Expand Down
1 change: 1 addition & 0 deletions doc/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
## 多容器

* [PHP-FPM](container/php-fpm.md)
* [服务器配置](container/php-fpm/serverConfig.md)
* [Swoole](container/swoole.md)
* [Swoole 环境安装教程](base/env.md)
* [子服务器(单项目多端口多协议)](core/subServer.md)
Expand Down
12 changes: 12 additions & 0 deletions doc/components/httpserver/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ request 数据包含 get/post/cookie
> 可在`nginx`配置`location`中添加 `proxy_set_header X-Forwarded-Proto $scheme;`
> 通过获取请求头`$this->request->getHeaderLine('x-forwarded-proto');`来获取对应的`scheme`
### 获取应用请求地址

`public function getAppUri(?string $serverName = null)`

`getUri()` 不同的是,可以通过配置修改 `getUri()` 获取到的 Uri 里的 `host` 等参数。

适合用于替换生产环境中的 https、域名等参数。

需要在服务器配置中修改,详见对应容器服务器配置。

`$serverName` 默认不传则使用当前服务器。

### 获取 Swoole 服务器对象

`public function getServerInstance(): \Imi\Swoole\Server\Http\Server`
Expand Down
24 changes: 24 additions & 0 deletions doc/container/php-fpm/serverConfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 服务器配置

[toc]

```php
'fpmServer' => [
// $request->getAppUri() 参数替换,每个参数都是可选项
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
'appUri' => [
'host' => 'imi-test', // 主机名
'port' => 1234, // 端口
'scheme' => 'https', // 协议
'user' => 'root', // 用户名
'pass' => '123', // 密码
'path' => '/test', // 路径
'query' => 'id=666', // 查询参数
'fragment' => 'test', // 锚点
],
// 也支持回调
'appUri' => function(\Imi\Util\Uri $uri) {
return $uri->withHost('imi-test');
},
],
```
16 changes: 16 additions & 0 deletions doc/container/roadrunner/serverConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
'roadRunnerServer' => [
'main' => [
'namespace' => 'ImiApp\ApiServer', // Http 服务的命名空间,为空则使用项目命名空间
// $request->getAppUri() 参数替换,每个参数都是可选项
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
'appUri' => [
'host' => 'imi-test', // 主机名
'port' => 1234, // 端口
'scheme' => 'https', // 协议
'user' => 'root', // 用户名
'pass' => '123', // 密码
'path' => '/test', // 路径
'query' => 'id=666', // 查询参数
'fragment' => 'test', // 锚点
],
// 也支持回调
'appUri' => function(\Imi\Util\Uri $uri) {
return $uri->withHost('imi-test');
},
],
],

Expand Down
16 changes: 16 additions & 0 deletions doc/container/workerman/serverConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ return [
'beans' => [
'aaa' => XXX::class,
],
// $request->getAppUri() 参数替换,每个参数都是可选项
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
'appUri' => [
'host' => 'imi-test', // 主机名
'port' => 1234, // 端口
'scheme' => 'https', // 协议
'user' => 'root', // 用户名
'pass' => '123', // 密码
'path' => '/test', // 路径
'query' => 'id=666', // 查询参数
'fragment' => 'test', // 锚点
],
// 也支持回调
'appUri' => function(\Imi\Util\Uri $uri) {
return $uri->withHost('imi-test');
},
],
// 下面可以继续加入其它协议其它端口的服务
'websocket' => [
Expand Down
16 changes: 16 additions & 0 deletions doc/core/subServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ http 和 WebSocket 同时做在一个项目中,代码之间没有隔阂,可
'beans' => [
'aaa' => XXX::class,
],
// $request->getAppUri() 参数替换,每个参数都是可选项
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
'appUri' => [
'host' => 'imi-test', // 主机名
'port' => 1234, // 端口
'scheme' => 'https', // 协议
'user' => 'root', // 用户名
'pass' => '123', // 密码
'path' => '/test', // 路径
'query' => 'id=666', // 查询参数
'fragment' => 'test', // 锚点
],
// 也支持回调
'appUri' => function(\Imi\Util\Uri $uri) {
return $uri->withHost('imi-test');
},
],
],
]
Expand Down
2 changes: 1 addition & 1 deletion src/Components/fpm/src/FpmApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function run(): void
$server = ServerManager::getServer('main');
if (null === $server)
{
$server = ServerManager::createServer('main', [
$server = ServerManager::createServer('main', Config::get('@app.fpmServer') + [
'type' => Type::HTTP,
'namespace' => $this->namespace,
]);
Expand Down
2 changes: 2 additions & 0 deletions src/Components/fpm/tests/HttpServer/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Imi\Fpm\Test\Web\Tests;

use Imi\Util\Http\Consts\MediaType;
use Imi\Util\Uri;
use Yurun\Util\HttpRequest;
use Yurun\Util\YurunHttp\Http\Psr7\UploadedFile;

Expand Down Expand Up @@ -210,6 +211,7 @@ public function testUri(): void
$response = $http->get($uri);
$data = $response->json(true);
$this->assertEquals($uri, $data['uri'] ?? null);
$this->assertEquals(TEST_APP_CALLBACK_URI, $data['appUri'] ?? null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function info()
'server' => $request->getServerParams(),
'request' => $request->request(),
'uri' => (string) $request->getUri(),
'appUri' => (string) $request->getAppUri(),
];
}

Expand Down
4 changes: 4 additions & 0 deletions src/Components/fpm/tests/Web/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,8 @@
],
],
],

'fpmServer' => [
'appUri' => 'testAppCallbackUriConfig',
],
];
4 changes: 2 additions & 2 deletions src/Components/roadrunner/src/RoadRunnerApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public function loadRuntime(): int
public function run(): void
{
$config = Config::get('@app.roadRunnerServer.main', []);
$server = ServerManager::createServer('main', [
$server = ServerManager::createServer('main', $config + [
'type' => Type::HTTP,
'namespace' => $config['namespace'] ?? $this->namespace,
'namespace' => $this->namespace,
]);
Event::trigger('IMI.APP.INIT', [], $this);
$server->start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function info()
'server' => $request->getServerParams(),
'request' => $request->request(),
'uri' => (string) $request->getUri(),
'appUri' => (string) $request->getAppUri(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function testUri(): void
$response = $http->get($uri);
$data = $response->json(true);
$this->assertEquals($uri, $data['uri'] ?? null);
$this->assertEquals(TEST_APP_URI, $data['appUri'] ?? null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
'RoadRunner' => 'Imi\RoadRunner',
],
'roadRunnerServer' => [
'main' => [],
'main' => [
'appUri' => TEST_APP_URI_CONFIG,
],
],
'ignorePaths' => [
\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'bin',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function info(): array
'server' => $request->getServerParams(),
'request' => $request->request(),
'uri' => (string) $request->getUri(),
'appUri' => (string) $request->getAppUri(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function info(): array
'server' => $request->getServerParams(),
'request' => $request->request(),
'uri' => (string) $request->getUri(),
'appUri' => (string) $request->getAppUri(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testUri(): void
$response = $http->get($uri);
$data = $response->json(true);
$this->assertEquals($uri, $data['uri'] ?? null);
$this->assertEquals($uri, $data['appUri'] ?? null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public function testUri(): void
$response = $http->get($uri);
$data = $response->json(true);
$this->assertEquals($uri, $data['uri'] ?? null);
$this->assertEquals(TEST_APP_URI, $data['appUri'] ?? null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
'task_worker_num' => 1,
'max_wait_time' => 30,
],
'appUri' => TEST_APP_URI_CONFIG,
],

// 子服务器(端口监听)配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function info(): array
'server' => $request->getServerParams(),
'request' => $request->request(),
'uri' => (string) $request->getUri(),
'appUri' => (string) $request->getAppUri(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public function testUri(): void
$response = $http->get($uri);
$data = $response->json(true);
$this->assertEquals($uri, $data['uri'] ?? null);
$this->assertEquals(TEST_APP_URI, $data['appUri'] ?? null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
'port' => 13000,
'configs' => [
],
'appUri' => TEST_APP_URI_CONFIG,
],
'websocket' => [
'namespace' => 'Imi\Workerman\Test\AppServer\WebSocketServer',
Expand Down
2 changes: 2 additions & 0 deletions src/Server/Http/Message/Proxy/RequestProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
* @method static \Imi\Util\Http\Contract\IRequest setMethod(string $method)
* @method \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
* @method static \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
* @method \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
* @method static \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
*/
class RequestProxy extends BaseRequestContextProxy
{
Expand Down
10 changes: 10 additions & 0 deletions src/Server/Http/Message/Proxy/RequestProxyObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
* @method static \Imi\Util\Http\Contract\IRequest setMethod(string $method)
* @method \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
* @method static \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
* @method \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
* @method static \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
*/
class RequestProxyObject extends BaseRequestContextProxy implements \Imi\Server\Http\Message\Contract\IHttpRequest
{
Expand Down Expand Up @@ -555,4 +557,12 @@ public function setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost =
{
return self::__getProxyInstance()->setUri($uri, $preserveHost);
}

/**
* {@inheritDoc}
*/
public function getAppUri(?string $serverName = null): \Psr\Http\Message\UriInterface
{
return self::__getProxyInstance()->getAppUri($serverName);
}
}
2 changes: 2 additions & 0 deletions src/Util/Http/Contract/IRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public function setMethod(string $method): self;
* @return static
*/
public function setUri(UriInterface $uri, bool $preserveHost = false): self;

public function getAppUri(?string $serverName = null): UriInterface;
}
Loading

0 comments on commit f6c7d50

Please sign in to comment.