Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9e2c759
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use MooseX::Declare; | ||
use Unshorten::Model; | ||
use POE; | ||
use HTTP::Engine::Role; # for type constraints | ||
use feature 'say'; | ||
|
||
class Unshorten with MooseX::Getopt with MooseX::Runnable with HTTP::Engine::Role { | ||
has '_engine_type' => ( is => 'ro', default => 'POE' ); | ||
|
||
has 'dsn' => ( | ||
is => 'ro', | ||
isa => 'Str', | ||
required => 1, | ||
default => 'hash', | ||
documentation => 'DSN of the KiokuDB', | ||
); | ||
|
||
has 'model' => ( | ||
traits => ['NoGetopt'], | ||
is => 'ro', | ||
isa => 'Unshorten::Model', | ||
lazy_build => 1, | ||
); | ||
|
||
method _build_model() { | ||
return Unshorten::Model->new( dsn => $self->dsn ); | ||
} | ||
|
||
method handle_request(HTTP::Engine::Request $req){ | ||
|
||
my $short = URI->new(substr $req->uri->path, 1); | ||
|
||
my $long = eval { $self->model->unshorten($short) }; | ||
if(!$long || $@){ | ||
return HTTP::Engine::Response->new( | ||
code => 500, | ||
content_type => 'text/plain', | ||
body => "An error occurred while shortening '$short': $@", | ||
); | ||
} | ||
|
||
return HTTP::Engine::Response->new( | ||
content_type => 'text/plain', | ||
body => $long, | ||
); | ||
} | ||
|
||
method run() { | ||
$self->engine->run; | ||
say "Starting server on port ". $self->port; | ||
POE::Kernel->run; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use MooseX::Declare; | ||
|
||
class Unshorten::Model extends KiokuX::Model { | ||
use Moose::Util::TypeConstraints; | ||
use MooseX::Types::URI qw(Uri); | ||
|
||
use AnyEvent::HTTP qw(http_head); | ||
|
||
method unshorten(Uri $url) { | ||
return $self->_lookup_or_cache($url, sub { | ||
$self->_lookup(@_), | ||
}); | ||
} | ||
|
||
method _lookup(Uri $url) { | ||
my $done = AnyEvent->condvar; | ||
|
||
http_head $url, sub { | ||
my ($data, $headers) = @_; | ||
my $long_url = eval { URI->new($headers->{URL}) }; | ||
confess 'Failed to get a URL' unless $url; | ||
$done->send($long_url); | ||
}; | ||
|
||
return $done->recv; | ||
} | ||
|
||
method _lookup_or_cache(Uri $url, CodeRef $shortener) { | ||
my $scope = $self->new_scope; | ||
my $shortened = $self->lookup("$url"); | ||
return $shortened if $shortened; | ||
|
||
$shortened = $shortener->($url); | ||
|
||
# "Unknown reason" because the shortener should die if it knows | ||
# the reason | ||
confess "Unable to shorten '$url': Unknown reason" | ||
unless $shortened; | ||
|
||
$self->store("$url" => $shortened); | ||
|
||
return $shortened; | ||
} | ||
|
||
}; |