https://github.com/lazychanger/swoole-gin
Teaching warehouse
https://github.com/lazychanger/swoole-gin
http http-protocol php swoole
Last synced: 8 months ago
JSON representation
Teaching warehouse
- Host: GitHub
- URL: https://github.com/lazychanger/swoole-gin
- Owner: lazychanger
- License: mit
- Created: 2022-03-21T02:17:02.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-29T10:04:58.000Z (over 3 years ago)
- Last Synced: 2025-05-19T10:05:17.615Z (8 months ago)
- Topics: http, http-protocol, php, swoole
- Language: PHP
- Homepage:
- Size: 646 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swoole-gin
Teaching warehouse
## [中文](./README_CN.md)
## [Document](./docs/00_Summary.md)
## How to install
```shell
$ composer require swoole/swoole-gin
```
## Usage
```php
declare(strict_types=1);
use SwooleGin\Gin\Context\Context;
use SwooleGin\Gin\Context\ContextHandlerFuncInterface;
use SwooleGin\Gin\Gin;
use SwooleGin\Gin\Middleware\FaviconMiddleware;
use SwooleGin\Options;
use SwooleGin\Server;
use SwooleGin\Stream\StringStream;
use SwooleGin\Utils\HTTPStatus;
include_once __DIR__ . '/../vendor/autoload.php';
$servOpts = new Options();
$servOpts->setAddr('0.0.0.0:8082');
$serv = new Server($servOpts);
$engine = new Gin();
$engine->use(
(new FaviconMiddleware),
// 假鉴权中间件
(new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
if ($context->query('token') !== '123456') {
$context->response->withBody(new StringStream('authorized failed'));
$context->response->withStatus(HTTPStatus::StatusForbidden);
$context->abort();
}
}
}),
// 用于模拟洋葱结构中间件:打印响应内容
(new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
$context->next();
$body = $context->response->getBody()->getContents();
echo 'resp:', $body, PHP_EOL;
$context->response->withBody(new StringStream($body));
}
}),
);
$engine->GET('/hello', (new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
$context->Raw(HTTPStatus::StatusOK, 'hello world');
}
}));
$engine->setOnNotFound((new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
$context->JSON(HTTPStatus::StatusOK, ['code' => HTTPStatus::StatusNotFound, 'msg' => 'not found']);
}
}));
$serv->setHandler($engine);
// server run
$serv->serve();
```
## todo
- [x] middleware
- [x] context
- [ ] gin-router