79
Hokkaido.pm#7 Yoshihiro Sasaki Dancerで Webアプリ再入門

Using Dancer

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Using Dancer

Hokkaido.pm#7Yoshihiro Sasaki

DancerでWebアプリ再入門

Page 2: Using Dancer

自己紹介

Page 3: Using Dancer

自己紹介@aloelight

Page 4: Using Dancer

自己紹介@aloelight

札幌在住

Page 5: Using Dancer

自己紹介@aloelight

札幌在住

PerlでWebアプリ開発

Page 6: Using Dancer

自己紹介@aloelight

札幌在住

PerlでWebアプリ開発

サーバ保守・運用

Page 7: Using Dancer

まず初めに

Page 8: Using Dancer

まず初めに

@riywoさん

Page 9: Using Dancer

まず初めに

@riywoさん

お越し頂きありがとうございます!

Page 10: Using Dancer

今回のテーマはWebアプリ再入門

Page 11: Using Dancer

Dancer使ってみました

Page 12: Using Dancer

AWS Health Info

Page 13: Using Dancer

AWS Health Info• Twitterアカウントでログイン

Page 14: Using Dancer

AWS Health Info• Twitterアカウントでログイン• AWS Service Healthをチェック

Page 15: Using Dancer

AWS Health Info• Twitterアカウントでログイン• AWS Service Healthをチェック•登録者にお知らせ

Page 16: Using Dancer
Page 17: Using Dancer

間に合いませんでした!てへぺろ☆(・ω<)

Page 18: Using Dancer

•表側のみ作ってあって、裏側の@送信とかないです

Page 19: Using Dancer

•表側のみ作ってあって、裏側の@送信とかないです

•でも、ドメインを勢いで取ったので後で実装します

Page 20: Using Dancer

•表側のみ作ってあって、裏側の@送信とかないです

•でも、ドメインを勢いで取ったので後で実装します

•さくらVPSで動かしてます

Page 21: Using Dancer

アジェンダ1.Dancerの紹介

2.Dancerの基本文法

3.AWS Health Infoのソースを読む

Page 22: Using Dancer

Dancerの紹介

Page 23: Using Dancer
Page 24: Using Dancer

Dancerの特徴

Page 25: Using Dancer

Dancerの特徴•Sinatra系のPerl製WAF

Page 26: Using Dancer

Dancerの特徴•Sinatra系のPerl製WAF•すっごくシンプル

Page 27: Using Dancer

Dancerの特徴•Sinatra系のPerl製WAF•すっごくシンプル•豊富なプラグイン

Page 28: Using Dancer

Dancerの特徴•Sinatra系のPerl製WAF•すっごくシンプル•豊富なプラグイン•意外と豊富なドキュメント

Page 29: Using Dancer

まずはインストールする

Page 30: Using Dancer

$ cpanm Dancer

Page 31: Using Dancer

Hello, worlduse Dancer;

get '/' => sub { return 'Hello, world';};

dance;

Page 32: Using Dancer

$ ./hello.pl$ curl localhost:5000Hello, world

Page 33: Using Dancer

簡単ですね!

Page 34: Using Dancer

もう少し複雑なプロジェクトのときは?

Page 35: Using Dancer

$ dancer -a MyApp

Page 36: Using Dancer

シンプル

Page 37: Using Dancer

シンプル

Page 38: Using Dancer

シンプル•基本は use Dancer するだけ

Page 39: Using Dancer

シンプル•基本は use Dancer するだけ• Dancer.pmがexportする関数を使う

Page 40: Using Dancer

シンプル•基本は use Dancer するだけ• Dancer.pmがexportする関数を使う•クラス構成もシンプル

Page 41: Using Dancer

EXPORTSconfigcookiedebugforwardgetlayoutparampost

redirectrequestsend_errorsetsessiontemplateuri_forその他諸々

Page 42: Using Dancer

豊富なプラグイン

Page 43: Using Dancer

豊富なプラグイン

Page 44: Using Dancer

豊富なプラグイン•Dancer::Plugin 94件

Page 45: Using Dancer

豊富なプラグイン•Dancer::Plugin 94件•Mojolicious::Plugin 102件

Page 46: Using Dancer

豊富なプラグイン•Dancer::Plugin 94件•Mojolicious::Plugin 102件• Catalyst::Plugin 320件

Page 47: Using Dancer

豊富なプラグイン•Dancer::Plugin 94件•Mojolicious::Plugin 102件• Catalyst::Plugin 320件• Amon2::Plugin 12件

Page 48: Using Dancer

メジャーなPlugin

Page 49: Using Dancer

メジャーなPlugin•Dancer::Plugin::Database

Page 50: Using Dancer

メジャーなPlugin•Dancer::Plugin::Database• Dancer::Plugin::DBIC

Page 51: Using Dancer

メジャーなPlugin•Dancer::Plugin::Database• Dancer::Plugin::DBIC• Dancer::Plugin::Email

