Updated index, 404 and 500 pages on the URL redirect service.

This commit is contained in:
flash 2023-01-07 22:32:28 +00:00
parent 1c38976b95
commit cdd879093d
8 changed files with 233 additions and 37 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

70
public/assets/style.css Normal file
View file

@ -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;
}

26
public/err404.html Normal file
View file

@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Error 404</title>
<link href="/assets/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="awaki">
<div class="awaki-inner">
<div class="awaki-header">
<div class="awaki-logo">
<img src="/assets/awaki-logo.png" alt="Awaki">
</div>
<div class="awaki-description">
<p>Couldn't find what you were looking for</p>
</div>
</div>
<div class="awaki-footer">
<a href="https://flash.moe" target="_blank" rel="noopener">Flashwave</a> 2022-2023
</div>
</div>
</div>
</body>
</html>

26
public/err500.html Normal file
View file

@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Error 500</title>
<link href="/assets/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="awaki">
<div class="awaki-inner">
<div class="awaki-header">
<div class="awaki-logo">
<img src="/assets/awaki-logo.png" alt="Awaki">
</div>
<div class="awaki-description">
<p>Something horrendously went wrong! Please <a href="/bugs">report</a> this if the error persists.</p>
</div>
</div>
<div class="awaki-footer">
<a href="https://flash.moe" target="_blank" rel="noopener">Flashwave</a> 2022-2023
</div>
</div>
</div>
</body>
</html>

26
public/index.html Normal file
View file

@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Awaki</title>
<link href="/assets/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="awaki">
<div class="awaki-inner">
<div class="awaki-header">
<div class="awaki-logo">
<img src="/assets/awaki-logo.png" alt="Awaki">
</div>
<div class="awaki-description">
<p>Short URL Service for <a href="/home" rel="noopener">Flashii</a></p>
</div>
</div>
<div class="awaki-footer">
<a href="https://flash.moe" target="_blank" rel="noopener">Flashwave</a> 2022-2023
</div>
</div>
</div>
</body>
</html>

View file

@ -1,42 +1,9 @@
<?php
namespace Awaki;
use Index\StringBuilder;
use Index\Data\DbType;
use Index\Http\HttpFx;
require_once __DIR__ . '/../awaki.php';
$router = new HttpFx;
$router->use('/', function($response) {
$response->setPoweredBy('Awaki+Index');
});
$router->get('/', function() {
$body = '<!doctype html>';
$body .= '<title>Awaki</title>';
$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()
);

View file

@ -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);
}
}

41
src/RedirectorRoutes.php Normal file
View file

@ -0,0 +1,41 @@
<?php
namespace Awaki;
use Index\Data\IDbConnection;
use Index\Data\DbType;
use Index\Routing\IRouter;
final class RedirectorRoutes {
private IDbConnection $dbConn;
public function __construct(IRouter $router, IDbConnection $dbConn) {
$this->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);
}
}