Allow adding/removing environment keys using API.
This commit is contained in:
@@ -4,14 +4,18 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\API\System;
|
||||
|
||||
use App\Commands\System\EnvCommand;
|
||||
use App\Libs\Attributes\Route\Get;
|
||||
use App\Libs\Attributes\Route\Route;
|
||||
use App\Libs\Config;
|
||||
use App\Libs\DataUtil;
|
||||
use App\Libs\EnvFile;
|
||||
use App\Libs\HTTP_STATUS;
|
||||
use App\Libs\Uri;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface as iResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface as iRequest;
|
||||
use Throwable;
|
||||
|
||||
#[Get(self::URL . '[/]', name: 'system.env')]
|
||||
final class Env
|
||||
{
|
||||
public const string URL = '%{api.prefix}/system/env';
|
||||
@@ -26,7 +30,8 @@ final class Env
|
||||
],
|
||||
];
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, array $args = []): ResponseInterface
|
||||
#[Get(self::URL . '[/]', name: 'system.env')]
|
||||
public function envList(iRequest $request): iResponse
|
||||
{
|
||||
$response = [
|
||||
'data' => [],
|
||||
@@ -70,9 +75,72 @@ final class Env
|
||||
$response['data'][] = [
|
||||
'key' => $key,
|
||||
'value' => $val,
|
||||
'urls' => [
|
||||
'self' => (string)$request->getUri()->withPath(parseConfigValue(self::URL . '/' . $key)),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, $response);
|
||||
}
|
||||
|
||||
#[Get(self::URL . '/{key}[/]', name: 'system.env.view')]
|
||||
public function envView(iRequest $request, array $args = []): iResponse
|
||||
{
|
||||
$key = strtoupper((string)ag($args, 'key', ''));
|
||||
if (empty($key)) {
|
||||
return api_error('Invalid value for key path parameter.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (false === str_starts_with($key, 'WS_') && false === in_array($key, EnvCommand::EXEMPT_KEYS)) {
|
||||
return api_error(r("Invalid key '{key}' was given.", ['key' => $key]), HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$val = env($key, '_not_set');
|
||||
|
||||
if ('_not_set' === $val) {
|
||||
return api_error(r("Key '{key}' is not set.", ['key' => $key]), HTTP_STATUS::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, [
|
||||
'key' => $key,
|
||||
'value' => $val,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(['POST', 'DELETE'], self::URL . '/{key}[/]', name: 'system.env.update')]
|
||||
public function envUpdate(iRequest $request, array $args = []): iResponse
|
||||
{
|
||||
$key = strtoupper((string)ag($args, 'key', ''));
|
||||
|
||||
if (empty($key)) {
|
||||
return api_error('Invalid value for key path parameter.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (false === str_starts_with($key, 'WS_')) {
|
||||
return api_error(r("Invalid key '{key}' was given.", ['key' => $key]), HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$envfile = (new EnvFile(file: Config::get('path') . '/config/.env', create: true));
|
||||
|
||||
if ('DELETE' === $request->getMethod()) {
|
||||
$envfile->remove($key);
|
||||
} else {
|
||||
$params = DataUtil::fromRequest($request);
|
||||
if (null === ($value = $params->get('value', null))) {
|
||||
return api_error(r("No value was provided for '{key}'.", [
|
||||
'key' => $key,
|
||||
]), HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$envfile->set($key, $value);
|
||||
}
|
||||
|
||||
$envfile->persist();
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, [
|
||||
'key' => $key,
|
||||
'value' => env($key, fn() => $envfile->get($key)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user