哺乳动物/healthz-vhost

⚕️ 基本健康检查虚拟主机

dev-master 2023-01-14 19:47 UTC

This package is auto-updated.

Last update: 2024-09-14 23:46:05 UTC


README

time docker run --rm -w `pwd` -v `pwd`:`pwd` -p 9666:9666  -it wyrihaximusnet/php:7.4-zts-alpine3.11 php ./vendor/bin/mammatus

HTTP服务器命令

Build Status Latest Stable Version Total Downloads Code Coverage License PHP 7 ready

安装

要通过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}")

可以使用以下方式从请求对象中获取不同的路由组件,如 mapcenter

$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 - 公共webroot用于服务,注意只在此处放置文件,任何人都可查看
  • http-server.public.preload.cache - 存储预加载webroot文件的自定义缓存,默认存储在内存中
  • http-server.middleware.prefix - 在accesslog和webroot服务中间件之前添加的react/http中间件数组
  • http-server.middleware.suffix - 在accesslog和webroot服务中间件之后、路由中间件和请求处理器之前添加的react/http中间件数组
  • http-server.pool.ttl - 子进程等待其下一个任务的超时时间
  • http-server.pool.min - 子进程的最小数量
  • http-server.pool.max - 子进程的最大数量
  • http-server.rewrites - 内部重写请求路径,从一条路径重写到另一条路径,对访客不可见

许可证

MIT许可证(MIT)

版权所有(c)2019 Cees-Jan Kiewiet

特此授予任何人免费获得本软件及其相关文档文件(以下简称“软件”)的副本的权利,无限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供副本的个人这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论此类责任基于合同、侵权或其他方式,源自、因之或与此软件或其使用或其他交易有关。