philo/artisan-remote

一个与Laravel Artisan命令交互的HTTP API。

v1.0.2 2021-01-24 10:41 UTC

This package is auto-updated.

Last update: 2024-09-05 18:59:28 UTC


README

Build Status Total Downloads Latest Stable Version License

关于Artisan Remote

Artisan Remote是一个Laravel包,允许您通过HTTP API与Artisan命令交互。

Artisan Remote桌面版

如果您想通过UI轻松地与Artisan命令交互,请确保下载桌面应用程序,该应用程序与Artisan Remote API集成。

  • 只需点击一下即可执行命令。
  • 通过简单的输入字段提供命令参数。
  • 跟踪先前执行命令的输出。
  • 支持无限数量的Laravel应用程序。
  • 在单一视图中查看应用程序环境详情。
  • 在Mac、Windows和Linux上运行。

通过Unlock下载Mac、Windows或Linux版本的Artisan Remote

安装

要开始使用,请通过Composer要求该包

composer require philo/artisan-remote

API端点

环境端点将返回有关应用程序环境的信息,例如您的PHP和Laravel版本。

GET /artisan-remote/environment

Content-Type: application/json
Accept: application/json
Authorization: Bearer 039ede05-d2c1-4ab4-8869-945e805e6bbc

{
  "applicationName": "Unlock.sh",
  "phpVersion": "7.4.12",
  "frameworkVersion": "7.28.1",
  "environment": "production",
  "inMaintenanceMode": true,
  "maxExecutionTime": "30"
}

获取可用命令的列表。您可以在artisan-remote.php配置文件中定义您希望公开的命令。

GET /artisan-remote/commands

Content-Type: application/json
Accept: application/json
Authorization: Bearer 039ede05-d2c1-4ab4-8869-945e805e6bbc

[
  {
    "name": "down",
    "description": "Put the application into maintenance mode",
    "arguments": [],
    "options": [
      {
        "name": "message",
        "description": "The message for the maintenance mode",
        "default": null,
        "isArray": false,
        "isRequired": false,
        "isOptional": true
      },
      {
        "name": "retry",
        "description": "The number of seconds after which the request may be retried",
        "default": null,
        "isArray": false,
        "isRequired": false,
        "isOptional": true
      },
      {
        "name": "allow",
        "description": "IP or networks allowed to access the application while in maintenance mode",
        "default": [],
        "isArray": true,
        "isRequired": false,
        "isOptional": true
      }
    ]
  },
  {
    "name": "up",
    "description": "Bring the application out of maintenance mode",
    "arguments": [],
    "options": []
  }
]

最后,您可以使用调用端点来执行您的命令。

POST /artisan-remote/commands/invoke

Content-Type: application/json
Accept: application/json
Authorization: Bearer 039ede05-d2c1-4ab4-8869-945e805e6bbc

{
  "name": "down",
  "options": {
    "message": "We will be back in 15 minutes",
    "retry": "900",
    "allow": [
      "127.0.0.1"
    ]
  }
}

// Response
{
  "rawCommandOutput": "\u001b[33mApplication is now in maintenance mode.\u001b[39m\n",
  "HtmlCommandOutput": "<span style=\"background-color: transparent; color: #e5e510\">Application is now in maintenance mode.<\/span><span style=\"background-color: transparent; color: #e5e5e5\">\n<\/span>",
  "exitCode": 0,
  "executionTime": 0.017518997192382812
}

API授权

在您可以对API端点发出请求之前,您需要设置授权。您可以在artisan-remote.php配置文件中授权对特定命令的访问。例如,您可能希望允许客户端运行artisan up和down命令。

<?php

return [
    'commands'     => [
        \Illuminate\Foundation\Console\UpCommand::class,
        \Illuminate\Foundation\Console\DownCommand::class,
        \Illuminate\Cache\Console\ClearCommand::class,
    ],
    'auth'         => [
        // This API token will be able to access only the up and down command.
        '79e9ab08-bdc0-4bef-8af2-5e5b5579f9af' => [
            \Illuminate\Foundation\Console\UpCommand::class,
            \Illuminate\Foundation\Console\DownCommand::class,
        ],
        // This API token will be able to access the up, down and cache:clear command.
        '3c562cb3-62ba-4fe4-9875-528ecae6e8b4' => ['*'],
    ],
    'route_prefix' => 'artisan-remote',
];

最好实践是不直接在配置文件中包含任何凭证,因此请确保在生产环境中使用环境变量。

// artisan-remote.php
'auth'         => [
    env('CLIENT_ARTISAN_REMOTE_API_KEY') => [
        \Illuminate\Foundation\Console\UpCommand::class,
        \Illuminate\Foundation\Console\DownCommand::class,
    ],
]

// .env
CLIENT_ARTISAN_REMOTE_API_KEY=79e9ab08-bdc0-4bef-8af2-5e5b5579f9af
当应用程序处于维护模式时运行命令

默认情况下,当您的应用程序处于维护模式时,您将无法执行命令。要在维护模式下运行命令,您需要调整CheckForMaintenanceMode中间件。

// app/Http/Middleware/CheckForMaintenanceMode.php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;

class CheckForMaintenanceMode extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array
     */
    protected $except = [
        'artisan-remote/*'
    ];
}

致谢

许可

Artisan Remote是开源软件,在MIT许可证下授权。