Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbestigrou committed Nov 12, 2010
0 parents commit 1305bcc
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 0 deletions.
5 changes: 5 additions & 0 deletions MahewinSexyUrl.pl
@@ -0,0 +1,5 @@
#!/usr/bin/env perl
use Dancer;
use lib path(dirname(__FILE__), 'lib');
load_app 'MahewinSexyUrl';
dance;
21 changes: 21 additions & 0 deletions Makefile.PL
@@ -0,0 +1,21 @@
use strict;
use warnings;
use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'MahewinSexyUrl',
AUTHOR => q{YOUR NAME <youremail@example.com>},
VERSION_FROM => 'lib/MahewinSexyUrl.pm',
ABSTRACT => 'YOUR APPLICATION ABSTRACT',
($ExtUtils::MakeMaker::VERSION >= 6.3002
? ('LICENSE'=> 'perl')
: ()),
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'YAML' => 0,
'Dancer' => 1.1904,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'MahewinSexyUrl-*' },
);
10 changes: 10 additions & 0 deletions config.yml
@@ -0,0 +1,10 @@
layout: "main"
logger: "file"
appname: "MahewinSexyUrl"
template: template_toolkit
charset: utf-8̈́
plugins:
DBIC:
db:
dsn: dbi:SQLite:dbname=./msu.db

8 changes: 8 additions & 0 deletions environments/development.yml
@@ -0,0 +1,8 @@
log: "core"
warnings: 1
show_errors: 1

# auto_reload is a development feature
# you should enable it by yourself if you want it
# Module::Refresh is needed
auto_reload: 0
7 changes: 7 additions & 0 deletions environments/production.yml
@@ -0,0 +1,7 @@
log: "warning"
warnings: 0
show_errors: 0
route_cache: 1
# never enable auto_reload in production
auto_reload: 0

117 changes: 117 additions & 0 deletions lib/MahewinSexyUrl.pm
@@ -0,0 +1,117 @@
package MahewinSexyUrl;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Data::Uniqid qw ( luniqid );

our $VERSION = '0.1';

get '/' => sub {
template 'index';
};

post '/' => sub {
my $long_url = create_sexy_url(params->{sexy_url});

if ( ! $long_url ) {

my $search_long_url = schema('db')->resultset('Url')->find({
long_url => params->{sexy_url},
});
return template 'index.tt', {
message => "This long url exist, and whouah! It's very beautiful $search_long_url->{_column_data}->{sexy_url}",
};
}

return template 'index.tt', {
message => "Whouah ! This a very beautiful url :) $long_url"
};
};

post '/get_long_url' => sub {
my $sexy_url = get_long_url(params->{long_url});

if ( ! $sexy_url ) {
return template 'index.tt', {
message => "This long url is not here."
};
}

return template 'index.tt', {
message => "The long url is $sexy_url"
};
};

get '/:sexy_url' => sub {
my $search_sexy_url = schema('db')->resultset('Url')->find({
sexy_url => params->{sexy_url},
});

if ( ! $search_sexy_url ) {
return send_error("This sexy url not exist, sorry", 404);
}

redirect $search_sexy_url->{_column_data}->{long_url};
};

=head1 create_sexy_url
This function is to create a sexy_url. Use luniqid from Data::Uniqid.
=cut

sub create_sexy_url {
my ( $long_url ) = @_;

my $search_long_url = schema('db')->resultset('Url')->find({
long_url => $long_url,
});

if ( $search_long_url ) {
return 0;
}

my $id = luniqid;
my $size = int(rand(10));
my $sexy_url = substr($id, $size);

my $search_sexy_url = schema('db')->resultset('Url')->find({
sexy_url => $sexy_url,
});

$sexy_url .= 1 if ( $search_sexy_url );

schema('db')->resultset('Url')->create({
long_url => $long_url,
sexy_url => $sexy_url
});

return $sexy_url;
}

=head1 get_long_url
This function give you a long_url of the sexy_url.
=cut

sub get_long_url {
my ( $sexy_url ) = @_;

my $search_sexy_url = schema('db')->resultset('Url')->find({
sexy_url => $sexy_url,
});

if ( ! $search_sexy_url ) {
return 0;
}

return $search_sexy_url->{_column_data}->{long_url};
}

