Use attribute based route registration.

This commit is contained in:
flash 2024-05-22 20:57:01 +00:00
parent 627c28bd4d
commit ac4a2deca6
2 changed files with 14 additions and 21 deletions

View file

@ -55,6 +55,6 @@ class AwakiContext {
}
private function registerHttpRoutes(): void {
new RedirectorRoutes($this->router, $this->dbConn, $this->urls);
$this->router->register(new RedirectorRoutes($this->dbConn, $this->urls));
}
}

View file

@ -2,40 +2,27 @@
namespace Awaki;
use Index\Data\IDbConnection;
use Index\Http\Routing\IRouter;
use Index\Http\Routing\{HandlerAttribute,HttpGet,IRouter,IRouteHandler};
use Index\Serialisation\Base62;
use Syokuhou\IConfig;
final class RedirectorRoutes {
final class RedirectorRoutes implements IRouteHandler {
private IDbConnection $dbConn;
private IConfig $urls;
public function __construct(IRouter $router, IDbConnection $dbConn, IConfig $urls) {
public function __construct(IDbConnection $dbConn, IConfig $urls) {
$this->dbConn = $dbConn;
$this->urls = $urls;
}
$router->get('/', $this->index(...));
// profile
$router->get('/[up]([0-9]+)', $this->redirectProfile(...));
$router->get('/[up]/([A-Za-z0-9\-_]+)', $this->redirectProfile(...));
// satori short urls
$router->get('/[bg]/([A-Za-z0-9]+)', $this->redirectSatoriShort(...));
// forum categories
$router->get('/fc?/?([0-9]+)', $this->redirectForumCategory(...));
// forum topic
$router->get('/ft/?([0-9]+)', $this->redirectForumTopic(...));
// forum post
$router->get('/fp/?([0-9]+)', $this->redirectForumPost(...));
public function registerRoutes(IRouter $router): void {
HandlerAttribute::register($router, $this);
// databased, registered last cuz it matches everything otherwise!
$router->get('/([A-Za-z0-9\-_]+)', $this->redirectDatabase(...));
}
#[HttpGet('/')]
public function index($response): void {
$response->accelRedirect('/index.html');
$response->setTypeHTML();
@ -66,6 +53,7 @@ final class RedirectorRoutes {
$this->redirect($response, $request, $info->getString(1));
}
#[HttpGet('/[bg]/([A-Za-z0-9]+)')]
public function redirectSatoriShort($response, $request, string $linkId) {
$linkId = Base62::decode($linkId);
@ -88,18 +76,23 @@ final class RedirectorRoutes {
$this->redirect($response, $request, $url);
}
#[HttpGet('/[up]([0-9]+)')]
#[HttpGet('/[up]/([A-Za-z0-9\-_]+)')]
public function redirectProfile($response, $request, string $userId) {
$this->redirectSimple($response, $request, $this->urls->getString('user_profile'), $userId);
}
#[HttpGet('/fc?/?([0-9]+)')]
public function redirectForumCategory($response, $request, string $categoryId) {
$this->redirectSimple($response, $request, $this->urls->getString('forum_category'), $categoryId);
}
#[HttpGet('/ft/?([0-9]+)')]
public function redirectForumTopic($response, $request, string $topicId) {
$this->redirectSimple($response, $request, $this->urls->getString('forum_topic'), $topicId);
}
#[HttpGet('/fp/?([0-9]+)')]
public function redirectForumPost($response, $request, string $postId) {
$this->redirectSimple($response, $request, $this->urls->getString('forum_post'), $postId);
}