diff --git a/public/assets/awaki-logo.png b/public/assets/awaki-logo.png new file mode 100644 index 0000000..fb2c10f Binary files /dev/null and b/public/assets/awaki-logo.png differ diff --git a/public/assets/style.css b/public/assets/style.css new file mode 100644 index 0000000..d7c7401 --- /dev/null +++ b/public/assets/style.css @@ -0,0 +1,70 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; + position: relative; +} + +html, +body { + width: 100%; + height: 100%; +} + +body { + background-color: #111; + color: #fff; + font: 12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif; +} + +.awaki { + width: 100%; + height: 100%; + display: flex; + justify-content: flex-start; + align-items: center; + flex-direction: column; +} + +.awaki a { + color: #d18c83; + text-decoration: none; +} +.awaki a:hover, +.awaki a:focus { + text-decoration: underline; +} +.awaki a:active { + color: #99403d; +} + +.awaki-inner { + margin: auto; +} + +.awaki-header { + margin: 20px 0; + padding: 0 20px; +} + +.awaki-logo { + text-align: center; +} +.awaki-logo img { + max-width: 425px; + width: 100%; +} +.awaki-description { + text-align: center; +} + +.awaki-footer { + font-size: .8em; + opacity: .6; + text-align: center; + margin: 20px 0; + padding: 0 20px; +} +.awaki-footer a { + color: inherit; +} diff --git a/public/err404.html b/public/err404.html new file mode 100644 index 0000000..559f63a --- /dev/null +++ b/public/err404.html @@ -0,0 +1,26 @@ + + + + + + Error 404 + + + +
+
+
+ +
+

Couldn't find what you were looking for

+
+
+ +
+
+ + diff --git a/public/err500.html b/public/err500.html new file mode 100644 index 0000000..3a4cb87 --- /dev/null +++ b/public/err500.html @@ -0,0 +1,26 @@ + + + + + + Error 500 + + + +
+
+
+ +
+

Something horrendously went wrong! Please report this if the error persists.

+
+
+ +
+
+ + diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..9295f1f --- /dev/null +++ b/public/index.html @@ -0,0 +1,26 @@ + + + + + + Awaki + + + +
+
+
+ +
+

Short URL Service for Flashii

+
+
+ +
+
+ + diff --git a/public/index.php b/public/index.php index 101f5ae..2e34b8a 100644 --- a/public/index.php +++ b/public/index.php @@ -1,42 +1,9 @@ use('/', function($response) { - $response->setPoweredBy('Awaki+Index'); -}); - -$router->get('/', function() { - $body = ''; - $body .= 'Awaki'; - $body .= 'Redirect service - OK'; - return $body; -}); - -$router->get('/:id', function($response, $request, $id) use ($db) { - $getInfo = $db->prepare('SELECT `redir_url` FROM `awk_redirects` WHERE `redir_id` = ? OR `redir_vanity` = ?'); - $getInfo->addParameter(1, $id, DbType::INTEGER); - $getInfo->addParameter(2, $id, DbType::STRING); - $getInfo->execute(); - $info = $getInfo->getResult(); - - if(!$info->next()) - return 404; - - $targetUrl = $info->getString(0); - - $params = $request->getParamString(); - if(!empty($params)) - $targetUrl .= '?' . $params; - - $response->redirect($targetUrl); -}); - -$router->dispatch(); +$awk->setUpHttp(); +$awk->dispatchHttp( + \Index\Http\HttpRequest::fromRequest() +); diff --git a/src/AwakiContext.php b/src/AwakiContext.php index bcb1f1b..25e8a42 100644 --- a/src/AwakiContext.php +++ b/src/AwakiContext.php @@ -5,11 +5,18 @@ use Index\Data\IDbConnection; use Index\Data\Migration\IDbMigrationRepo; use Index\Data\Migration\DbMigrationManager; use Index\Data\Migration\FsDbMigrationRepo; +use Index\Http\HttpFx; +use Index\Http\HttpRequest; +use Index\Routing\IRouter; + +// theme colours: #99403d, #592824, #d18c83 +// totally didn't just eyedrop musujime class AwakiContext { private const DB_INIT = 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'; private IDbConnection $dbConn; + private HttpFx $router; public function __construct(IDbConnection $dbConn) { $this->dbConn = $dbConn; @@ -28,4 +35,37 @@ class AwakiContext { public function createMigrationRepo(): IDbMigrationRepo { return new FsDbMigrationRepo(AWK_DIR_DBM); } + + public function getRouter(): IRouter { + return $this->router->getRouter(); + } + + public function setUpHttp(): void { + $this->router = new HttpFx; + $this->router->use('/', function($response) { + $response->setPoweredBy('Awaki'); + }); + + $this->registerErrorPages(); + $this->registerHttpRoutes(); + } + + public function dispatchHttp(?HttpRequest $request = null): void { + $this->router->dispatch($request); + } + + private function registerErrorPages(): void { + $this->router->addErrorHandler(404, function($response) { + $response->accelRedirect('/err404.html'); + $response->setTypeHTML(); + }); + $this->router->addErrorHandler(500, function($response) { + $response->accelRedirect('/err500.html'); + $response->setTypeHTML(); + }); + } + + private function registerHttpRoutes(): void { + new RedirectorRoutes($this->router, $this->dbConn); + } } diff --git a/src/RedirectorRoutes.php b/src/RedirectorRoutes.php new file mode 100644 index 0000000..4e842ce --- /dev/null +++ b/src/RedirectorRoutes.php @@ -0,0 +1,41 @@ +dbConn = $dbConn; + + $router->get('/', [$this, 'index']); + $router->get('/:id', [$this, 'redirect']); + } + + public function index($response): void { + $response->accelRedirect('/index.html'); + $response->setTypeHTML(); + } + + public function redirect($response, $request, $id) { + $getInfo = $this->dbConn->prepare('SELECT redir_url FROM awk_redirects WHERE redir_id = ? OR redir_vanity = ?'); + $getInfo->addParameter(1, $id, DbType::INTEGER); + $getInfo->addParameter(2, $id, DbType::STRING); + $getInfo->execute(); + $info = $getInfo->getResult(); + + if(!$info->next()) + return 404; + + $targetUrl = $info->getString(0); + + $params = $request->getParamString(); + if(!empty($params)) + $targetUrl .= '?' . $params; + + $response->redirect($targetUrl); + } +}