reactive-apps / command-http-server
HTTP 服务器命令
Requires
- php: ^7.3
- cakephp/collection: ^3.6
- doctrine/annotations: ^1.6
- lcobucci/jwt: ^3.3
- nikic/fast-route: ^1.2
- react/http: ^0.8.0
- reactive-apps/command: dev-master
- reactive-apps/life-cycle-events: dev-master
- roave/better-reflection: ^3.2
- thecodingmachine/safe: ^0.1.16
- thruway/middleware: ^0.0.2
- voryx/thruway: ^0.5.3
- wyrihaximus/broadcast: dev-master
- wyrihaximus/doctrine-annotation-autoloader: ^1.0
- wyrihaximus/get-in-packages-composer.jason: ^1.0
- wyrihaximus/json-psr7: ^1.0
- wyrihaximus/psr-3-callable-throwable-logger: ^2.0
- wyrihaximus/psr-3-context-logger: ^1.0
- wyrihaximus/react-child-process-closure: ^1.0
- wyrihaximus/react-child-process-pool: ^1.4
- wyrihaximus/react-http-middleware-resume-response-body: ^1.0
- wyrihaximus/react-http-middleware-rewrite: ^1.0
- wyrihaximus/react-http-middleware-runner: ^1.0
- wyrihaximus/react-http-middleware-session: ^3.0
- wyrihaximus/react-http-middleware-twig: ^1.0
- wyrihaximus/react-http-middleware-webroot-preload: ^1.0
- wyrihaximus/react-http-psr-15-middleware-group: ^1.2
- wyrihaximus/react-stream-json: ^1.2
- wyrihaximus/recoil-promise-coroutine-wrapper: ^1.0
- wyrihaximus/recoil-queue-caller-pool: ^1.2
- wyrihaximus/string-get-in: ^1.0
- wyrihaximus/to-child-process-or-not-to-child-process: ^1.0
- wyrihaximus/to-coroutine-or-not-to-coroutine: ^1.0
- wyrihaximus/to-thread-or-not-to-thread: dev-master
- zendframework/zend-diactoros: ^2.1
Requires (Dev)
- monolog/monolog: ^2.0
- twig/twig: ^2.8
- wyrihaximus/async-test-utilities: ^1.1
- wyrihaximus/cs-fixer-config: ^1.0
- wyrihaximus/pool-info: ^1.0
- wyrihaximus/react-parallel: dev-master
Suggests
- wyrihaximus/react-parallel: Run request handlers in threads (more performant than child processes)
This package is auto-updated.
Last update: 2024-08-29 04:31:37 UTC
README
安装
要通过 Composer 安装,请使用以下命令,它将自动检测最新版本并将其绑定到 ^
。
composer require reactive-apps/command-http-server
控制器
控制器有两种不同的类型:静态控制器和实例化控制器。
静态控制器
当您的控制器没有依赖项时,推荐使用静态控制器,例如此 ping 控制器用于 updown.io
健康检查。 注意: /ping
不是 updown 的标准,但这是我为我的应用程序进行健康检查的个人标准 此控制器只有一个方法、一个路由和没有依赖项。
<?php declare(strict_types=1); namespace App\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use ReactiveApps\Command\HttpServer\Annotations\Method; use ReactiveApps\Command\HttpServer\Annotations\Routes; use RingCentral\Psr7\Response; final class Ping { /** * @Method("GET") * @Routes("/ping") * * @param ServerRequestInterface $request * @return ResponseInterface */ public static function ping(ServerRequestInterface $request): ResponseInterface { return new Response( 200, ['Content-Type' => 'text/plain'], 'pong' ); } }
实例化控制器
另一方面,实例化控制器将被实例化并保持,以便在将来处理更多请求,因此它们可以注入依赖项。以下示例是一个控制器,它注入事件循环以在返回响应之前等待随机秒数。它还使用协程使代码更易读。
<?php declare(strict_types=1); namespace App\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\LoopInterface; use ReactiveApps\Command\HttpServer\Annotations\Method; use ReactiveApps\Command\HttpServer\Annotations\Routes; use ReactiveApps\Command\HttpServer\Annotations\Template; use ReactiveApps\Command\HttpServer\TemplateResponse; use WyriHaximus\Annotations\Coroutine; use function WyriHaximus\React\timedPromise; /** * @Coroutine()) */ final class Root { /** @var LoopInterface */ private $loop; /** @var int */ private $time; public function __construct(LoopInterface $loop) { $this->loop = $loop; $this->time = \time(); } /** * @Method("GET") * @Routes("/") * @Template("root") * * @param ServerRequestInterface $request * @return ResponseInterface */ public function root(ServerRequestInterface $request) { $start = \time(); yield timedPromise($this->loop, \random_int(1, 5)); return (new TemplateResponse( 200, ['Content-Type' => 'text/plain'] ))->withTemplateData([ 'uptime' => (\time() - $this->time), 'took' => (\time() - $start), ]); } }
路由
路由是通过处理路由的方法上的注解来完成的。每个方法可以处理多个路由,但建议仅映射适合该方法的路由。
例如,以下注解将当前方法映射到 /
(注意:所有路由都必须以 /
前缀):@Routes("/")
多路由注解有略微不同的语法,在以下示例中,/old
和 /new
都将由相同的方法处理。
@Routes({ "/old", "/new" })
路由的底层引擎是 nikic/fast-route
,这还使得像这样的复杂路由成为可能。
@Route("/{map:(?:wow_cata_draenor|wow_cata_land|wow_cata_underwater|wow_legion_azeroth|wow_battle_for_azeroth|wow_cata_elemental_plane|wow_cata_twisting_nether|wow_comp_wotlk)}/{zoom:1|2|3|4|5|6|7|8|9|10}/{width:[0-9]{1,5}}/{height:[0-9]{1,5}}/{center:[a-zA-Z0-9\`\-\~\_\@\%]{1,35}}{blips:/blip\_center|/[a-zA-Z0-9\`\-\~\_\@\%\[\]]{3,}.+|}.{quality:png|hq.jpg|lq.jpg}")
可以使用请求对象中的不同路由组件(如 map
和 center
)。
$request->getAttribute('center');
模板
路由可以在完成后渲染模板。它需要一个注解,并返回/解析一个包含该模板所需数据的 TemplateResponse
。例如
/** * @Template("root") */ public function root(ServerRequestInterface $request) { return (new TemplateResponse( 200, ['Content-Type' => 'text/plain'] ))->withTemplateData([ 'beer' => 'Allmouth', // https://untappd.com/user/WyriHaximus/checkin/745226210 ]); }
请求中的阻塞操作
虽然我们旨在构建一个完全非阻塞的应用程序,但我们无法逃避这样一个事实,即我们的应用程序中可能会有部分阻塞循环。对于这些情况,有两种方法可以处理这些情况
- 子进程(慢,为每个工作进程启动完整的 PHP 进程以处理请求)
- 线程(快,使用线程执行工作,需要 ZTS 版本的 PHP)
子进程
适用于大多数系统,但如果不是所有系统,则每个工作进程都需要一个完整的 PHP 进程。启动可能较慢,与子进程的通信通过套接字进行。将 @ChildProcess
注解添加到处理特定操作的子进程中。
线程
仅在 ZTS PHP 构建上工作,但启动几乎瞬间完成,通信直接在内存中进行,因此永远不会离开应用程序服务器。将 @Thread
注解添加到处理特定操作的线程中。
注解
@ChildProcess
- 在子进程中运行控制器操作@Coroutine
- 在协程中运行控制器操作@Method
- 允许的 HTTP 方法(GET、POST、PATCH 等)@Routes
- 要使用给定方法的路由@Template
- 当使用 TemplateResponse 时使用的模板@Thread
- 在线程中运行控制器操作(比使用子进程更受欢迎)
选项
http-server.address
- 监听的IP地址和端口,例如:0.0.0.0:8080
http-server.hsts
- 是否设置HSTS头信息http-server.public
- 公共Web根目录,注意这里放置的文件任何人都可以查看http-server.public.preload.cache
- 自定义缓存用于存储预加载的Web根目录文件,默认存储在内存中http-server.middleware.prefix
- 一个数组,包含在accesslog和webroot服务中间件之前添加的react/http中间件http-server.middleware.suffix
- 一个数组,包含在accesslog和webroot服务中间件之后、路由中间件和请求处理器之前的react/http中间件http-server.pool.ttl
- 子进程等待下一个任务的TTL(生存时间)http-server.pool.min
- 最小子进程数量http-server.pool.max
- 最大子进程数量http-server.rewrites
- 内部重写请求路径,从一条路径到另一条路径,对访客不可见
许可证
MIT许可证(MIT)
版权所有 © 2019 Cees-Jan Kiewiet
在此特此授予任何获取此软件及其相关文档副本(“软件”)的个人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得软件的个人进行此类操作,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
本软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论此类责任基于合同、侵权或其他原因,是否由软件或其使用或其它方式引起。