From 234c3bfbd51de0b0e0163aec818b191539160fcc Mon Sep 17 00:00:00 2001 From: Denis Fedoseev Date: Tue, 12 Feb 2019 22:57:29 +0300 Subject: [PATCH] Initial commit --- public/icons/copy2.png | Bin 0 -> 1358 bytes shrlbe.pl | 88 ++++++++++++++++++++++++++++++++++++++++ templates/index.html.ep | 81 ++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 public/icons/copy2.png create mode 100644 shrlbe.pl create mode 100644 templates/index.html.ep diff --git a/public/icons/copy2.png b/public/icons/copy2.png new file mode 100644 index 0000000000000000000000000000000000000000..d72fa583aa30bb3830cb016e644483a1b0d32f57 GIT binary patch literal 1358 zcmeAS@N?(olHy`uVBq!ia0vp^GeDSw4M<8HQcwg^k|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*9U+SUz~VIEGZ*dNbG7N5oO2LA);@LPCmLSeVo8sgx2c=R%={ zo0^*VI>Nmdm6aSXE%7=LD&nm)SM7qw(@b{{m5Bi!3XK;8xVbr7)gtC>-l2AU!}G#> z=g!^#t~&W_+F8TxHI<($^3%_Z^DkV#{=7zZ&EdeE zavg`=FCY1J`sU5Z4VyO?>&<$-#abhL`^loKo3?HhJ^P)r>89&Nww+lVvD4O^J;?Db zRwyUBCrF|#yY0;SsP~g*JT?+MQ}g6SYEO)vo6+-V*J&r7u1Wc#G|@-XhX0yM=JigN zzp)bX+ zr(OPYxX{6U?wJk_vG>t`WbR7{movB)YjsuHZQt3F=rngtY;#QDlTAtY7d=?|>Zx1J zzZqU9OZ2B(Db0BQTg?B-?}b4|cP%5IS`L8w=loBF88Wxl-rPQ;E#=W~xz58I)K1Q;RG*Q% zL?v>H*q7s$!P6F{&iie4=AcW&-szvt-wz4WsQHv2;jj|u;~n^Utka_6_wXHzCm zo-DrZv(1dUSDhyD?lukvpl~W?ns+ikZj*xJil4J;&SpqG^;sLI-XwT3LTMt;L57nj z4wgJiVP_8h?78>!AD76NEl=((=lQK9+nJ*IO)pZ3@uUC`P)*F)lS(I#Scn_Ej$6W0 zYVdDYN@vUe>1V$FpUrXbi?`S>6-mn(ZF6*{>0J1z$N4J3#b}zxc?nk|7VpWyFKgIN z%A7iArhckA^533==KfD@8AUbIPjv@|+4O$6cxd;B*LNRIvtJRYKgnuFotW^V8AW;d z`+4(fBhx#-G$b1?EA=QV{?FW0c_{D0^Q}RvBfF;k{yJI4$JuX*@uaiM=H+i)WKo!TEB>#p1cRHq=( zFxzs5<6aNdH&N4^1wq*Zl9@n^1A;GH(|GrKFt9MQFfS0FY57Ho5t7ZI%C;WIRKsh!-yZQZ-{Y42Id^SJWO64S>{%+t>0{l({%24GvhFBG3yJIPgF2y6q;`ZMd&n~ '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; +} + diff --git a/templates/index.html.ep b/templates/index.html.ep new file mode 100644 index 0000000..c10db1f --- /dev/null +++ b/templates/index.html.ep @@ -0,0 +1,81 @@ + + + + + + + +
+
+ URL Shortener +
+
+
+ + +
+
+ <% if (exists($page_data->{'url'})) { %> +
+

source url: '><%= $page_data->{'url'} %>

+

+
+ <% } %> +
+ + + \ No newline at end of file