Page 52: Using Dancer

メジャーなPlugin•Dancer::Plugin::Database• Dancer::Plugin::DBIC• Dancer::Plugin::Email• Dancer::Plugin::Thumbnail

Page 53: Using Dancer

メジャーなPlugin•Dancer::Plugin::Database• Dancer::Plugin::DBIC• Dancer::Plugin::Email• Dancer::Plugin::Thumbnail• Dancer::Plugin::Facebook

Page 54: Using Dancer

もっと知りたい方はDancer plugins ecosystem

http://advent.perldancer.org/2011/17

Page 55: Using Dancer

Pluginの作り方package Dancer::Plugin::LinkBlocker;use Dancer ':syntax';use Dancer::Plugin;

register block_links_from => sub { my $conf = plugin_setting(); my $re = join( '|', @{ $conf->{hosts} } ); before sub { if ( request->referer && request->referer =~ /$re/ ) { status 403 || $conf->{http_code}; } };};

register_plugin;1;

Page 56: Using Dancer

ドキュメント

Page 57: Using Dancer

ドキュメント

Page 58: Using Dancer

• Dancer::Introductionドキュメント

Page 59: Using Dancer

• Dancer::Introduction• Dancer::Tutorial

ドキュメント

Page 60: Using Dancer

• Dancer::Introduction• Dancer::Tutorial• Dancer::Cookbook

ドキュメント

Page 61: Using Dancer

• Dancer::Introduction• Dancer::Tutorial• Dancer::Cookbook• Dancer::Plugins

ドキュメント

Page 62: Using Dancer

• Dancer::Introduction• Dancer::Tutorial• Dancer::Cookbook• Dancer::Plugins• Dancer::Deployment

ドキュメント

Page 63: Using Dancer

httpd.conf<VirtualHost *:80> ServerName www.example.com DocumentRoot /srv/www.example.com/public ServerAdmin [email protected]

<Directory "/srv/www.example.com/public"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all AddHandler cgi-script .cgi </Directory>

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /dispatch.cgi/$1 [QSA,L]

ErrorLog /var/log/apache2/www.example.com-error.log CustomLog /var/log/apache2/www.example.com-access_log common</VirtualHost>

Page 64: Using Dancer

Dancerの基本文法

Page 65: Using Dancer

Routingget '/' => sub { return 'Hello, world';};

any ['get','post'] => '/' => sub { ...};

Page 66: Using Dancer

Routingget '/user/:name' => sub { my $name = param 'name';};

get '/user/*' => sub { my ($name) = splat;};

Page 67: Using Dancer

Request Parametersget '/' => sub { my $id = param 'id';};

post '/' => sub { my $id = param 'id'; my $file = upload('file'); # Dancer::Request::Upload object $file->copy_to('/path/to/flie');};

Page 68: Using Dancer

Request Parameters# GET /entry/foo?id=barget '/entry/:id' => sub { my $id = param 'id'; # $id => foo};

# 追記 paramsを使えばデータソース別に取れます# perldoc Dancer::Request# Fetching_only_params_from_a_given_source

Page 69: Using Dancer

Configset 'tmdpir' => '/tmp/myapp';get '/' => sub { my $config = config; return $config->{appname};};

Page 70: Using Dancer

Cookieget '/login' => sub { my $id = param 'id'; my $password = param 'password'; if ( $id eq 'cto' && $password eq 'onagatani' ) { cookie 'logged_in' => 1; redirect '/'; }};

Page 71: Using Dancer

Sessionset session => 'YAML';set session_dir => '/tmp/sessions';set session_name => 'sid';# default dancer.session

get '/login' => sub { my $id = param 'id'; my $password = param 'password'; if ( $id eq 'cto' && $password eq 'onagatani' ) { session 'user' => { name => 'onagatani' }; redirect '/'; }};

Page 72: Using Dancer

Templateset template => ‘template_toolkit’;get '/' => sub { my $id = param 'id'; template 'index', { id => $id };};

Page 73: Using Dancer

注意Dancer::Template::SimpleIF, LOOPがありません

Page 74: Using Dancer

D::T::TemplateToolkitこちらを代わりに使いましょう

Page 75: Using Dancer

Databaseuse Dancer::Plugin::Database;

get '/entry/:id' => sub { my $id = param 'id'; my $data = database->quick_select( 'entry', { id => $id } );};

Page 76: Using Dancer

Redirectget '/foo' => sub { redirect '/myaction';};

get '/bar' => sub { # internal redirect forward '/myaction';};

Page 77: Using Dancer

Loggingset logger => 'console';set log => 'debug';get '/logging' => sub { core 'foo'; debug 'bar'; info 'baz'; warning 'hoge'; error 'piyo';};

Page 78: Using Dancer

AWS Health Infoのソースを読む

Page 79: Using Dancer

ご清聴ありがとうございました