mirror of
https://github.com/harivansh-afk/betterNAS.git
synced 2026-04-16 20:01:23 +00:00
init
This commit is contained in:
parent
4f174ec3a8
commit
db1dea097f
81 changed files with 6263 additions and 545 deletions
88
apps/nextcloud-app/lib/Service/ControlPlaneClient.php
Normal file
88
apps/nextcloud-app/lib/Service/ControlPlaneClient.php
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\BetterNasControlplane\Service;
|
||||
|
||||
use OCP\Http\Client\IClientService;
|
||||
use OCP\Http\Client\IResponse;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ControlPlaneClient {
|
||||
public function __construct(
|
||||
private readonly IClientService $clientService,
|
||||
private readonly ControlPlaneConfig $controlPlaneConfig,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function fetchSnapshot(): array {
|
||||
$baseUrl = $this->controlPlaneConfig->getBaseUrl();
|
||||
|
||||
try {
|
||||
$healthResponse = $this->request($baseUrl . '/health');
|
||||
$versionResponse = $this->request($baseUrl . '/version');
|
||||
|
||||
return [
|
||||
'available' => $healthResponse['statusCode'] === 200,
|
||||
'url' => $baseUrl,
|
||||
'health' => $healthResponse['body'],
|
||||
'version' => $versionResponse['body'],
|
||||
];
|
||||
} catch (\Throwable $exception) {
|
||||
$this->logger->warning('Failed to reach betterNAS control plane', [
|
||||
'exception' => $exception,
|
||||
'url' => $baseUrl,
|
||||
]);
|
||||
|
||||
return [
|
||||
'available' => false,
|
||||
'url' => $baseUrl,
|
||||
'error' => $exception->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{statusCode: int, body: array<string, mixed>}
|
||||
*/
|
||||
private function request(string $url): array {
|
||||
$client = $this->clientService->newClient();
|
||||
$response = $client->get($url, [
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
],
|
||||
'http_errors' => false,
|
||||
'timeout' => 2,
|
||||
'nextcloud' => [
|
||||
'allow_local_address' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
return [
|
||||
'statusCode' => $response->getStatusCode(),
|
||||
'body' => $this->decodeBody($response),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function decodeBody(IResponse $response): array {
|
||||
$body = $response->getBody();
|
||||
if ($body === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
|
||||
if (!is_array($decoded)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
31
apps/nextcloud-app/lib/Service/ControlPlaneConfig.php
Normal file
31
apps/nextcloud-app/lib/Service/ControlPlaneConfig.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\BetterNasControlplane\Service;
|
||||
|
||||
use OCA\BetterNasControlplane\AppInfo\Application;
|
||||
use OCP\IAppConfig;
|
||||
|
||||
class ControlPlaneConfig {
|
||||
public function __construct(
|
||||
private readonly IAppConfig $appConfig,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getBaseUrl(): string {
|
||||
$environmentUrl = getenv('BETTERNAS_CONTROL_PLANE_URL');
|
||||
if (is_string($environmentUrl) && $environmentUrl !== '') {
|
||||
return rtrim($environmentUrl, '/');
|
||||
}
|
||||
|
||||
$configuredUrl = $this->appConfig->getValueString(
|
||||
Application::APP_ID,
|
||||
'control_plane_url',
|
||||
'http://control-plane:3000',
|
||||
);
|
||||
|
||||
return rtrim($configuredUrl, '/');
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue