Hyperlink-Redirect/lib/Hyperlink-Redirect.rakumod

43 lines
1.3 KiB
Raku
Raw Normal View History

2023-09-23 00:44:32 -04:00
use Humming-Bird::Core;
2023-10-08 10:45:47 -04:00
use Humming-Bird::Middleware;
use Humming-Bird::Advice;
2023-09-24 00:45:18 -04:00
use Template6;
use Base64;
use Libarchive::Filter :gzip;
2023-09-23 00:44:32 -04:00
# Normally would 'use' local libs here for Controller and Model and
# what not but keeping it simple for now...
2023-09-24 00:52:15 -04:00
# Set things up (config stuff would go here?)
2023-09-24 00:45:18 -04:00
my $templates = Template6.new;
$templates.add-path: 'templates';
2023-10-08 10:45:47 -04:00
# Logging
middleware &middleware-logger;
advice &advice-logger;
2023-09-23 00:44:32 -04:00
# Must set the root path lest yet miss setting $!root
my $router = Router.new(root => '/');
$router.get(-> $request, $response {
2023-09-24 00:45:18 -04:00
$response.html($templates.process: 'index');
2023-09-23 00:44:32 -04:00
});
2023-10-07 15:15:55 -04:00
$router.post(-> $request, $response {
2023-10-07 22:48:48 -04:00
my $return-url = $request.content.{'hyperlink'};
my $url-scheme = $request.headers.{'X-Forwarded-Proto'} || 'http';
my $url-host = $request.headers.{'Host'};
my $base-url = $url-scheme ~ '://' ~ $url-host ~ '/';
my $hyperlink = $base-url ~ encode-base64(gzip($return-url), :str);
$response.html($templates.process: 'index', :$hyperlink);
2023-10-07 15:15:55 -04:00
});
# Try a wildcard to catch 'all' path
$router.get('/**', -> $request, $response {
2023-10-07 22:48:48 -04:00
my $return-url = $request.path.substr(1); # Omits the leading slash
my $redirect-url = gunzip(decode-base64($return-url, :bin));
2023-10-07 15:15:55 -04:00
2023-10-07 22:48:48 -04:00
$response.redirect($redirect-url);
2023-10-07 15:15:55 -04:00
});