Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

perl5 golang scala perl6

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

• 2014/04 ~ Hatena • ~2016/07 ͸ͯͳϒοΫϚʔΫ etc. (Perl) • 2016/08~ Mackerel (Scala, Golang)

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

DVSMT*-NBDLFSFMJPcHSFQFOHJOFFS <13>8&3&)*3*/(

Slide 9

Slide 9 text

NO PERL6 AT HATENA

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

ҙࣝ߹Θͤ

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

• Amon2 (Perl5) • Mojolicious (Perl5) • Play Framework (Java/Scala) • Ruby on Rails • Sinatra (Ruby) • etc, etc……

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

14(* http://plackperl.org 5

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

ʮϦΫΤετ৘ใͷ٧·ͬͨ IBTIΛड͚औͬͯɺϨεϙϯε ৘ใͷೖͬͨBSSBZSFGΛฦ͢ʯ

Slide 30

Slide 30 text

Handler Middleware %env → $@res

Slide 31

Slide 31 text

return sub { my %env = shift; return [200, [], ["body"]]; } 5

Slide 32

Slide 32 text

14(* https://github.com/supernovus/perl6-psgi

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

1MBDL http://plackperl.org 5

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

໨ࢦ͢෺

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

JNBHJOF,PTTZ

Slide 41

Slide 41 text

get '/user/:name' do "You're #{params['name']}!" end get '/' => sub { my ( $self, $c ) = @_; $c->render('index.tx', { msg => "Hello!" }); }; Sinatra Kossy

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

4JYBUSB

Slide 44

Slide 44 text

unit module MyWeb; use Sixatra; get '/user/:name', -> $c { "I am " ~ $c.param; } get '/html', -> $c { $c.render('view/page.html'); }

Slide 45

Slide 45 text

$ crustup -e \ 'use Sixatra; sixatra-app("MyWeb");'

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

ͭ͘Βͳ͍΋ͷ

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

• ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB ઀ଓ૚ • ίʔυδΣωϨʔλ See Crust::Middleware::Session See Crust::Middleware::Static ࿩୊͕ൃࢄ͢Δ ؒʹ߹Θͳ͔ͬͨ ؒʹ߹Θͳ͔ͬͨ

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

%4-

Slide 53

Slide 53 text

unit module MyWeb; use Sixatra; get '/user/:name', -> $c { "I am " ~ $c.param; } get '/html', -> $c { $c.render('view/page.html'); } ࠶ܝ

Slide 54

Slide 54 text

unit class Sixatra::Connection; has Sixatra::Request $.req; has Hash $.params; method render(Str $filename, *@args) { ... }

Slide 55

Slide 55 text

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; }

Slide 56

Slide 56 text

ϧʔςΟϯά

Slide 57

Slide 57 text

NFUIPE QBUI ˣ DPOUSPMMFS QBSBNT

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

get '/login' -> $c { $c.render('view/login-form.html'); } post '/login' -> $c { ... $c.redirect( $c.req.parameters ); }

Slide 60

Slide 60 text

get '/users/:name' -> $c { "You are " ~ $c.params; }

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

my $match = $ROUTER.match( $req.method, $req.path-info ); return 405, [], ['Method Not Allowed'] if $match; with $match { my $c = Sixatra::Connection.new( :$req, :params($match) ); my $res = $match.app.($c); ...

Slide 64

Slide 64 text

ςϯϓϨʔτ

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

method render($filename, *@args) { my $body = Template::Mojo.from-file( $filename ).render(|@args); Crust::Response.new( :status(200), :headers(...), :body([$body]) ); }

Slide 70

Slide 70 text

Ϩεϙϯε

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

• ࠷ऴతʹ͸: [$status, $headers, $body] • Crust::Response? • εςʔλείʔυ͚ͩ • จࣈྻ͚ͩ

Slide 73

Slide 73 text

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;

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

׬੒ʁ

Slide 76

Slide 76 text

unit module MyApp; use Sixatra; get '/users/:name', -> $c { $c.render('./templates/user.tm', {:name($c.params)}); }; post '/entries', -> $c { $c.redirect('/', 303); }; get '/', -> $c { $c.render('./templates/index.tm'); }; get '/403', -> $c { 403; }

Slide 77

Slide 77 text

• ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB ઀ଓ૚ • ίʔυδΣωϨʔλ See Crust::Middleware::Session See Crust::Middleware::Static ࿩୊͕ൃࢄ͢Δ ؒʹ߹Θͳ͔ͬͨ ؒʹ߹Θͳ͔ͬͨ ࠶ܝ

Slide 78

Slide 78 text

• ҆શੑ (CSRF, CORS, XSS etc…)

Slide 79

Slide 79 text

○ ॻ͍ͯ༡΂ͦ͏ × ॻ͍ͯ࢖͑ͦ͏

Slide 80

Slide 80 text

ະ׬

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

4JYBUSB https://github.com/astj/p6-Sixatra

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

5IBOLZPVGPS MJTUFOJOH

Slide 85

Slide 85 text

2VFTUJPOT