| 1 | package Net::Lujoyglamour::WebApp; | ||
| 2 | |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | our $VERSION = sprintf "%d.%03d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/g; | ||
| 8 | |||
| 9 | 2 | ▶ | use lib qw( ../../../lib ../../lib |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | /home/jmerelo/proyectos/CPAN/Net-Lujoyglamour/lib/); #Just in case we are testing it in-place | |
| 11 | |||
| 12 | 2 | ▶ | use base qw/DBIx::Class::Schema/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | use base 'CGI::Application'; | ||
| 15 | |||
| 16 | use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); | ||
| 17 | use CGI::Application::Plugin::DBIx::Class ':all'; | ||
| 18 | use CGI::Application::Plugin::Redirect; | ||
| 19 | use Net::Lujoyglamour qw(is_valid); | ||
| 20 | use JSON; | ||
| 21 | |||
| 22 | sub cgiapp_init { | ||
| 23 | my $self = shift; | ||
| 24 | my (%args ) = @_; | ||
| 25 | croak "No DSN" if !$args{'PARAMS'}->{'dsn'}; | ||
| 26 | croak "No domain" if !$args{'PARAMS'}->{'domain'}; | ||
| 27 | $self->param('domain', $args{'PARAMS'}->{'domain'} ); | ||
| 28 | |||
| 29 | my %params = %{$args{'PARAMS'}}; | ||
| 30 | $self->dbh_config($params{'dsn'}, | ||
| 31 | $params{'username'}, | ||
| 32 | $params{'auth'}); | ||
| 33 | $self->dbic_config({schema => Net::Lujoyglamour->connect( $params{'dsn'}, | ||
| 34 | $params{'username'}, | ||
| 35 | $params{'auth'})}); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | # ( setup() can even be skipped for common cases. See docs below. ) | ||
| 40 | sub setup { | ||
| 41 | my $self = shift; | ||
| 42 | $self->start_mode('form'); | ||
| 43 | $self->mode_param('rm'); | ||
| 44 | $self->run_modes( | ||
| 45 | 'form' => 'show_form', | ||
| 46 | 'geturl' => 'get_url', | ||
| 47 | 'redirect' => 'redirect_url' | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub show_form { | ||
| 52 | my $self = shift; | ||
| 53 | my $tmpl; | ||
| 54 | eval { | ||
| 55 | $tmpl = $self->load_tmpl; | ||
| 56 | }; | ||
| 57 | croak( "Can't load template from ".$self->{'__TMPL_PATH'}.". Error $@\n" ) if $@; | ||
| 58 | $tmpl->param( domain => $self->param('domain') ); | ||
| 59 | return $tmpl->output; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub get_url { | ||
| 63 | my $self = shift; | ||
| 64 | my $tmpl = $self->load_tmpl; | ||
| 65 | my $long_url = $self->query->param('longurl'); | ||
| 66 | my $short_url = $self->query->param('shorturl'); | ||
| 67 | my $new_short_url = ''; | ||
| 68 | eval { | ||
| 69 | $new_short_url = $self->schema->create_new_short( $long_url, $short_url ); | ||
| 70 | }; | ||
| 71 | if ( $@ ) { | ||
| 72 | croak "Error when retrieving short URL: $@"; | ||
| 73 | } | ||
| 74 | my $format = $self->query->param('fmt') || ''; | ||
| 75 | if ( $format eq '' ) { | ||
| 76 | if ($new_short_url ne '') { | ||
| 77 | $tmpl->param( short => $self->param('domain')."/".$new_short_url, | ||
| 78 | long => $long_url ); | ||
| 79 | } else { | ||
| 80 | $tmpl->param( msg => $@ ); | ||
| 81 | } | ||
| 82 | return $tmpl->output; | ||
| 83 | } elsif ( $format eq 'JSON' ) { | ||
| 84 | my $json; | ||
| 85 | if ($new_short_url ne '') { | ||
| 86 | $json = to_json( { shortu => $self->param('domain')."/".$new_short_url, | ||
| 87 | longu => $long_url} ); | ||
| 88 | } else { | ||
| 89 | $json = to_json( {msg => $@} ); | ||
| 90 | } | ||
| 91 | $self->header_props(-type=>'application/json'); | ||
| 92 | return $json; | ||
| 93 | } | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | sub redirect_url { | ||
| 98 | my $self = shift; | ||
| 99 | my $url = $self->param('url'); | ||
| 100 | my $long_url = $self->schema->get_long_for( $url ); | ||
| 101 | if ( $long_url ) { | ||
| 102 | return $self->redirect("http://".$long_url ); | ||
| 103 | } else { | ||
| 104 | my $tmpl = $self->load_tmpl; | ||
| 105 | $tmpl->param( domain => $self->param('domain'), | ||
| 106 | shorturl => $url ); | ||
| 107 | return $tmpl->output; | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | Net::Lujoyglamour::WebApp - Use URL shortener from the web | ||
| 117 | |||
| 118 | |||
| 119 | =head1 SYNOPSIS | ||
| 120 | |||
| 121 | use Net::Lujoyglamour; | ||
| 122 | |||
| 123 | my $app = new Net::Lujoyglamour::WebApp | ||
| 124 | PARAMS => { dsn => $dsn}, | ||
| 125 | TMPL_PATH => '/luxury/path/to/templates'; | ||
| 126 | |||
| 127 | $app->run(); | ||
| 128 | |||
| 129 | If you want this to work as an URL redirector, you'll have to first | ||
| 130 | have mod_rewrite activated, and then write something like this in | ||
| 131 | your .htaccess | ||
| 132 | RewriteEngine on | ||
| 133 | Options +FollowSymlinks | ||
| 134 | DirectoryIndex /cgi-bin/lg/lg.cgi | ||
| 135 | RewriteRule ^(\w+)$ /cgi-bin/lg/lg.cgi?rm=redirect&url=$1 | ||
| 136 | |||
| 137 | Change, ofcourse, that path to the path your CGI resides. | ||
| 138 | |||
| 139 | =head1 DESCRIPTION | ||
| 140 | |||
| 141 | Configure a L<CGI::Application> module for using L<Net::Lujoyglamour> | ||
| 142 | from the web. | ||
| 143 | |||
| 144 | =head1 INTERFACE | ||
| 145 | |||
| 146 | Mostly intended for using internally; only thing you need to know is | ||
| 147 | C<run> and C<new>; however, here's the skinny | ||
| 148 | |||
| 149 | =head2 new( PARAMS => { dsn => $dsn }, | ||
| 150 | TMPL_PATH => '/More/glamourous/templates' ) | ||
| 151 | |||
| 152 | The DSN must point to a working database, and the templates to a | ||
| 153 | directory containing C<form.html> and C<geturl.html> | ||
| 154 | templates. Examples can be found in the distribution, and you can | ||
| 155 | create your own taking into account that: | ||
| 156 | |||
| 157 | =over 4 | ||
| 158 | |||
| 159 | =item C<form.html> must include the form and all its fields, including | ||
| 160 | the hidden field | ||
| 161 | |||
| 162 | =item C<geturl.html> must include all the L<HTML::Template> commands, | ||
| 163 | which will be substituted by variables or an error message | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =head2 run | ||
| 168 | |||
| 169 | Start to run the web loop | ||
| 170 | |||
| 171 | The rest are included mostly to avoid complains from pod-coverage | ||
| 172 | |||
| 173 | =head2 cgiapp_init | ||
| 174 | |||
| 175 | Called internally from C<new> to inialize DB and whatever is needed | ||
| 176 | for the application | ||
| 177 | |||
| 178 | =head2 get_url | ||
| 179 | |||
| 180 | Called from the C<geturl> runmode to obtain a new short URL | ||
| 181 | |||
| 182 | =head2 redirect_url | ||
| 183 | |||
| 184 | Called from the C<redirect> runmode to retrieve a short URL from the | ||
| 185 | DB and redirect it | ||
| 186 | |||
| 187 | =head2 setup | ||
| 188 | |||
| 189 | Setting up run modes | ||
| 190 | |||
| 191 | =head2 show_form | ||
| 192 | |||
| 193 | Shows the form to obtain a new URL | ||
| 194 | |||
| 195 | |||
| 196 | =head1 DIAGNOSTICS | ||
| 197 | |||
| 198 | See L<Net::Lujoyglamour> for errors | ||
| 199 | |||
| 200 | =head1 CONFIGURATION AND ENVIRONMENT | ||
| 201 | |||
| 202 | See above; don't forget to configure a database and the two | ||
| 203 | templates mentioned above. | ||
| 204 | |||
| 205 | =head1 DEPENDENCIES | ||
| 206 | |||
| 207 | L<CGI::Application> dependencies, plus | ||
| 208 | L<CGI::Application::Plugin::DBIx::Class> used for integration with | ||
| 209 | L<DBIx::Class> and L<CGI::Application::Plugin::Redirect> | ||
| 210 | redirection. | ||
| 211 | |||
| 212 | =head1 INCOMPATIBILITIES | ||
| 213 | |||
| 214 | None reported. | ||
| 215 | |||
| 216 | |||
| 217 | =head1 BUGS AND LIMITATIONS | ||
| 218 | |||
| 219 | No bugs have been reported. | ||
| 220 | |||
| 221 | Please report any bugs or feature requests to | ||
| 222 | C<bug-net-lujoyglamour@rt.cpan.org>, or through the web interface at | ||
| 223 | L<http://rt.cpan.org>. | ||
| 224 | |||
| 225 | |||
| 226 | =head1 AUTHOR | ||
| 227 | |||
| 228 | JJ Merelo C<< <jj@merelo.net> >> | ||
| 229 | |||
| 230 | |||
| 231 | =head1 LICENCE AND COPYRIGHT | ||
| 232 | |||
| 233 | Copyright (c) 2009, JJ Merelo C<< <jj@merelo.net> >>. All rights reserved. | ||
| 234 | |||
| 235 | This module is free software; you can redistribute it and/or | ||
| 236 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 237 | |||
| 238 | |||
| 239 | =head1 DISCLAIMER OF WARRANTY | ||
| 240 | |||
| 241 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY | ||
| 242 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN | ||
| 243 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES | ||
| 244 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER | ||
| 245 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 246 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE | ||
| 247 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH | ||
| 248 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL | ||
| 249 | NECESSARY SERVICING, REPAIR, OR CORRECTION. | ||
| 250 | |||
| 251 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | ||
| 252 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR | ||
| 253 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE | ||
| 254 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, | ||
| 255 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE | ||
| 256 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING | ||
| 257 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A | ||
| 258 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF | ||
| 259 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF | ||
| 260 | SUCH DAMAGES. | ||