spacetab-io / amphp-support
用于减少服务之间代码重复的包。
1.1.0
2020-05-02 09:08 UTC
Requires
- php: >=7.4
- ext-json: *
- ext-pcntl: *
- amphp/http-server: ^2.0
- spacetab-io/body-validator: ^1.0
- spacetab-io/transformation-layer: ^1.0
Requires (Dev)
- amphp/http-client: ^4.2
- amphp/http-server-router: ^1.0
- amphp/phpunit-util: ^1.3
- phpstan/phpstan: ^0.12.5
- phpunit/phpunit: ^9
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-09-20 19:40:25 UTC
README
安装
composer require spacetab-io/amphp-support
使用
use Amp\Http\{Server\HttpServer, Server\Request, Server\Response, Server\Router, Status}; use Amp\{Loop, Promise, Socket}; use HarmonyIO\Validation\Rule\{Combinator\All, Text\AlphaNumeric, Text\LengthRange}; use Spacetab\AmphpSupport\Handler\AbstractTrustedRequest; use Spacetab\AmphpSupport\Server\ErrorHandler; use Spacetab\AmphpSupport\Middleware\{AcceptJsonBody, ExceptionMiddleware}; use function Amp\call; Loop::run(function () { $sockets = [ Socket\Server::listen('0.0.0.0:8081'), ]; $router = new Router(); $handler = new class extends AbstractTrustedRequest { public function handleRequest(Request $request): Promise { var_dump($this->getTrustedBody()); return call(fn() => new Response(Status::OK, [], '{"message": "hey!"}')); } }; $router->stack(new ExceptionMiddleware()); $router->addRoute('POST', '/', $handler, new class extends AcceptJsonBody { public function validate(): iterable { yield 'username' => new All(new LengthRange(3, 15), new AlphaNumeric()); } }); $server = new HttpServer($sockets, $router, new \Psr\Log\NullLogger()); $server->setErrorHandler(new ErrorHandler()); yield $server->start(); // Stop the server gracefully when SIGINT is received. Loop::onSignal(SIGINT, function (string $watcherId) use ($server) { Loop::cancel($watcherId); yield $server->stop(); }); });
curl -X POST -d '{"username": "roquie"}' -H 'Content-Type: application/json' http://0.0.0.0:8081/ | jq .
依赖
- >= PHP 7.4
- 使用Composer安装包
许可协议
MIT许可协议
版权所有 © 2020 spacetab.io, Inc. https://spacetab.io
在此特此授予任何人免费获得此软件及其相关文档副本(“软件”)的权利,不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供的人进行此类操作,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的适用性和非侵权性保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任承担责任,无论是基于合同、侵权或其他原因,无论该索赔、损害或其他责任是否源于、源于或与软件或其使用或其他操作有关。