denagus/arxservice

ARX平台的HTTP请求核心

1.0.8 2022-04-26 14:14 UTC

This package is not auto-updated.

Last update: 2024-09-24 18:59:01 UTC


README

Total Downloads Latest Stable Version License

ARX Service 是ARX应用程序用于在服务之间进行HTTP请求的包,具有身份验证和安全访问功能

支持的版本

版本Laravel版本
> 1.0.0>=5.7

安装方法

  • composer require denagus/arxservice

    ##### Laravel

    • Laravel Package Discovery自动注册

    ##### Lumen

    • bootstrap/app.php中注册服务提供者
        	$app->register(Denagus\ArxService\ArxServiceProvider::class);
      

      配置设置

  • 创建文件config/arxservice.php

      <?php
    
      return [
        
          /*
          |--------------------------------------------------------------------------
          | Encryption Key
          |--------------------------------------------------------------------------
          |
          | This key is used by the ARX Service and should be set
          | to a random, 32 character string, otherwise these encrypted strings
          | will not be safe. Please do this before deploying an application!
          |
          */
        
          'key' => env('APP_KEY'),
        
          'cipher' => 'AES-256-CBC',
        
          /*
          |--------------------------------------------------------------------------
          | Hash
          |--------------------------------------------------------------------------
          |
          | This key is used by the ARX Service
          |
          */
        
          'hash' => 'sha256',
        
          /*
          |--------------------------------------------------------------------------
          | Expire
          |--------------------------------------------------------------------------
          |
          | The expire time is the number of seconds that the access key should be
          | considered valid. This security feature keeps access keys short-lived so
          | they have less time to be guessed. You may change this as needed.
          |
          */
        
          'expire' => 14400
        
      ];
    
  • 注册配置文件#### Laravel
    • 自动注册#### Lumen
    • app/bootstrap.php中注册配置文件
        	$app->configure('arxservice');
      

      设置中间件

  • 确保每个服务请求都使用中间件service#### Laravel
    • app/Http/Kernel.php中注册中间件服务
        /**
         * The application's route middleware.
         *
         * These middleware may be assigned to groups or used individually.
         *
         * @var array
         */
        	protected $routeMiddleware = [
            'service'   => \Denagus\ArxService\Middleware\Accesskey::class,
        ];
      

      #### Lumen

    • app/bootstrap.php中注册中间件服务
        	$app->routeMiddleware([
            'service'   => Denagus\ArxService\Middleware\Accesskey::class,
        ]);
      
  • controller文件中使用中间件的示例

      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this->middleware('service');
      }
    

    设置服务

    1. 在路径app/Libraries/Services上创建文件夹
    2. 创建文件UserService.php
        <?php
      

    namespace App\Libraries\Services;

    use Denagus\ArxService\Service;

    class UserService extends Service {

      /**
      * SERVICE_USER_URI
      */
      protected $baseUri = 'SERVICE_USER_URI';
        
      /**
      * Available URL
      */
      private const USER_BY_ID = 'api/v1/user/service/by-user-id';
    

    }

    3. Buat file `AuthService.php`
    

    <?php

    namespace App\Libraries\Services;

    use Denagus\ArxService\Service;

    class AuthService extends Service {

      /**
      * SERVICE_USER_URI
      */
      protected $baseUri = 'SERVICE_AUTH_URI';
    

    }

    3. Tambahkan `SERVICE_USER_URI` pada .env, dan atur URI dari masing-masing service
    

    SERVICE_USER_URI=https:// SERVICE_AUTH_URI=https://

    
    

使用方法

  • 请求服务用户/UserService

      use App\Libraries\Services\UserService;
      use Denagus\ArxService\Exception as ServiceException;
        
      try {
          $userService = UserService::get(UserService::USER_BY_ID . "/1");
          ServiceException::on($userService);
          $user = $userService->data;
      } catch (\Exception $e) {
          $user = null;
      }