Commit 87342e89 by PotatoGim

mojolicious: first bunches...

parent 525f59e1
#!/usr/bin/env perl
#=============================================================================
# FILE: blocks.pl
# USAGE: ./blocks.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 04:51:09 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
get '/with_block' => 'block';
app->start;
1;
__END__
=encoding utf8
=head1 NAME
blocks.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
# 아래 __DATA__ 섹션에서는 첫번째 begin과 마지막 end가 한쌍,
#
# 그리고 <%= link_to 주소 => 이름 %>에서 이름을 변수화하기 위한 begin과 end가
# 한쌍이다.
#
# 조금 이상한 부분은 두번째 begin 직후의 '%>'과 end 직전의 '<%'인데, 이
# 부분은 한 줄 내에서 코드 블록을 식별하기 위한 별도의 토큰이 필요하기 때문이
# 아닐까?
__DATA__
@@block.html.ep
% my $link = begin
% my ($url, $name) = @_;
Try <%= link_to $url => begin %><%= $name %><% end %>.
%end
<!DOCTYPE html>
<html>
<head><title>Sebastians frameworks</title></head>
<body>
%= $link->('http://mojolicio.us', 'Mojolicious')
%= $link->('http://catalystframework.org', 'Catalyst')
</body>
</html>
#!/usr/bin/env perl
#=============================================================================
# FILE: builtin_except.pl
# USAGE:
# ./builtin_except.pl get /dies "#error"
# ./builtin_except.pl get /missing "#error"
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 04:13:24 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# Not found
# 인라인 템플릿을 렌더링해서 던져준다.
# (오류에서 써먹는건가?)
get "/missing" => sub { shift->render(template => "does_not_exist") };
# Exception (500)
# die는 기본 die 동작과 마찬가지로 파일과 줄 번호 등의 오류를 던져준다.
get "/dies" => sub { die "Intentional error" };
app->start;
1;
__END__
=encoding utf8
=head1 NAME
builtin_except.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
#=============================================================================
# FILE: get_and_post_parms.pl
# USAGE: ./get_and_post_parms.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 02:11:15 AM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# /foo?user=sri
get '/foo' => sub {
my $c = shift;
my $user = $c->param("user");
$c->render(text => "Hello $user.");
};
app->start;
1;
__END__
=encoding utf8
=head1 NAME
get_and_post_parms.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
use Mojolicious::Lite;
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
get '/' => sub {
my $c = shift;
$c->render(template => 'index');
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
Welcome to the Mojolicious real-time web framework!
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
#!/usr/bin/env perl
#=============================================================================
# FILE: helpers.pl
# USAGE: ./helpers.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 05:26:36 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# A helper to identify visitors
helper whois => sub {
my $c = shift;
my $agent = $c->req->headers->user_agent || 'Anonymous';
my $ip = $c->tx->remote_address;
return "$agent ($ip)";
};
# Use helper in action and template
get '/secret' => sub {
my $c = shift;
my $user = $c->whois;
$c->app->log->debug("Request from $user");
};
app->start;
1;
__END__
=encoding utf8
=head1 NAME
helpers.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
__DATA__
@@ secret.html.ep
We know who you are <%= whois %>.
#!/usr/bin/env perl
#=============================================================================
# FILE: http.pl
# USAGE: ./http.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 02:28:06 AM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# Access request informatin
get "/agent" => sub {
my $c = shift;
my $host = $c->req->url->to_abs->host;
my $ua = $c->req->headers->user_agent;
$c->render(text => "Request by $ua reached $host.");
};
# Echo the request body and send custom header with response
post "/echo" => sub {
my $c = shift;
$c->res->headers->header("X-Bender" => "Bite my shiny metal ass!");
$c->render(data => $c->req->body);
};
app->start;
1;
__END__
=encoding utf8
=head1 NAME
http.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
#=============================================================================
# FILE: http_methods.pl
# USAGE: ./http_methods.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 06:05:56 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# GET /hello
#
# hello에 대해 GET으로 한정
get '/hello' => sub {
my $c = shift;
$c->render(text => 'Hello World!');
};
# PUT /hello
#
# hello에 대해 PUT으로 한정
put '/hello' => sub {
my $c = shift;
my $size = length($c->req->body);
$c->render(text => "You uploaded $size bytes to /hello.");
};
# GET|POST|PATCH /bye
#
# bye에 대해 GET, POST, PATCH로 한정하되 한꺼번에 정의
any [qw(GET POST PATCH)] => '/bye' => sub {
my $c = shift;
$c->render(text => 'Bye World!');
};
# * /whatever
#
# whatever에 대해 모두 가능하도록 정의
any '/whatever' => sub {
my $c = shift;
my $method = $c->req->method;
$c->render(text => "You called /whatever with $method.");
};
app->start;
1;
__END__
=encoding utf8
=head1 NAME
http_methods.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
#=============================================================================
# FILE: layouts.pl
# USAGE: ./layouts.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 04:44:07 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
get '/with_layout';
app->start;
1;
__END__
=encoding utf8
=head1 NAME
layouts.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
__DATA__
@@ with_layout.html.ep
% title 'Green';
% layout 'green';
Hello World!
@@ layouts/green.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
#!/usr/bin/env perl
#=============================================================================
# FILE: optional_placeholders.pl
# USAGE: ./optional_placeholders.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 06:11:47 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# /hello
# /hello/PotatoKim
#
# 흠... 이 경우는 기본 매개변수를 지정해주는 형태다.
# day는 입력을 별도로 받지 않고 name만 받는데, name이 없다면 'PotatoGim'이
# 기본값으로 들어간다.
get '/hello/:name' => {name => "PotatoGim", day => "Saturday"} => sub {
my $c = shift;
$c->render(template => 'groovy', format => 'txt');
};
app->start;
1;
__END__
=encoding utf8
=head1 NAME
optional_placeholders.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@gluesys.com>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
__DATA__
@@ groovy.txt.ep
My name is <%= $name %> and it is <%= $day %>.
#!/usr/bin/env perl
#=============================================================================
# FILE: placeholders.pl
# USAGE: ./placeholders.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 06/27/2015 05:39:24 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Mojolicious::Lite;
# /foo/test
# /foo/test123
#
# 이 부분은 URL의 일부로 들어온 녀석을 변수화하는 부분으로 보인다.
# 실제로 구동해보면 test, test123이 각각 $bar에 들어가게 된다.
get '/foo/:bar' => sub {
my $c = shift;
my $bar = $c->stash('bar');