feat: portal groups

This commit is contained in:
Rekryt
2024-09-05 00:54:41 +03:00
parent f5a036c89e
commit 73b38ec96b
5 changed files with 69 additions and 23 deletions

View File

@@ -2,12 +2,25 @@
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Entity\Site;
use OpenCCK\Domain\Factory\SiteFactory;
class MainController extends AbstractIPListController {
/**
* @var array<string, array<string, Site>>
*/
private array $groups = [];
/**
* @return string
*/
public function getBody(): string {
$this->setHeaders(['content-type' => 'text/html; charset=utf-8']);
foreach ($this->service->sites as $siteEntity) {
$this->groups[$siteEntity->group][$siteEntity->name] = $siteEntity;
}
return $this->renderTemplate('index');
}

View File

@@ -10,6 +10,7 @@ use Exception;
use Monolog\Logger;
use Revolt\EventLoop;
use function Amp\delay;
use function OpenCCK\dbg;
class IPListService {
private static IPListService $_instance;
@@ -30,17 +31,27 @@ class IPListService {
if (!is_dir($dir)) {
throw new Exception('config directory not found');
}
foreach (scandir($dir) as $file) {
if (str_ends_with($file, '.json')) {
$name = substr($file, 0, -5);
$this->loadConfig($name, json_decode(file_get_contents($dir . $file)));
foreach (scandir($dir) as $item) {
if (in_array($item, ['.', '..'])) {
continue;
}
$path = $dir . $item;
if (is_dir($path)) {
foreach (scandir($path) as $file) {
if (is_file($path . '/' . $file)) {
$this->loadConfig($path . '/' . $file);
}
}
}
}
EventLoop::queue(function () {
foreach ($this->sites as $siteEntity) {
$siteEntity->reload();
delay(1);
if ($siteEntity->timeout) {
$siteEntity->reload();
delay(1);
}
}
});
}
@@ -55,15 +66,24 @@ class IPListService {
}
/**
* @param string $name
* @param object $config
* @param string $path
* @return void
*/
private function loadConfig(string $name, object $config): void {
if (isset($this->sites[$name])) {
$this->logger->error(sprintf('Site "%s" already exists', $name));
return;
private function loadConfig(string $path): void {
if (str_ends_with($path, '.json')) {
$parts = explode('/', $path);
$filename = array_pop($parts);
$name = substr($filename, 0, -5);
$config = json_decode(file_get_contents($path));
$group = array_pop($parts);
if (isset($this->sites[$name])) {
$this->logger->error(sprintf('Site config "%s" already exists', $name));
delay(5);
exit();
}
$this->sites[$name] = SiteFactory::create($name, $group, $config);
}
$this->sites[$name] = SiteFactory::create($name, $config);
}
}

View File

@@ -1,6 +1,7 @@
<?php
use OpenCCK\App\Controller\TextController;
/** @var TextController $this */
use OpenCCK\App\Controller\MainController;
/** @var MainController $this */
?>
<!doctype html>
<html lang="en">
@@ -600,7 +601,9 @@ use OpenCCK\App\Controller\TextController;
text-align: center;
}
.main-formSelect {}
.main-formSelect_site {}
.main-formSelect_site {
min-height: 60vh;
}
.main-formItemCheckbox {
margin: 0 6px 0 0;
}
@@ -644,8 +647,12 @@ use OpenCCK\App\Controller\TextController;
<span>
Site:
<select name="site" class="main-formSelect main-formSelect_site" multiple>
<?php foreach ($this->service->sites as $site): ?>
<option value="<?= $site->name ?>"><?= $site->name ?></option>
<?php foreach ($this->groups as $group => $items): ?>
<optgroup label="<?= $group ?>">
<?php foreach ($items as $site): ?>
<option value="<?= $site->name ?>"><?= $site->name ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
</span>