=head1 AUTHOR
Natal Ngétal
=cut

true;
19 changes: 19 additions & 0 deletions lib/Model/DB/Schema.pm
@@ -0,0 +1,19 @@
package Model::DB::Schema;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Schema';

__PACKAGE__->load_namespaces;


# Created by DBIx::Class::Schema::Loader v0.07002 @ 2010-11-11 18:16:04
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:AOo0y7nJCWzhcQ7DoKkfoQ


# You can replace this text with custom content, and it will be preserved on regeneration
1;
58 changes: 58 additions & 0 deletions lib/Model/DB/Schema/Result/Url.pm
@@ -0,0 +1,58 @@
package Model::DB::Schema::Result::Url;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';


=head1 NAME
Model::DB::Schema::Result::Url
=cut

__PACKAGE__->table("url");

=head1 ACCESSORS
=head2 id
data_type: 'integer'
is_auto_increment: 1
is_nullable: 0
=head2 long_url
data_type: 'varchar'
is_nullable: 0
size: 250
=head2 sexy_url
data_type: 'varchar'
is_nullable: 0
size: 30
=cut

__PACKAGE__->add_columns(
"id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"long_url",
{ data_type => "varchar", is_nullable => 0, size => 250 },
"sexy_url",
{ data_type => "varchar", is_nullable => 0, size => 30 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.07002 @ 2010-11-11 18:16:04
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:cp9zO3rdrwmp6DxHnPC1+Q


# You can replace this text with custom content, and it will be preserved on regeneration
1;
11 changes: 11 additions & 0 deletions msu.sql
@@ -0,0 +1,11 @@
-- --------------------------------------------------------
-- `url`
-- --------------------------------------------------------

DROP TABLE IF EXISTS `url`;

CREATE TABLE url (
id INTEGER PRIMARY KEY,
long_url VARCHAR(250) NOT NULL,
sexy_url VARCHAR(30) NOT NULL
);
17 changes: 17 additions & 0 deletions public/404.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Error 404</title>
<link rel="stylesheet" href="/css/error.css" />
<meta charset=UTF-8" />
</head>
<body>
<h1>Error 404</h1>
<div id="content">
<h2>Page Not Found</h2><p>Sorry, this is the void.</p>
</div>
<footer>
Powered by <a href="http://perldancer.org/">Dancer</a> 1.1904
</footer>
</body>
</html>
17 changes: 17 additions & 0 deletions public/500.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Error 500</title>
<link rel="stylesheet" href="/css/error.css" />
<meta charset=UTF-8" />
</head>
<body>
<h1>Error 500</h1>
<div id="content">
<h2>Internal Server Error</h2><p>Wooops, something went wrong</p>
</div>
<footer>
Powered by <a href="http://perldancer.org/">Dancer</a> 1.1904
</footer>
</body>
</html>
70 changes: 70 additions & 0 deletions public/css/error.css
@@ -0,0 +1,70 @@
body {
font-family: Lucida,sans-serif;
}

h1 {
color: #AA0000;
border-bottom: 1px solid #444;
}

h2 { color: #444; }

pre {
font-family: "lucida console","monaco","andale mono","bitstream vera sans mono","consolas",monospace;
font-size: 12px;
border-left: 2px solid #777;
padding-left: 1em;
}

footer {
font-size: 10px;
}

span.key {
color: #449;
font-weight: bold;
width: 120px;
display: inline;
}

span.value {
color: #494;
}

/* these are for the message boxes */

pre.content {
background-color: #eee;
color: #000;
padding: 1em;
margin: 0;
border: 1px solid #aaa;
border-top: 0;
margin-bottom: 1em;
}

div.title {
font-family: "lucida console","monaco","andale mono","bitstream vera sans mono","consolas",monospace;
font-size: 12px;
background-color: #aaa;
color: #444;
font-weight: bold;
padding: 3px;
padding-left: 10px;
}

pre.content span.nu {
color: #889;
margin-right: 10px;
}

pre.error {
background: #334;
color: #ccd;
padding: 1em;
border-top: 1px solid #000;
border-left: 1px solid #000;
border-right: 1px solid #eee;
border-bottom: 1px solid #eee;
}

0 comments on commit 1305bcc

Please sign in to comment.