benycode / slim-middleware
此包最新版本(v1.5.0)没有提供许可证信息。
Slim 4 中间件包
v1.5.0
2024-01-29 17:11 UTC
Requires
- php: ^8.0
README
A Slim 4 框架的有用中间件。
特性
- 健康检查端点;
- 信息端点;
- 设置配置;
- 异常处理器;
- APISIX 自动路由注册;
- 领导者选举中间件;
- 使用 X-Api-Token 保护端点。
目录
- 安装
- [健康检查端点使用](#health check endpoint usage)
- [信息端点使用](#info endpoint usage)
- [设置配置使用](#settings setup usage)
- [异常处理器使用](#exception handler usage)
- [APISIX 自动路由注册使用](#apisix auto route register usage)
- [领导者选举使用](#leader election usage)
- [使用 X-Api-Token 保护端点](#endpoint protection with X-Api-Token usage)
安装
通过 Composer
$ composer require benycode/slim-middleware
需要 Slim 4。
健康检查端点使用
使用 DI 注入库中的 Middleware 类
use BenyCode\Slim\Middleware\HealthCheckEndpointMiddleware; return [ ...... HealthCheckEndpointMiddleware::class => function (ContainerInterface $container) { return new HealthCheckEndpointMiddleware( [ 'health_endpoint' => '/_health', // change if needed other endpoint ], <<inject you PSR7 logger if needed>>, ); }, ...... ];
将 Middleware 添加到任何路由的末尾
use Slim\Exception\HttpNotFoundException; use BenyCode\Slim\Middleware\HealthCheckEndpointMiddleware; $app ->get( '/{any:.*}', function (Request $request, Response $response) { throw new HttpNotFoundException($request); } ) .... ->add(HealthCheckEndpointMiddleware::class) ->setName('any') ;
欢迎,您的应用已在新路径中
- /_health 或您定义的
创建健康检查。
信息端点使用
使用 DI 注入库中的 Middleware 类
use BenyCode\Slim\Middleware\InfoEndpointMiddleware; return [ ...... InfoEndpointMiddleware::class => function (ContainerInterface $container) { return new InfoEndpointMiddleware( [ 'info_endpoint' => '/_info', // change if needed other endpoint ], '<<define api version here>>', // example: v0.0.0 ); }, ...... ];
将 Middleware 添加到任何路由的末尾
use Slim\Exception\HttpNotFoundException; use BenyCode\Slim\Middleware\InfoEndpointMiddleware; $app ->get( '/{any:.*}', function (Request $request, Response $response) { throw new HttpNotFoundException($request); } ) .... ->add(InfoEndpointMiddleware::class) ->setName('any') ;
欢迎,您的应用已在新路径中
- /_info
设置配置使用
将 Middleware 添加到全局列表中
use BenyCode\Middleware\SettingsUpMiddleware; return function (App $app) { ... $app->add(SettingsUpMiddleware::class); ... };
获取设置
protected function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { $settings = $request ->getAttribute('settings') ; }
异常处理器使用
将 Middleware 添加到全局列表中
use BenyCode\Middleware\ExceptionMiddleware; return function (App $app) { ... $app->add(ExceptionMiddleware::class); ... };
欢迎,您的应用已在新错误处理器中
APISIX 自动路由注册使用
想法:在健康检查过程中自动创建服务和路由。
您可以使用它与
- Docker 健康检查;
- k8s 健康检查;
- 以及其他更多...
需要 curl
和 docker/k8s
健康检查机制。
与 LeaderElectionMiddleware
平衡,提供更多稳定性和激活单个实例注册功能。
使用 DI 注入库中的 Middleware 类
use BenyCode\Slim\Middleware\APISIXRegisterMiddleware; return [ ...... APISIXRegisterMiddleware::class => function (ContainerInterface $container) { return new APISIXRegisterMiddleware( [ 'register_endpoint' => '/_health', // change if needed other endpoint 'service_id' => '<<describe your service name>>', 'service' => [ 'upstream' => [ 'type' => 'roundrobin', 'nodes' => [ '<<describe working endpoint>>:<<describe working port>>' => 1, // example: books-microservice:80 ], ], ], 'route' => [ 'uri' => "<<describe working path>>", // example: /books/* 'service_id' => '<<describe service id>>', // example: books-microservice ], 'api_admin_secret' => '<<describe APISIX admin secret>>', 'api_endpoint' => '<<describe APISIX API endpoint url>>', // example: http://api-gateway:9180 ], <<inject you PSR7 logger if needed>>, ); }, ...... ];
将 Middleware 添加到任何路由的末尾
use Slim\Exception\HttpNotFoundException; use BenyCode\Slim\Middleware\APISIXRegisterMiddleware; $app ->get( '/{any:.*}', function (Request $request, Response $response) { throw new HttpNotFoundException($request); } ) .... ->add(APISIXRegisterMiddleware::class) ->setName('any') ;
创建健康检查 /_health
或您定义的。
欢迎,您的应用将在每次健康检查时自动(重新)注册到 APISIX 上。
领导者选举使用
想法:在微服务世界中,可能有多个实例可以执行相关命令,需要这些命令只能由一个实例执行。使用健康检查机制投票选举领导者!
与 APISIXRegisterMiddleware
平衡。
您可以使用它与
- Docker 健康检查;
- k8s 健康检查;
- 以及其他更多...
需要 curl
、docker/k8s
健康检查机制和 ETCD v3
。
使用 DI 注入库中的 Middleware 类
use BenyCode\Slim\Middleware\LeaderElectionMiddleware; return [ ...... LeaderElectionMiddleware::class => function (ContainerInterface $container) { return new LeaderElectionMiddleware( [ 'leader_election_endpoint' => '/_health', // change if needed other endpoint 'etcd_endpoint' => '<<etcd endpoint>>', 'alection_frequency' => 5, // alection frequence in seconds <<inject you PSR7 logger if needed>>, ], ); }, ...... ];
将 Middleware 添加到任何路由的末尾
use Slim\Exception\HttpNotFoundException; use BenyCode\Slim\Middleware\LeaderElectionMiddleware; $app ->get( '/{any:.*}', function (Request $request, Response $response) { throw new HttpNotFoundException($request); } ) .... ->add(LeaderElectionMiddleware::class) ->setName('any') ;
获取领导者状态
protected function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { $leader = $request ->getAttribute('im_leader') ; if($leader) { // the leader code } }
使用 X-Api-Token 保护端点
想法:像健康检查一样使用 Api token 保护您的端点。
使用 DI 注入库中的 Middleware 类
use BenyCode\Slim\Middleware\OnePathXApiTokenProtectionMiddleware; return [ ...... OnePathXApiTokenProtectionMiddleware::class => function (ContainerInterface $container) { return new OnePathXApiTokenProtectionMiddleware( [ 'path' => '/_health', // change if needed other endpoint 'x-api-token' => '4bfdb81c03f42600d9018103a4df878b', // change to yours <<inject you PSR7 logger if needed>>, ], ); }, ...... ];
将 Middleware 添加到任何路由的末尾
use Slim\Exception\HttpNotFoundException; use BenyCode\Slim\Middleware\OnePathXApiTokenProtectionMiddleware; $app ->get( '/{any:.*}', function (Request $request, Response $response) { throw new HttpNotFoundException($request); } ) .... ->add(OnePathXApiTokenProtectionMiddleware::class) ->setName('any') ;