Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl 6 で Web Application Framework をつくる

81e8ac1d3a766b3d9880cd08d9a7aba4?s=47 astj
PRO
March 09, 2017

Perl 6 で Web Application Framework をつくる

YAPC::Kansai OSAKA 2017

81e8ac1d3a766b3d9880cd08d9a7aba4?s=128

astj
PRO

March 09, 2017
Tweet

Transcript

  1. YAPC::Kansai 2017/03/04 1FSMͰ8FC "QQMJDBUJPO 'SBNFXPSLΛͭ͘Δ

  2. ASTJ github: @astj Twitter: @ast_j hatena: id:astj CPAN: ASTJ

  3. perl5 golang scala perl6

  4. • p5-Test-WWW-Stub • p5-Plack-Middleware-RevisionPlate • p6-SQL-NamedPlaceholder

  5. None
  6. • 2014/04 ~ Hatena • ~2016/07 ͸ͯͳϒοΫϚʔΫ etc. (Perl) •

    2016/08~ Mackerel (Scala, Golang)
  7. None
  8. DVSMT*-NBDLFSFMJPcHSFQFOHJOFFS <13>8&3&)*3*/(

  9. NO PERL6 AT HATENA

  10. None
  11. ҙࣝ߹Θͤ

  12. ໌೔࣮ફͰ࢖͑Δ࿩͸ ͋Γ·ͤΜ

  13. ࠓ͙͢8&#։ൃͰ࢖͑Δ ϑϨʔϜϫʔΫͷ঺հΛ ͢ΔΘ͚Ͱ΋͋Γ·ͤΜ

  14. 8FCΞϓϦέʔγϣϯ։ൃͷ ίϯϙʔωϯτͷ঺հ

  15. 4JOBUSBMJLFͳ ܰྔϑϨʔϜϫʔΫͷ ίϯηϓτ࣮૷

  16. 1FSMͰ 8FCΞϓϦέʔγϣϯΛ ؾܰʹॻ͖࢝ΊΒΕΔΑ͏ʹ͢Δ

  17. None
  18. YAPC::Kansai 2017/03/04 1FSMͰ8FC "QQMJDBUJPO 'SBNFXPSLΛͭ͘Δ ࠶ܝ

  19. YAPC::Kansai 2017/03/04 1FSMͰ8FC "QQMJDBUJPO 'SBNFXPSLΛͭ͘Δ

  20. -BSSZ8BMM͕EFTJHOͨ͠ 1FSMͬΆ͍ເͷݴޠ ※ݸਓͷײ૝Ͱ͢

  21. ͨͱ͑͹ܕ͕͋Δ https://docs.perl6.org/images/type-graph-Positional.svg

  22. YAPC::Kansai 2017/03/04 1FSMͰ8FC "QQMJDBUJPO 'SBNFXPSLΛͭ͘Δ ࠶ܝ

  23. YAPC::Kansai 2017/03/04 1FSMͰ8FC "QQMJDBUJPO 'SBNFXPSLΛͭ͘Δ

  24. https://ja.wikipedia.org/wiki/WebΞϓϦέʔγϣϯϑϨʔϜϫʔΫ 8FCΞϓϦέʔγϣϯϑϨʔϜϫʔΫʢ΢ΣϒΞϓϦέʔ γϣϯϑϨʔϜϫʔΫɺӳ8FC"QQMJDBUJPO 'SBNFXPSLʣ͸ɺಈతͳ΢ΣϒαΠτɺ8FCΞϓϦέʔ γϣϯɺ8FCαʔϏεͷ։ൃΛαϙʔτ͢ΔͨΊʹઃܭ͞ ΕͨΞϓϦέʔγϣϯϑϨʔϜϫʔΫͰ͋ΔɻϑϨʔϜϫʔ Ϋͷ໨త͸ɺ8FC։ൃͰ༻͍ΒΕΔڞ௨ͨ͠࡞ۀʹ൐͏࿑ ྗΛܰݮ͢Δ͜ͱͰ͋Δɻͨͱ͑͹ɺଟ਺ͷϑϨʔϜϫʔ Ϋ͕σʔλϕʔε΁ͷΞΫηεͷͨΊͷϥΠϒϥϦ΍ɺςϯ ϓϨʔτΤϯδϯʢˠ8FCςϯϓϨʔτʣɺηογϣϯ؅

    ཧΛఏڙ͠ɺίʔυͷ࠶ར༻Λଅਐͤ͞Δ΋ͷ΋͋Δɻ
  25. • Amon2 (Perl5) • Mojolicious (Perl5) • Play Framework (Java/Scala)

    • Ruby on Rails • Sinatra (Ruby) • etc, etc……
  26. None
  27. 14(* http://plackperl.org 5

  28. PSGI is an interface between Perl web applications and web

    servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers. http://plackperl.org
  29. ʮϦΫΤετ৘ใͷ٧·ͬͨ IBTIΛड͚औͬͯɺϨεϙϯε ৘ใͷೖͬͨBSSBZSFGΛฦ͢ʯ

  30. Handler Middleware %env → $@res

  31. return sub { my %env = shift; return [200, [],

    ["body"]]; } 5
  32. 14(* https://github.com/supernovus/perl6-psgi

  33. return sub(%env) { return 200, [], ["body"]; }

  34. 1MBDL http://plackperl.org 5

  35. $SVTU https://github.com/tokuhirom/p6-Crust

  36. Crust is a set of tools for using the PSGI

    stack. It contains middleware components(TBI), and utilities for Web application frameworks. Crust is like Perl5's Plack, Ruby's Rack, Python's Paste for WSGI. https://github.com/tokuhirom/p6-Crust
  37. None
  38. ໨ࢦ͢෺

  39. 4JOBUSBMJLFͳ ܰྔϑϨʔϜϫʔΫͷ ίϯηϓτ࣮૷ ࠶ܝ

  40. JNBHJOF,PTTZ

  41. get '/user/:name' do "You're #{params['name']}!" end get '/' => sub

    { my ( $self, $c ) = @_; $c->render('index.tx', { msg => "Hello!" }); }; Sinatra Kossy
  42. None
  43. 4JYBUSB

  44. unit module MyWeb; use Sixatra; get '/user/:name', -> $c {

    "I am " ~ $c.param<name>; } get '/html', -> $c { $c.render('view/page.html'); }
  45. $ crustup -e \ 'use Sixatra; sixatra-app("MyWeb");'

  46. • ϧʔςΟϯάఆٛDSL • ศརͳϨεϙϯεͷੜ੒ • ςϯϓϨʔτΤϯδϯͱ͔

  47. • Built on Crust Toolkit • Crust::Middleware ࢖͍͍ͨͰ͠ΐʁ • ϦΫΤετύʔε͓೚͍ͤͨ͠Ͱ͠ΐʁ

    • etc……
  48. ͭ͘Βͳ͍΋ͷ

  49. • ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB

    ઀ଓ૚ • ίʔυδΣωϨʔλ kossy-setup MyApp
  50. • ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB

    ઀ଓ૚ • ίʔυδΣωϨʔλ See Crust::Middleware::Session See Crust::Middleware::Static ࿩୊͕ൃࢄ͢Δ ؒʹ߹Θͳ͔ͬͨ ؒʹ߹Θͳ͔ͬͨ
  51. None
  52. %4-

  53. unit module MyWeb; use Sixatra; get '/user/:name', -> $c {

    "I am " ~ $c.param<name>; } get '/html', -> $c { $c.render('view/page.html'); } ࠶ܝ
  54. unit class Sixatra::Connection; has Sixatra::Request $.req; has Hash $.params; method

    render(Str $filename, *@args) { ... }
  55. my class RoutingStaff { has Callable $.app; }; sub router($methods,

    $path, $app) is export { $ROUTER.add( @$methods, $path, RoutingStaff.new(:$app) ); } sub get($path, $app) is export { router ["GET", "HEAD"], $path, $app; }
  56. ϧʔςΟϯά

  57. NFUIPE QBUI  ˣ DPOUSPMMFS QBSBNT

  58. None
  59. get '/login' -> $c { $c.render('view/login-form.html'); } post '/login' ->

    $c { ... $c.redirect( $c.req.parameters<location> ); }
  60. get '/users/:name' -> $c { "You are " ~ $c.params<name>;

    }
  61. 3PVUFS#PPTU.FUIPE https://github.com/moznion/p6-Router-Boost

  62. 3PVUFS#PPN.FUIPE https://github.com/tokuhirom/Router-Boom 5

  63. my $match = $ROUTER.match( $req.method, $req.path-info ); return 405, [],

    ['Method Not Allowed'] if $match<is-method-not-allowed>; with $match<stuff> { my $c = Sixatra::Connection.new( :$req, :params($match<captured>) ); my $res = $match<stuff>.app.($c); ...
  64. ςϯϓϨʔτ

  65. • DSL (TT2) • embedded (erb, Mojo::Template…) • Logicless (Mustache)

  66. None
  67. • DSL => Template6 • embedded => Template::Mojo, etc… •

    Logicless => Template::Mustache
  68. • DSL => Template6 • embedded => Template::Mojo, etc… •

    Logicless => Template::Mustache
  69. method render($filename, *@args) { my $body = Template::Mojo.from-file( $filename ).render(|@args);

    Crust::Response.new( :status(200), :headers(...), :body([$body]) ); }
  70. Ϩεϙϯε

  71. my $match = $router.match(...); my List $res = $match(...); return

    $res;
  72. • ࠷ऴతʹ͸: [$status, $headers, $body] • Crust::Response? • εςʔλείʔυ͚ͩ •

    จࣈྻ͚ͩ
  73. my $res = $match(...); if $res !~~ Crust::Response { given

    $res { when List { return $res; } when Int { $res = Crust::Response.new(:status($res), ...); } when Str { $res = Crust::Response.new( :status(200), ... :body([$res]) ); } ... } } return $res.finalize;
  74. None
  75. ׬੒ʁ

  76. unit module MyApp; use Sixatra; get '/users/:name', -> $c {

    $c.render('./templates/user.tm', {:name($c.params<name>)}); }; post '/entries', -> $c { $c.redirect('/', 303); }; get '/', -> $c { $c.render('./templates/index.tm'); }; get '/403', -> $c { 403; }
  77. • ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB

    ઀ଓ૚ • ίʔυδΣωϨʔλ See Crust::Middleware::Session See Crust::Middleware::Static ࿩୊͕ൃࢄ͢Δ ؒʹ߹Θͳ͔ͬͨ ؒʹ߹Θͳ͔ͬͨ ࠶ܝ
  78. • ҆શੑ (CSRF, CORS, XSS etc…)

  79. ◦ ॻ͍ͯ༡΂ͦ͏ × ॻ͍ͯ࢖͑ͦ͏

  80. ະ׬

  81. • جૅతͳίϯϙʔωϯτ͸͋Δ • ૊Έ߹ΘͤΔͱγϯϓϧͳ WAF ͕Ͱ͖Δ • Perl6 ͓΋͠Ζ͍Ͱ͢Α •

    ͍͍͜ͱ͹͔ΓͰ΋ͳ͍͚Ͳ
  82. 4JYBUSB https://github.com/astj/p6-Sixatra

  83. zef install \ git://github.com/astj/p6-Sixatra.git

  84. 5IBOLZPVGPS MJTUFOJOH

  85. 2VFTUJPOT