URL shortener: Difference between revisions

(→‎{{header|Perl 6}}: bug fix, make more easily configurable)
(→‎{{header|Perl 6}}: Style tweaks)
Line 82:
use JSON::Fast;
 
my $urlfile = './urls.json'.IO;
my %urls = ($urlfile.IO.e and $urlfile.IO.f and $urlfile.IO.s) ??
( $urlfile.IO.slurp.&from-json ) !!
( index => 1, url => { 0 => 'http://rosettacode.org/wiki/URL_shortener#Perl_6' } );
 
$urlfile.IO.spurt(%urls.&to-json);
 
# Setup parameters
Line 98:
<input type="submit" value="Submit"></form>
HTML
;
 
# Micro HTTP service
Line 121 ⟶ 120:
 
get -> $short {
(if my $link = retrieve(%urls<url>{$short))} ??{
( redirect :permanent, $link) !!
not-found}
else {
not-found
}
}
}
 
my Cro::Service $shorten = Cro::HTTP::Server.new:
:host($host), :port($port), :$application;
 
$shorten.start;
 
react whenever signal(SIGINT) { $shorten.stop; exit; }
 
sub retrieve ($short) { %urls<url>{$short} }
 
sub store ($url) {
%urls<url>{ %urls<index>.base(36) } = $url;
++%urls<index>;
$urlfile.IO.spurt(%urls.&to-json);
}</lang>
10,333

edits