Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spacetab-io/amphp-support-php
Package to reduce code duplication across services.
https://github.com/spacetab-io/amphp-support-php
amphp async php php74
Last synced: 15 days ago
JSON representation
Package to reduce code duplication across services.
- Host: GitHub
- URL: https://github.com/spacetab-io/amphp-support-php
- Owner: spacetab-io
- License: mit
- Created: 2020-04-19T16:06:59.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-20T09:43:58.000Z (over 4 years ago)
- Last Synced: 2024-09-30T01:07:44.947Z (about 2 months ago)
- Topics: amphp, async, php, php74
- Language: PHP
- Size: 210 KB
- Stars: 2
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Amphp support
=============[![CircleCI](https://circleci.com/gh/spacetab-io/amphp-support-php/tree/master.svg?style=svg)](https://circleci.com/gh/spacetab-io/amphp-support-php/tree/master)
[![codecov](https://codecov.io/gh/spacetab-io/amphp-support-php/branch/master/graph/badge.svg)](https://codecov.io/gh/spacetab-io/amphp-support-php)## Installation
```bash
composer require spacetab-io/amphp-support
```## Usage
```php
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();
});
});
``````bash
curl -X POST -d '{"username": "roquie"}' -H 'Content-Type: application/json' http://0.0.0.0:8081/ | jq .
```## Depends
* \>= PHP 7.4
* Composer for install package## License
The MIT License
Copyright © 2020 spacetab.io, Inc. https://spacetab.io
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.