Initial commit

This commit is contained in:
Denis Fedoseev 2019-02-12 22:57:29 +03:00
commit 234c3bfbd5
3 changed files with 169 additions and 0 deletions

BIN
public/icons/copy2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

88
shrlbe.pl Normal file
View file

@ -0,0 +1,88 @@
use strict;
use warnings FATAL => 'all';
use feature qw/say/;
use Mojolicious::Lite -signatures;
use URI;
use DBIx::Struct qw/connector hash_ref_slice/;
use Data::Dumper;
my $SITE_NAME = 'shrl.be/';
my $db_file = 'shrl.db';
DBIx::Struct::connect(sprintf('dbi:SQLite:dbname=%s', $db_file),"","");
# Render template "index.html.ep" from the DATA section
get '/' => sub ($c) {
$c->render(template => 'index', page_data => {});
};
post '/' => sub ($c) {
my $url = normalize_source_url($c->param('url'));
my $shorten_path = write_url($url);
my $shorten_url = $SITE_NAME.$shorten_path;
$c->render(template => 'index', page_data => { url => $url, shorten_url => $shorten_url});
};
get '/:shorten_path' => sub ($c) {
my $path = $c->param('shorten_path');
my $url = get_url($path);
if ($url) {
$c->res->code(307);
$c->redirect_to($url);
} else {
$c->render(status => 404, text => 'Page not found :(');
}
};
app->start;
sub rand_str() {
my @set = ('0' ..'9', 'A' .. 'z', 'a'..'z');
my $str = join '' => map $set[rand @set], 1 .. 8;
return $str;
}
sub normalize_source_url($source_url) {
my $uri = URI->new($source_url);
$uri = URI->new('http://'.$source_url) if !$uri->scheme;
return $uri->as_string;
}
sub write_url($source_url) {
my $shorten_path;
eval {
my $short_row = one_row('urls',{ source_url => $source_url});
if (!$short_row) {
$shorten_path = rand_str();
new_row('urls' =>
source_url => $source_url,
shorten_path => $shorten_path,
);
} else {
$shorten_path = $short_row->shorten_path;
}
};
if ($@) { #may fall with deep recursion if no free names available
if ($@ =~ /urls.shorten_path inserting /) {
&write_url($source_url);
}
die $@;
}
return $shorten_path;
}
sub get_url($shorten_path) {
my $row = one_row('urls', { shorten_path => $shorten_path});
return $row->source_url if($row);
return;
}

81
templates/index.html.ep Normal file
View file

@ -0,0 +1,81 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css" media="screen">
.content {
width: 100%
}
.shorten_form {
width: 50%;
margin: 0 auto;
}
.title {
width: 50%;
margin: 0 auto;
padding-bottom: 10px;
}
.url_data {
width: 50%;
margin: 0 auto;
}
button.copy_short_url {
width: 32px;
height:32px;
padding: 0px;
margin: 0;
margin-left: 10px;
}
button.copy_short_url > img {
display: block;
width: 28px;
height: 28px;
padding: 0px;
margin: 0;
margin-left: -6px;
margin-top: -4px;
}
textarea.short_url_text {
width: 300px;
height: 32px;
}
</style>
</head>
<body>
<div class=content>
<div class=title>
URL Shortener
</div>
<div class=shorten_form>
<form method=post>
<input type=text name=url>
<input type=submit value=short!>
</form>
</div>
<% if (exists($page_data->{'url'})) { %>
<div class=url_data>
<p>source url: <a href='<%== $page_data->{'url'} %>'><%= $page_data->{'url'} %></a></p>
<p><textarea autofocus class=short_url_text><%= $page_data->{'shorten_url'} %></textarea><button class="copy_short_url"><img src="/icons/copy2.png"></button></p>
</div>
<% } %>
</div>
</body>
<script>
var copyTextareaBtn = document.querySelector('.copy_short_url');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.short_url_text');
copyTextarea.focus();
copyTextarea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
});
</script>
</html>