Updated to new HTTP router.

This commit is contained in:
flash 2024-03-30 00:52:32 +00:00
parent 2bef9f456c
commit b115706359
6 changed files with 35 additions and 39 deletions

32
composer.lock generated
View file

@ -12,7 +12,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://patchii.net/flash/index.git", "url": "https://patchii.net/flash/index.git",
"reference": "18397477d6734b4336c8b273f069cd8584173435" "reference": "9d5b050b8928435416a7efbebe2a19ae8e626224"
}, },
"require": { "require": {
"ext-mbstring": "*", "ext-mbstring": "*",
@ -50,7 +50,7 @@
], ],
"description": "Composer package for the common library for my projects.", "description": "Composer package for the common library for my projects.",
"homepage": "https://railgun.sh/index", "homepage": "https://railgun.sh/index",
"time": "2024-01-04T02:07:43+00:00" "time": "2024-03-28T23:27:04+00:00"
}, },
{ {
"name": "flashwave/syokuhou", "name": "flashwave/syokuhou",
@ -209,16 +209,16 @@
}, },
{ {
"name": "jean85/pretty-package-versions", "name": "jean85/pretty-package-versions",
"version": "2.0.5", "version": "2.0.6",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/Jean85/pretty-package-versions.git", "url": "https://github.com/Jean85/pretty-package-versions.git",
"reference": "ae547e455a3d8babd07b96966b17d7fd21d9c6af" "reference": "f9fdd29ad8e6d024f52678b570e5593759b550b4"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/Jean85/pretty-package-versions/zipball/ae547e455a3d8babd07b96966b17d7fd21d9c6af", "url": "https://api.github.com/repos/Jean85/pretty-package-versions/zipball/f9fdd29ad8e6d024f52678b570e5593759b550b4",
"reference": "ae547e455a3d8babd07b96966b17d7fd21d9c6af", "reference": "f9fdd29ad8e6d024f52678b570e5593759b550b4",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -226,9 +226,9 @@
"php": "^7.1|^8.0" "php": "^7.1|^8.0"
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "^2.17", "friendsofphp/php-cs-fixer": "^3.2",
"jean85/composer-provided-replaced-stub-package": "^1.0", "jean85/composer-provided-replaced-stub-package": "^1.0",
"phpstan/phpstan": "^0.12.66", "phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^7.5|^8.5|^9.4", "phpunit/phpunit": "^7.5|^8.5|^9.4",
"vimeo/psalm": "^4.3" "vimeo/psalm": "^4.3"
}, },
@ -262,9 +262,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/Jean85/pretty-package-versions/issues", "issues": "https://github.com/Jean85/pretty-package-versions/issues",
"source": "https://github.com/Jean85/pretty-package-versions/tree/2.0.5" "source": "https://github.com/Jean85/pretty-package-versions/tree/2.0.6"
}, },
"time": "2021-10-08T21:21:46+00:00" "time": "2024-03-08T09:58:59+00:00"
}, },
{ {
"name": "psr/http-factory", "name": "psr/http-factory",
@ -525,16 +525,16 @@
}, },
{ {
"name": "sentry/sentry", "name": "sentry/sentry",
"version": "4.3.1", "version": "4.6.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/getsentry/sentry-php.git", "url": "https://github.com/getsentry/sentry-php.git",
"reference": "cd89f230bda0833cb9992ebe9a1b7d24d6ee245b" "reference": "5a94184175e5830b589bf923da8c9c3af2c0f409"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/getsentry/sentry-php/zipball/cd89f230bda0833cb9992ebe9a1b7d24d6ee245b", "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/5a94184175e5830b589bf923da8c9c3af2c0f409",
"reference": "cd89f230bda0833cb9992ebe9a1b7d24d6ee245b", "reference": "5a94184175e5830b589bf923da8c9c3af2c0f409",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -598,7 +598,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/getsentry/sentry-php/issues", "issues": "https://github.com/getsentry/sentry-php/issues",
"source": "https://github.com/getsentry/sentry-php/tree/4.3.1" "source": "https://github.com/getsentry/sentry-php/tree/4.6.1"
}, },
"funding": [ "funding": [
{ {
@ -610,7 +610,7 @@
"type": "custom" "type": "custom"
} }
], ],
"time": "2023-12-22T18:46:49+00:00" "time": "2024-03-08T08:18:09+00:00"
}, },
{ {
"name": "symfony/deprecation-contracts", "name": "symfony/deprecation-contracts",

View file

@ -18,7 +18,7 @@
</div> </div>
</div> </div>
<div class="uiharu-footer"> <div class="uiharu-footer">
<a href="https://flash.moe" target="_blank" rel="noopener">flashwave</a> 2021-2023 <a href="https://flash.moe" target="_blank" rel="noopener">flashwave</a> 2021-2024
</div> </div>
</div> </div>
</div> </div>

View file

@ -18,10 +18,10 @@ use Index\MediaType;
use Index\Colour\Colour; use Index\Colour\Colour;
use Index\Colour\ColourRGB; use Index\Colour\ColourRGB;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
use Index\Http\HttpFx; use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler};
use Index\Performance\Stopwatch; use Index\Performance\Stopwatch;
final class v1_0 implements \Uiharu\IApi { final class v1_0 extends RouteHandler implements \Uiharu\IApi {
private UihContext $ctx; private UihContext $ctx;
private IDbConnection $db; private IDbConnection $db;
@ -34,15 +34,7 @@ final class v1_0 implements \Uiharu\IApi {
return !str_starts_with($url, '/v'); return !str_starts_with($url, '/v');
} }
public function register(HttpFx $router): void { #[HttpGet('/metadata/thumb/audio')]
$router->get('/metadata', [$this, 'getMetadata']);
$router->post('/metadata', [$this, 'postMetadata']);
$router->get('/metadata/batch', [$this, 'getMetadataBatch']);
$router->post('/metadata/batch', [$this, 'postMetadataBatch']);
$router->get('/metadata/thumb/audio', [$this, 'getThumbAudio']);
$router->get('/metadata/thumb/video', [$this, 'getThumbVideo']);
}
public function getThumbAudio($response, $request) { public function getThumbAudio($response, $request) {
$targetUrl = (string)$request->getParam('url'); $targetUrl = (string)$request->getParam('url');
@ -63,6 +55,7 @@ final class v1_0 implements \Uiharu\IApi {
$response->setContent(FFMPEG::grabFirstAudioCover($parsedUrl)); $response->setContent(FFMPEG::grabFirstAudioCover($parsedUrl));
} }
#[HttpGet('/metadata/thumb/video')]
public function getThumbVideo($response, $request) { public function getThumbVideo($response, $request) {
$targetUrl = (string)$request->getParam('url'); $targetUrl = (string)$request->getParam('url');
@ -83,6 +76,7 @@ final class v1_0 implements \Uiharu\IApi {
$response->setContent(FFMPEG::grabFirstVideoFrame($parsedUrl)); $response->setContent(FFMPEG::grabFirstVideoFrame($parsedUrl));
} }
#[HttpGet('/metadata')]
public function getMetadata($response, $request) { public function getMetadata($response, $request) {
if($request->getMethod() === 'HEAD') { if($request->getMethod() === 'HEAD') {
$response->setTypeJson(); $response->setTypeJson();
@ -95,6 +89,7 @@ final class v1_0 implements \Uiharu\IApi {
); );
} }
#[HttpPost('/metadata')]
public function postMetadata($response, $request) { public function postMetadata($response, $request) {
if(!$request->isStreamContent()) if(!$request->isStreamContent())
return 400; return 400;
@ -105,6 +100,7 @@ final class v1_0 implements \Uiharu\IApi {
); );
} }
#[HttpGet('/metadata/batch')]
public function getMetadataBatch($response, $request) { public function getMetadataBatch($response, $request) {
if($request->getMethod() === 'HEAD') { if($request->getMethod() === 'HEAD') {
$response->setTypeJson(); $response->setTypeJson();
@ -117,6 +113,7 @@ final class v1_0 implements \Uiharu\IApi {
); );
} }
#[HttpPost('/metadata/batch')]
public function postMetadataBatch($response, $request) { public function postMetadataBatch($response, $request) {
if(!$request->isFormContent()) if(!$request->isFormContent())
return 400; return 400;

View file

@ -1,9 +1,8 @@
<?php <?php
namespace Uiharu; namespace Uiharu;
use Index\Http\HttpFx; use Index\Http\Routing\IRouteHandler;
interface IApi { interface IApi extends IRouteHandler {
function match(string $url): string; function match(string $url): string;
function register(HttpFx $router): void;
} }

View file

@ -2,13 +2,13 @@
namespace Uiharu; namespace Uiharu;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
use Index\Http\HttpFx; use Index\Http\Routing\HttpRouter;
use Syokuhou\IConfig; use Syokuhou\IConfig;
final class UihContext { final class UihContext {
private IDbConnection $database; private IDbConnection $database;
private IConfig $config; private IConfig $config;
private HttpFx $router; private HttpRouter $router;
private array $apis = []; private array $apis = [];
private array $lookups = []; private array $lookups = [];
@ -21,7 +21,7 @@ final class UihContext {
return $this->database; return $this->database;
} }
public function getRouter(): HttpFx { public function getRouter(): HttpRouter {
return $this->router; return $this->router;
} }
@ -39,7 +39,7 @@ final class UihContext {
} }
public function setupHttp(): void { public function setupHttp(): void {
$this->router = new HttpFx; $this->router = new HttpRouter;
$this->router->use('/', function($response) { $this->router->use('/', function($response) {
$response->setPoweredBy('Uiharu'); $response->setPoweredBy('Uiharu');
}); });
@ -69,8 +69,8 @@ final class UihContext {
}); });
} }
public function dispatchHttp(...$args): void { public function dispatchHttp(): void {
$this->router->dispatch(...$args); $this->router->dispatch();
} }
public function registerApi(IApi $api): void { public function registerApi(IApi $api): void {
@ -81,7 +81,7 @@ final class UihContext {
$reqPath = '/' . trim(parse_url($reqPath, PHP_URL_PATH), '/'); $reqPath = '/' . trim(parse_url($reqPath, PHP_URL_PATH), '/');
foreach($this->apis as $api) foreach($this->apis as $api)
if($api->match($reqPath)) { if($api->match($reqPath)) {
$api->register($this->router); $this->router->register($api);
break; break;
} }
} }

View file

@ -11,7 +11,7 @@ define('UIH_DEBUG', is_file(UIH_ROOT . '/.debug'));
define('UIH_PUBLIC', UIH_ROOT . '/public'); define('UIH_PUBLIC', UIH_ROOT . '/public');
define('UIH_SOURCE', UIH_ROOT . '/src'); define('UIH_SOURCE', UIH_ROOT . '/src');
define('UIH_LIBRARY', UIH_ROOT . '/lib'); define('UIH_LIBRARY', UIH_ROOT . '/lib');
define('UIH_VERSION', '20231215'); define('UIH_VERSION', '20240330');
require_once UIH_ROOT . '/vendor/autoload.php'; require_once UIH_ROOT . '/vendor/autoload.php';