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

Perl 6 で Web Application Framework をつくる

astj
March 09, 2017

Perl 6 で Web Application Framework をつくる

YAPC::Kansai OSAKA 2017

astj

March 09, 2017
Tweet

More Decks by astj

Other Decks in Technology

Transcript

  1. • Amon2 (Perl5) • Mojolicious (Perl5) • Play Framework (Java/Scala)

    • Ruby on Rails • Sinatra (Ruby) • etc, etc……
  2. 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
  3. 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
  4. get '/user/:name' do "You're #{params['name']}!" end get '/' => sub

    { my ( $self, $c ) = @_; $c->render('index.tx', { msg => "Hello!" }); }; Sinatra Kossy
  5. unit module MyWeb; use Sixatra; get '/user/:name', -> $c {

    "I am " ~ $c.param<name>; } get '/html', -> $c { $c.render('view/page.html'); }
  6. • ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB

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

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

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

    "I am " ~ $c.param<name>; } get '/html', -> $c { $c.render('view/page.html'); } ࠶ܝ
  10. 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; }
  11. get '/login' -> $c { $c.render('view/login-form.html'); } post '/login' ->

    $c { ... $c.redirect( $c.req.parameters<location> ); }
  12. 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); ...
  13. method render($filename, *@args) { my $body = Template::Mojo.from-file( $filename ).render(|@args);

    Crust::Response.new( :status(200), :headers(...), :body([$body]) ); }
  14. 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;
  15. 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; }
  16. • ϑΟϧλ (before, after) • ηογϣϯ • ੩తϑΝΠϧ഑৴ • DB

    ઀ଓ૚ • ίʔυδΣωϨʔλ See Crust::Middleware::Session See Crust::Middleware::Static ࿩୊͕ൃࢄ͢Δ ؒʹ߹Θͳ͔ͬͨ ؒʹ߹Θͳ͔ͬͨ ࠶ܝ