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

Web API for Perl 6 - The Perl Conference NA 2016

Web API for Perl 6 - The Perl Conference NA 2016

20e28cb713a93e12e1fb95828979f341?s=128

Sterling Hanenkamp

June 22, 2016
Tweet

More Decks by Sterling Hanenkamp

Other Decks in Technology

Transcript

  1. Web API for Perl 6 Sterling Hanenkamp Software Engineer 1

  2. Yes, we're hiring. We’re also happy to help you find

    a job somewhere else…
 https:/ /www.ziprecruiter.com Thanks for the plane ticket! 2
  3. P6W:Perl6::PSGI:Perl 3

  4. Easy Peasey Just Translate from P5 to P6 WRONG! 4

  5. hello.p6w use v6; sub app(%env) { start { 200, [

    Content-Type => 'text/plain' ], [ 'Hello World' ] } } 5
  6. Small Print All code samples in this presentation are non-normative

    and may not reflect the reality of the current specification or any other revision to the specification. They are for demonstration purposes only and they may not even compile. 6
  7. Perl 6: Making the World a Better Place Since Christmas

    CONCURRENCY Composable Concurrency Objects Promise Supply Automatic Type Coercion 7
  8. Goals Standardize the app/server API No 6PAN requirements for apps

    Simplicity Flexibility to optimize Flexibility to extend 8
  9. Implied Objectives Common tasks should be easy Unusual/unanticipated tasks should

    be possible and even portable Low-level API within a high-level language Making the policy choices required to solve the problem and no others. 9
  10. Layers Layer 1: Middleware Layer 2: Application Layer 0: Server

    All Running Concurrently 10
  11. Environment %env passed to the application as an Associative Contains

    all the CGI-ish keys as well as custom p6w.* ones. Similar to PSGI but Asynchronous (maybe synchronous too). 11
  12. A Response, I Promise use v6; sub app(%env) { start

    { 200, [ Content-Type => 'text/plain' ], [ 'Hello World' ] } } 12
  13. A Response-ish, I Promise use v6; sub app(%env) { supply

    { emit [ 200, [ Content-Type => 'text/plain' ], [ 'Hello World' ] ]; }.Promise; } 13
  14. Status/Headers Int (or coerces to an Int) List of Pairs

    (or something listy that contains something pairy) 14
  15. Message Payload Supply .emit('blah blah blah HTML blah'); .done; .quit(X::Adhoc.new(:message('bad

    stuff'));
 OR within on, die "bad stuff"; Or anything else that can .Supply! 15
  16. Message Payload use v6; sub app(%env) { start { 200,

    [ Content-Type => 'text/plain' ], [ 'Hello World' ] } } 16
  17. Message Payload use v6; sub app(%env) { start { 200,

    [ Content-Type => 'text/plain' ], Supply.from-list(['Hello World' ]) } } 17
  18. Message Payload use v6; sub app(%env) { start { 200,

    [ Content-Type => 'text/plain' ], ['Hello World' ].Supply } } 18
  19. Stringify-Encode Blobs are passed through as-is Arrays are treated as

    headers, encoded using ISO-8859-1 Anything else: stringify and encode using p6w.encode OR via detected Content-Type charset 19
  20. P6W > PSGI 20

  21. P6W < PSGI (more accurately) 21

  22. PSGI Delayed Response my $app = sub { my $env

    = shift; # Delays response until it fetches content from the network return sub { my $responder = shift; fetch_content_from_server(sub { my $content = shift; $responder->([ 200, $headers, [ $content ] ]); }); }; }; 22
  23. P6SGI Delayed Response my &app = sub (%env) { #

    Delays response until it fetches content from the network start { my $content = fetch-content-from-server(); 200, $headers, [ $content ] } } 23
  24. PSGI Streaming my $app = sub { my $env =

    shift; # immediately starts the response and stream the content return sub { my $responder = shift; my $writer = $responder->( [ 200, [ 'Content-Type', 'application/json' ]]); wait_for_events(sub { my $new_event = shift; if ($new_event) { $writer->write($new_event->as_json . "\n"); } else { $writer->close; } }); }; }; 24
  25. P6W Streaming my &app = sub (%env) { # immediately

    starts the response and stream the content start { my $events = wait-for-events(); 200, [ 'Content-Type' => 'application/json' ], $events.map({ .as-json ~ "\n" }); } } 25
  26. PSGI Middleware # $app is a simple PSGI application my

    $app = sub { my $env = shift; return [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ]; }; # $xheader is a piece of middleware that wraps $app my $xheader = sub { my $env = shift; my $res = $app->($env); push @{$res->[1]}, 'X-PSGI-Used' => 1; return $res; }; 26
  27. P6W Middleware # &app is a simple PSGI application my

    &app = sub (%env) { 200, [ Content-Type => 'text/plain' ], [ "Hello World" ] } # &xheader is a piece of middleware that wraps some &app my &xheader = sub (%env) { nextsame.then(-> $r { my ($s, @h, $p) = $r.result; @h.push: 'P6SGI-Used' => 1; $s, @h, $p }); } &app.wrap(&xheader); 27
  28. Middleware: Payload sub debug-line-lengths(%env) { nextsame.then({ my ($s, $h, Supply()

    $p) = .result; $s, $h, $p.lines.map({ .chars ~ "\n" }) }); } 28
  29. Input: Supplies! use v6; sub app(%env) { start { my

    $form-str = ''; %env<p6w.input>.tap: -> $v { $form-str ~= $v }; %env<p6w.input>.wait; # Yeah, this is naïve. Get over it. my %params = |$form-str.split('&')».split('='); 200, [ Content-Type => 'text/html' ], do-search(%params<q>); } } 29
  30. Request Life Cycle Server MUST start the app ASAP The

    Server MUST begin sending the response from the App ASAP It is generally assumed that the application and server must run concurrently. (This is Perl 6, not some neolithic language without a decent threading model, after all.) Request Srv Application Response 30
  31. Request Life Cycle my $expect-continue = %env<SERVER_PROTOCOL> ~~ 'HTTP/1.1'|'HTTP/2' &&

    %env<HTTP_EXPECT> eq '100-continue'; if $expect-continue { 100, [ ], on -> $out { my ($s, @h, Supply() $b) = process-input(); @h.push: ':status' => $s; $out.emit(@h); $b.do: -> $v { $out.emit($v) {; }; } 31
  32. Raw HTTP/2 # Look, ma, no p6w.io! sub app(%env) {

    start { if %env<HTTP_CONNECTION> ~~ /\<Upgrade\>/ && %env<HTTP_UPGRADE> eq 'h2c' { 101, [ Connection => 'Upgrade', Upgrade => 'h2c' ], supply { # beginning reading HTTP/2 frames and output HTTP/2 frames # using p6w.input and p6w.output }; } else { ... } } } 32
  33. HTTP/2 Extension if %env<SERVER_PROTOCOL> eq 'HTTP/1.1' && %env<HTTP_CONNECTION> ~~ /\<Upgrade\>/

    && %env<HTTP_UPGRADE> eq 'h2c' && any(|%env<p6w.protocol.upgrade>) ~~ 'h2c' { 101, [ P6SGIx-Upgrade => 'h2c' ], [] } if %env<PATH_INFO> eq '/index.html' { if %env<SERVER_PROTOCOL> eq 'HTTP/2' { %env<p6w.h2c.push-promise>.emit([ ':method' => 'GET', ':scheme' => 'http', ':path' => 'foo.png', ]); } 200, [ 'Content-Type' => 'text/html' ], ... } elsif %env<PATH_INFO> eq '/foo.png' { 200, [ 'Content-Type' => 'image/png' ], ... } 33
  34. WebSocket Extension if %env<SERVER_PROTOCOL> eq 'HTTP/1.1' && %env<HTTP_CONNECTION> ~~ /\<Upgrade\>/

    && %env<HTTP_UPGRADE> eq 'websocket' && any(|%env<p6w.protocol.upgrade>) ~~ 'websocket' { 101, [ P6Wx-Upgrade => 'websocket' ], [] } # echo service elsif %env<SERVER_PROTOCOL> eq 'websocket' { supply { whenever %env<p6w.input> -> $v { emit $v; LAST { done } }; }; } 34
  35. Protocol Handling The overall specification assumes HTTP/1.x So far, provides

    special case handling for others: HTTP/2 (adds p6sgix.h2c.push-promise) WebSocket 35
  36. Custom Protocol Implementation New protocols can be implemented at any

    level They can be implemented according to specified extension They can be implemented some other way 36
  37. Ready Check sub app(%env) { start { 200, [ Content-Type

    => 'text/plain' ], supply { await %env<p6w.ready>; for 0 .. * -> $n { emit $n ~ "\n" } }; } } 37
  38. Header/Body Done Extensions sub app(%env) { start { 200, [

    Content-Type => 'text/plain' ], supply { await %env<p6wx.header.done>; emit("HEADER WRITTEN OUT YAY!"); done; await %env<p6wx.body.done>; %env<p6w.errors>.emit("BODY WRITTEN OUT YAY!"); # TODO Lame. Come up with something better. } } } 38
  39. Output Block Detection Extension sub app(%env) { start { my

    $fh = "really-big-file.txt".IO.open(:r); 200, [ Content-Type => 'text/plain' ], supply { whenever %env<p6wx.body.backpressure.supply> -> $yes { sleep 10 if $yes; } whenever $fh -> $chars { # throws an exception when this promise is broken %env<p6wx.body.done>.result if %env<p6wx.body.done>.status ~~ Broken; emit $chars; } } } } 39
  40. PSGI Extensions Sessions Logger Harikiri Cleanup 40

  41. More Details https:/ /github.com/zostay/P6W/ https:/ /github.com/zostay/P6W/tree/ master/eg https:/ /speakerdeck.com/zostay/perl-6- standard-gateway-interface-ppw-2015

    41
  42. Contributing https:/ /github.com/zostay/P6W/ https:/ /github.com/zostay/Smack/ #p6sgi on Freenode #perl6 on

    Freenode My name is zostay on Freenode/irc.perl.org 42
  43. Questions? 43