feat: new frontend

This commit is contained in:
Rekryt
2025-07-02 20:40:13 +03:00
parent bda887db3c
commit 7d7c82514f
111 changed files with 5223 additions and 52 deletions

View File

@@ -23,7 +23,8 @@ abstract class AbstractController implements ControllerInterface {
* @return Response
*/
public function __invoke(): Response {
return new Response(status: $this->httpStatus, headers: $this->headers, body: $this->getBody());
$body = $this->getBody();
return new Response(status: $this->httpStatus, headers: $this->headers, body: $body);
}
abstract public function getBody(): string;

View File

@@ -2,16 +2,30 @@
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Entity\Site;
use OpenCCK\Domain\Factory\SiteFactory;
use Amp\Http\HttpStatus;
use function Amp\File\isFile;
use function Amp\File\read;
use function finfo_close;
use function finfo_file;
use function finfo_open;
class MainController extends AbstractIPListController {
/**
* @return string
*/
public function getBody(): string {
$this->setHeaders(['content-type' => 'text/html; charset=utf-8']);
return $this->renderTemplate('index');
$url = parse_url($this->request->getUri());
$path = str_replace('..', '', $url['path']);
$path = PATH_ROOT . '/public/' . $path;
foreach ([$path, $path . 'index.html', $path . '.html', $path . '/index.html'] as $filePath) {
if (isFile($filePath)) {
return $this->output($filePath);
}
}
$this->setHttpStatus(HttpStatus::NOT_FOUND);
return '';
}
/**
@@ -23,4 +37,27 @@ class MainController extends AbstractIPListController {
include PATH_ROOT . '/src/App/Template/' . ucfirst($template) . 'Template.php';
return ob_get_clean();
}
/**
* @param string $path
* @return string
*/
private function output(string $path): string {
$mimeTypes = [
'js' => 'application/javascript',
'json' => 'application/json',
'css' => 'text/css',
];
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if (isset($mimeTypes[$ext])) {
$contentType = $mimeTypes[$ext];
} else {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$contentType = finfo_file($finfo, $path);
finfo_close($finfo);
}
$this->setHeaders(['content-type' => $contentType]);
return read($path);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace OpenCCK\App\Controller;
use Amp\Http\HttpStatus;
use function Amp\File\isFile;
use function Amp\File\read;
use function mime_content_type;
class ScriptsController extends AbstractIPListController {
/**
* @return string
*/
public function getBody(): string {
$url = parse_url($this->request->getUri());
$path = str_replace('..', '', $url['path']);
$path = implode('/', array_slice(explode('/', $path), 2));
$path = PATH_ROOT . '/scripts/' . $path;
if (isFile($path) && ($mimeType = mime_content_type($path))) {
$this->setHeaders(['content-type' => $mimeType]);
return read($path);
}
$this->setHttpStatus(HttpStatus::NOT_FOUND);
return '';
}
}

View File

@@ -21,11 +21,11 @@ class TextController extends AbstractIPListController {
$response = [];
if (count($sites)) {
foreach ($sites as $site) {
$response = array_merge($response, $this->getSites()[$site]->$data);
$response = array_merge($response, $this->getSites()[$site]->$data ?? []);
}
} else {
foreach ($this->getSites() as $siteEntity) {
$response = array_merge($response, $siteEntity->$data);
$response = array_merge($response, $siteEntity->$data ?? []);
}
}

View File

@@ -10,6 +10,7 @@ use Amp\Http\Server\Response;
use Psr\Log\LoggerInterface;
use Throwable;
use function OpenCCK\dbg;
use function OpenCCK\getEnv;
final class HTTPHandler extends Handler implements HTTPHandlerInterface {

View File

@@ -98,7 +98,9 @@ final class Server implements AppModuleInterface {
// $this->bindContext
//);
$router = new Router($this->httpServer, $this->logger, $this->errorHandler);
$router->addRoute('GET', '/', HTTPHandler::getInstance($this->logger)->getHandler());
$httpHandler = HTTPHandler::getInstance($this->logger)->getHandler();
$router->addRoute('GET', '/', $httpHandler);
$router->addRoute('GET', '/{name:.+}', $httpHandler);
$router->setFallback(new DocumentRoot($this->httpServer, $this->errorHandler, PATH_ROOT . '/public'));
$this->httpServer->start($router, $this->errorHandler);