morcen/passage

Laravel 的 API 网关

v1.2.3 2023-08-06 04:31 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

介绍

这是一个强大的 Laravel API 网关包,允许您高效地管理和路由传入的 API 请求到各种微服务。它简化了处理 API 请求和响应的过程,使得客户端和微服务之间的通信更加顺畅。

特性

  • 请求路由到多个微服务
  • 请求有效负载验证和转换
  • 响应有效负载转换
  • 身份验证和授权处理(即将推出)
  • 速率限制和节流(即将推出)
  • 缓存和响应缓存(即将推出)
  • 错误处理和日志记录(即将推出)

要求

  • PHP 版本 8.1 或更高
  • Laravel 版本 8.x 或更高

安装

您可以通过 composer 安装此包

composer require morcen/passage

然后使用以下命令安装包

php artisan passage:install

这将发布包的配置文件到 config/passage.php

如果您想创建一个处理请求的控制器,使用以下命令发布控制器存根

php artisan vendor:publish --tag=passage-stubs

然后运行以下命令生成 Passage 控制器

php artisan passage:controller {name}

其中 {name} 是您要生成的控制器名称。

用法

启用 Passage

要开始使用此包,请将以下行添加到您的 routes/web.php 中以启用 Passage

Route::passage();

并确保在您的 .env 中,PASSAGE_ENABLED 没有设置或设置为 true

PASSAGE_ENABLED=true

设置网关路由

Passage 作为代理

config/passage.php 中,定义您想要转发服务的列表。

示例

// config/passage.php
return [
    'services' => [
        // Forwards `GET http://{your-host}/github/users/morcen` to `GET https://api.github.com/users/morcen`:
        'github' => [ // <-- This is the name of the service
            'base_uri' => 'http://users-service/api/v1/', // <-- This is where the request will be forwarded to
            // other options at https://docs.guzzlephp.org/en/stable/request-options.html
        ],
    ]
]

注意

  • 确保 base_uri 以反斜杠 / 结尾,否则请求可能无法正确转发。
  • 所有头部、查询参数以及请求类型(GETPOST 等)都将转发到服务。
通过 Passage 控制器转换/验证请求和响应

如果您有一个名为 github 的服务,并且您想处理传入和传出的有效负载,您可以运行以下命令创建一个控制器

php artisan passage:controller GithubPassageController

这将创建一个控制器在 app/Http/Controllers/Passage/GithubPassageController.php

在您的控制器中,您可以定义以下方法

// app/Http/Controllers/Passage/GithubPassageController.php
use App\Http\Controllers\Controller;
use Illuminate\Http\Client\Response;
use Illuminate\Http\Request;
use Morcen\Passage\PassageControllerInterface;

class GithubPassageController extends Controller implements PassageControllerInterface
{
    /**
     * Transform and/or validate the request before it is sent to the service.
     *
     * @param  Request  $request
     * @return Request
     */
    public function getRequest(Request $request): Request
    {
        // Transform the request here
        return $request;
    }

    /**
     * Transform or validate the response before it is sent back to the client.
     *
     * @param  Request  $request
     * @param  Response  $response
     * @return Response
     */
    public function getResponse(Request $request, Response $response): Response
    {
        // Transform the response here
        return $response;
    }

    /**
     * Set the route options when the service is instantiated.
     *
     * @return array
     */
    public function getOptions(): array
    {
        return [
             'base_uri' => 'https://api.github.com/',
        ];
    }
}

在您的 config/passage.php 中,将控制器添加到 services 数组中

// config/passage.php
return [
    'services' => [
        'github' => \App\Http\Controllers\Passage\GithubPassageController::class, // <-- Add this line,
    ]
]

使用 Passage 门面

如果您不希望使用 Passage 的自动路由并想在控制器中手动使用 Passage 服务,您可以使用 Passage 门面。

// config/passage.php
return [
    'services' => [
        'github' => 'https://api.github.com/',
    ]
]

并在您的控制器中

// app/Http/Controllers/UserController.php

use Morcen\Passage\Facades\Passage

class UserController extends Controller
{
    public function index()
    {
        $response = Passage::getService('github')->get('users/morcen');
        return $response->json();
    }
}

禁用 Passage

要禁用服务器/应用程序级别的 Passage,请在您的 .env 文件中将 PASSAGE_ENABLED 设置为 false

PASSAGE_ENABLED=false

或者,在您的 routes/web.php 中取消注释此行

Route::passage();

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。