More WebUI Updates.

This commit is contained in:
Abdulmhsen B. A. A
2024-05-03 17:28:17 +03:00
parent f53034ffa5
commit 85fcc593cd
14 changed files with 453 additions and 71 deletions

View File

@@ -4,74 +4,42 @@ 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 as iResponse;
use Psr\Http\Message\ServerRequestInterface as iRequest;
use Throwable;
final class Env
{
public const string URL = '%{api.prefix}/system/env';
private const array BLACKLIST = [
'WS_API_KEY'
];
private EnvFile $envfile;
private const array BLACKLIST_PARSE_URL = [
'WS_CACHE_URL' => [
'password',
],
];
public function __construct()
{
$this->envfile = (new EnvFile(file: Config::get('path') . '/config/.env', create: true));
}
#[Get(self::URL . '[/]', name: 'system.env')]
public function envList(iRequest $request): iResponse
{
$response = [
'data' => [],
'file' => Config::get('path') . '/config/.env',
'links' => [
'self' => (string)$request->getUri()->withHost('')->withPort(0)->withScheme(''),
],
];
foreach ($_ENV as $key => $val) {
if (false === str_starts_with($key, 'WS_') && $key !== 'HTTP_PORT') {
foreach ($this->envfile->getAll() as $key => $val) {
if (false === str_starts_with($key, 'WS_')) {
continue;
}
try {
if (array_key_exists($key, self::BLACKLIST_PARSE_URL)) {
$val = new Uri($val);
$query = $val->getQuery();
$auth = $val->getUserInfo();
if (!empty($auth) && str_contains($auth, ':')) {
$val = $val->withUserInfo(before($auth, ':'), '__hidden__');
}
if (!empty($query)) {
parse_str($query, $q);
foreach ($q ?? [] as $k => $v) {
if (false === in_array(strtolower($k), self::BLACKLIST_PARSE_URL[$key], true)) {
continue;
}
$q[$k] = '__hidden__';
}
$val = $val->withQuery(http_build_query($q));
}
$val = (string)$val;
}
} catch (Throwable) {
}
if (in_array($key, self::BLACKLIST, true)) {
$val = '__hidden__';
}
$response['data'][] = [
'key' => $key,
'value' => $val,
@@ -92,19 +60,17 @@ final class Env
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)) {
if (false === str_starts_with($key, 'WS_')) {
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) {
if (false === $this->envfile->has($key)) {
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,
'value' => $this->envfile->get($key),
]);
}
@@ -121,10 +87,8 @@ final class Env
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);
$this->envfile->remove($key);
} else {
$params = DataUtil::fromRequest($request);
if (null === ($value = $params->get('value', null))) {
@@ -133,14 +97,14 @@ final class Env
]), HTTP_STATUS::HTTP_BAD_REQUEST);
}
$envfile->set($key, $value);
$this->envfile->set($key, $value);
}
$envfile->persist();
$this->envfile->persist();
return api_response(HTTP_STATUS::HTTP_OK, [
'key' => $key,
'value' => env($key, fn() => $envfile->get($key)),
'value' => env($key, fn() => $this->envfile->get($key)),
]);
}
}

View File

@@ -22,6 +22,9 @@ final class Index
{
}
/**
* @throws InvalidArgumentException
*/
#[Get(self::URL . '[/]', name: 'tasks.index')]
public function tasksIndex(iRequest $request): iResponse
{
@@ -35,6 +38,8 @@ final class Index
],
];
$queuedTasks = $this->cache->get('queued_tasks', []);
foreach (TasksCommand::getTasks() as $task) {
$task = array_filter(
self::formatTask($task),
@@ -47,6 +52,8 @@ final class Index
'queue' => (string)$apiUrl->withPath($urlPath . '/' . ag($task, 'name') . '/queue'),
];
$task['queued'] = in_array(ag($task, 'name'), $queuedTasks);
$response['tasks'][] = $task;
}
@@ -56,7 +63,7 @@ final class Index
/**
* @throws InvalidArgumentException
*/
#[Route(['GET', 'POST'], self::URL . '/{id:[a-zA-Z0-9_-]+}/queue[/]', name: 'tasks.task.queue')]
#[Route(['GET', 'POST', 'DELETE'], self::URL . '/{id:[a-zA-Z0-9_-]+}/queue[/]', name: 'tasks.task.queue')]
public function taskQueue(iRequest $request, array $args = []): iResponse
{
if (null === ($id = ag($args, 'id'))) {
@@ -77,6 +84,12 @@ final class Index
return api_response(HTTP_STATUS::HTTP_ACCEPTED, ['queue' => $queuedTasks]);
}
if ('DELETE' === $request->getMethod()) {
$queuedTasks = array_filter($queuedTasks, fn($v) => $v !== $id);
$this->cache->set('queued_tasks', $queuedTasks, new DateInterval('P3D'));
return api_response(HTTP_STATUS::HTTP_OK, ['queue' => $queuedTasks]);
}
$apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme('')->withUserInfo('');
$urlPath = parseConfigValue(Index::URL);