Hyperlink-Redirect/lib/Hyperlink-Redirect.rakumod

64 lines
2.0 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-11-01 00:52:19 -04:00
use Template::Mustache;
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-11-13 21:13:20 -05:00
# UPDATE: We now have local lib (sry libs)
use Hyperlink-Redirect::Helpers;
2023-09-23 00:44:32 -04:00
2023-09-24 00:52:15 -04:00
# Set things up (config stuff would go here?)
2023-11-01 00:52:19 -04:00
my $template = Template::Mustache.new: :from<../templates>;
2023-09-24 00:45:18 -04:00
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-11-01 00:52:19 -04:00
my Str %stash = title => 'Create new hyperlink';
$response.html($template.render: 'index', %stash);
2023-09-23 00:44:32 -04:00
});
2023-10-07 15:15:55 -04:00
$router.post(-> $request, $response {
my Str $return-url = fix-protocol($request.content<hyperlink>);
2023-11-03 13:20:12 -04:00
my Bool $meta-refresh = $request.content<meta-refresh>.defined;
2023-11-13 21:13:20 -05:00
my Str $url-scheme = $request.headers<x-forwarded-proto> || 'http';
2023-11-13 21:15:26 -05:00
my Str $url-host = $request.headers<host>;
2023-11-03 00:14:08 -04:00
my (Str $base-url, Str $hyperlink, Str %stash);
2023-11-03 12:54:11 -04:00
$base-url = $url-scheme ~ '://' ~ $url-host ~
($meta-refresh ?? '/--meta-refresh/' !! '/');
2023-11-03 00:14:08 -04:00
2023-11-13 21:38:00 -05:00
$hyperlink = $base-url ~ hyperlink $return-url;
2023-11-03 00:14:08 -04:00
2023-11-03 01:04:45 -04:00
%stash = title => 'New hyperlink created', :$hyperlink;
2023-11-01 00:52:19 -04:00
$response.html($template.render: 'index', %stash);
2023-10-07 15:15:55 -04:00
});
2023-11-14 00:35:06 -05:00
# Don't try to process favicon as a hyperlink
$router.get('/favicon.ico', -> $request, $response {
$response.status(204)
});
2023-11-03 00:14:08 -04:00
# Process the hyperlink
$router.get('/--meta-refresh/**', -> $request, $response {
my Str $return-url = $request.path.subst: /^ '/--meta-refresh/'/, Empty;
2023-11-13 21:38:00 -05:00
my Str $redirect-url = redirect $return-url;
2023-11-03 01:04:45 -04:00
my Str %stash = title => 'Hyperlinking...', :$redirect-url;
2023-11-03 00:14:08 -04:00
$response.html($template.render: 'index', %stash);
});
2023-10-07 15:15:55 -04:00
$router.get('/**', -> $request, $response {
my Str $return-url = $request.path.substr(1); # Omits the leading slash
2023-11-13 21:38:00 -05:00
my Str $redirect-url = redirect $return-url;
2023-11-13 21:15:26 -05:00
2023-10-07 22:48:48 -04:00
$response.redirect($redirect-url);
2023-10-07 15:15:55 -04:00
});