2023-09-23 04:44:32 +00:00
|
|
|
use Humming-Bird::Core;
|
2023-10-08 14:45:47 +00:00
|
|
|
use Humming-Bird::Middleware;
|
|
|
|
use Humming-Bird::Advice;
|
2023-11-01 04:52:19 +00:00
|
|
|
use Template::Mustache;
|
2023-10-08 01:10:33 +00:00
|
|
|
use Base64;
|
|
|
|
use Libarchive::Filter :gzip;
|
2023-09-23 04:44:32 +00:00
|
|
|
|
|
|
|
# Normally would 'use' local libs here for Controller and Model and
|
|
|
|
# what not but keeping it simple for now...
|
|
|
|
|
2023-09-24 04:52:15 +00:00
|
|
|
# Set things up (config stuff would go here?)
|
2023-11-01 04:52:19 +00:00
|
|
|
my $template = Template::Mustache.new: :from<../templates>;
|
2023-09-24 04:45:18 +00:00
|
|
|
|
2023-10-08 14:45:47 +00:00
|
|
|
# Logging
|
|
|
|
middleware &middleware-logger;
|
|
|
|
advice &advice-logger;
|
|
|
|
|
2023-09-23 04:44:32 +00:00
|
|
|
# Must set the root path lest yet miss setting $!root
|
|
|
|
my $router = Router.new(root => '/');
|
|
|
|
|
|
|
|
$router.get(-> $request, $response {
|
2023-11-01 04:52:19 +00:00
|
|
|
my Str %stash = title => 'Create new hyperlink';
|
|
|
|
|
|
|
|
$response.html($template.render: 'index', %stash);
|
2023-09-23 04:44:32 +00:00
|
|
|
});
|
2023-10-07 19:15:55 +00:00
|
|
|
|
|
|
|
$router.post(-> $request, $response {
|
2023-11-03 05:04:45 +00:00
|
|
|
my Str $return-url = $request.content.<hyperlink>;
|
|
|
|
my Bool $meta-refresh = $request.content.<meta-refresh>.defined;
|
|
|
|
my Str $url-scheme = $request.headers.<X-Forwarded-Proto> || 'http';
|
|
|
|
my Str $url-host = $request.headers.<Host>;
|
2023-11-03 04:14:08 +00:00
|
|
|
my (Str $base-url, Str $hyperlink, Str %stash);
|
|
|
|
|
2023-11-03 16:54:11 +00:00
|
|
|
$base-url = $url-scheme ~ '://' ~ $url-host ~
|
|
|
|
($meta-refresh ?? '/--meta-refresh/' !! '/');
|
2023-11-03 04:14:08 +00:00
|
|
|
|
|
|
|
$hyperlink = $base-url ~ encode-base64(gzip($return-url), :str);
|
|
|
|
|
2023-11-03 05:04:45 +00:00
|
|
|
%stash = title => 'New hyperlink created', :$hyperlink;
|
2023-10-08 01:10:33 +00:00
|
|
|
|
2023-11-01 04:52:19 +00:00
|
|
|
$response.html($template.render: 'index', %stash);
|
2023-10-07 19:15:55 +00:00
|
|
|
});
|
|
|
|
|
2023-11-03 04:14:08 +00:00
|
|
|
# Process the hyperlink
|
|
|
|
$router.get('/--meta-refresh/**', -> $request, $response {
|
|
|
|
my Str $return-url = $request.path.subst: /^ '/--meta-refresh/'/, Empty;
|
|
|
|
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
|
2023-11-03 05:04:45 +00:00
|
|
|
my Str %stash = title => 'Hyperlinking...', :$redirect-url;
|
2023-11-03 04:14:08 +00:00
|
|
|
|
|
|
|
$response.html($template.render: 'index', %stash);
|
|
|
|
});
|
|
|
|
|
2023-10-07 19:15:55 +00:00
|
|
|
$router.get('/**', -> $request, $response {
|
2023-11-01 01:54:43 +00:00
|
|
|
my Str $return-url = $request.path.substr(1); # Omits the leading slash
|
|
|
|
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
|
2023-10-07 19:15:55 +00:00
|
|
|
|
2023-10-08 02:48:48 +00:00
|
|
|
$response.redirect($redirect-url);
|
2023-10-07 19:15:55 +00:00
|
|
|
});
|