]> Dogcows Code - chaz/talk-introduction-to-psgi/blob - slides.html
fef0bc2a56e47c841dfd48358d97a74927100f8d
[chaz/talk-introduction-to-psgi] / slides.html
1 <!DOCTYPE html>
2 <html><head><meta charset="utf-8"><title>Introduction to PSGI</title><link rel="stylesheet" href="css/common.css"><link rel="stylesheet" href="css/slides.css"></head><body><textarea id="source">
3
4 class: center, middle
5 name: title
6
7 # Introduction to PSGI
8
9 Charles McGarvey
10
11 ---
12
13 class: center, middle
14 name: bluehost
15
16 ![Bluehost](img/bluehost.png)
17
18 ### https://bluehost.com/careers
19
20 ---
21
22 ## Agenda
23
24 - Answer "What is PSGI?"
25 - Examine some alternatives to PSGI.
26 - Examine PSGI.
27 - Examine Plack.
28
29 ---
30
31 ## What is PSGI?
32
33 ### **P**erl [web] **S**erver **G**ateway **I**nterface
34
35 --
36 - It is an interface between Perl web applications and web servers.
37
38 --
39 - It is a *specification*, not code.
40
41 --
42 - First released to the CPAN on 13 Oct 2009.
43
44 --
45 - Originally written by Miyagawa.
46
47 .center[![Tatsuhiko Miyagawa](img/avatar-miyagawa.jpg)]
48
49 ???
50 Written by **Tatsuhiko Miyagawa**, author of:
51 - cpanm
52 - carton
53 - way too many Perl modules on CPAN to list
54
55 --
56 - Inspired by WSGI (Python) and Rack (Ruby).
57
58 ???
59 - PEP-333 (WSGI 1.0) was released on 07 Dec 2003.
60 - Rack 0.1 was released on 03 Mar 2007.
61
62 Despite Perl's long history of powering the web, we were well behind the curve on this.
63
64 ---
65
66 class: center, middle
67
68 ## Extremely High-level Overview
69
70 .basic-flow[
71 ![Basic Flow](img/basic-flow1.svg)
72 ]
73
74 ???
75 - PSGI is the language your app speaks in order to communicate with user agents.
76 - User agents are browsers.
77 - I've glossed over some important details, like the fact that users don't speak PSGI.
78
79 ---
80
81 class: center, middle
82
83 ## Pretty High-level Overview
84
85 .basic-flow[
86 ![Basic Flow](img/basic-flow2.svg)
87 ]
88
89 ???
90 - In reality, your app speaks PSGI but user agents speak HTTP.
91 - You need software in the middle that can speak both, and that's usually a web server.
92
93 ---
94
95 class: center, middle
96
97 ## Somewhat High-level Overview
98
99 .basic-flow[
100 ![Basic Flow](img/basic-flow3.svg)
101 ]
102
103 ???
104 - In reality, most web servers don't speak PSGI. :-(
105 - There are "handlers" that speak both PSGI and another language that web servers do speak.
106 - CGI
107 - mod_perl
108 - FastCGI
109 - **HTTP**
110 - Yes, HTTP. Many modern web servers speak HTTP not only as a server but also as a client.
111 - This allows them to *proxy* (act as middleman) between user agents and other servers.
112 - In the world of PSGI (and "Plack"), handlers are also called adapters or connectors.
113 - There are already adapters for every major web server.
114 - Apache
115 - nginx
116 - IIS
117
118 ---
119
120 class: center, middle
121
122 ## Somewhat High-level Overview
123
124 .basic-flow[
125 ![Basic Flow](img/basic-flow4.svg)
126 ]
127
128 ???
129 - Notice that as we've zoomed in, the interaction between the web app and the rest has remained the
130 same: PSGI.
131 - From a developer perspective, the etremely high-level overview is sufficient.
132 - This is one of the benefits of PSGI:
133 - Write your application once and leave deployment details to devops.
134 - The intrastructure details can change (swap in a different web server) and the app will still work.
135 - Maybe you're both developer and system architect, but the separation between developer and
136 devops is valuable.
137 - In fairness, this isn't a new concept.
138 - The way this has been achieved before is using a *web framework*.
139
140 ---
141
142 class: center, middle
143
144 ## High-level Overview
145
146 .basic-flow[
147 ![Basic Flow](img/basic-flow5.svg)
148 ]
149
150 ???
151 - A web framework makes it so your app doesn't need to speak HTTP or CGI or even PSGI.
152 -
153
154 ---
155
156 class: middle
157
158 ## Word of Caution
159
160 > Writing your web application directly using [PSGI/Plack] is certainly possible but not recommended.
161 >
162 > […]
163 >
164 > If you're writing a web application, not a framework, then you're encouraged to use one of the web
165 > application frameworks that support PSGI (http://plackperl.org/#frameworks), or see modules like
166 > HTTP::Engine to provide higher level Request and Response API on top of PSGI.
167 >
168 > -- [Plack::Request Documentation](https://search.cpan.org/~miyagawa/Plack/lib/Plack/Request.pm)
169
170 ???
171 - When you start learning about Plack, you'll realize that it is actually pretty capable.
172 - You may start to think that everything should be developed on that level -- don't do it!
173 - For most stuff, you'll still want to use a web framework.
174 - Web frameworks often offer more convenient abstractions than raw PSGI.
175
176 ---
177
178 ## Why care?
179
180 ???
181 If you're a developer writing a web app, you're probably asking why then you should care about PSGI.
182
183 --
184
185 - So that you can understand how things work.
186
187 ???
188 - I'll never understood people who don't want to understand things.
189 - Knowledge is cool!
190 - I recommend you learn and understand as much as you can about the entire request-response cycle of
191 your web app; it will help you troubleshoot when things go wrong or aren't behaving as expected.
192 - Be an expert!
193
194 --
195 - So that you can do DevOps (if you want).
196
197 ???
198 - New skills make you more marketable.
199
200 --
201 - So that you it when you see PSGI exposed through your web framework.
202
203 --
204 - You can do cool things with PSGI!
205
206 ???
207 - Even if you do most of your work using your framework, you can do some useful things with PSGI.
208 - We will get to some of those cool things, so hang tight.
209
210 ---
211
212 class: http
213 layout: true
214
215 ## HTTP
216
217 ---
218
219 ### Hypertext Transfer Protocol
220
221 --
222 - Invented by Tim Berners-Lee in 1989, first specified as [HTTP 0.9](https://www.w3.org/Protocols/HTTP/AsImplemented.html) in 1991.
223
224 --
225 - The IETF and W3C took over standards development, resulting in [RFC 1945](https://tools.ietf.org/html/rfc1945) ("HTTP 1.0") in 1996.
226
227 --
228 - [RFC 2068](https://tools.ietf.org/html/rfc2068) ("HTTP 1.1") happened in 1997, superceded by [RFC 2616](https://tools.ietf.org/html/rfc2616) in 1999.
229
230 ???
231 RFC 2616 was then superceded in 2014 by:
232 - [RFC 7230](https://tools.ietf.org/html/rfc7230)
233 - [RFC 7231](https://tools.ietf.org/html/rfc7231)
234 - [RFC 7232](https://tools.ietf.org/html/rfc7232)
235 - [RFC 7233](https://tools.ietf.org/html/rfc7233)
236 - [RFC 7234](https://tools.ietf.org/html/rfc7234)
237 - [RFC 7235](https://tools.ietf.org/html/rfc7235)
238
239 --
240 - Oh yeah, and HTTP2 came out in 2015, defined in [RFC 7540](https://tools.ietf.org/html/rfc7540).
241
242 ---
243
244 .col[
245 ### Request
246
247 ```http
248 GET /ip HTTP/1.1
249 User-Agent: Mozilla/5.0 (X11; Linux x86_64)
250 Host: foo.acme.tld
251 Accept-Language: en-us
252 Connection: Keep-Alive
253 ```
254 ]
255
256 .col[
257 ### Response
258
259 ```http
260 HTTP/1.1 200 OK
261 Date: Thu, 07 Jul 2016 11:56:23 GMT
262 Server: nginx
263 Content-Length: 30
264 Content-Type: text/plain
265 Connection: Closed
266
267 Your IP address is 127.0.0.1.
268 ```
269 ]
270
271 ---
272
273 .col[
274 ### Request
275
276 ```http
277 *GET /ip HTTP/1.1
278 User-Agent: Mozilla/5.0 (X11; Linux x86_64)
279 Host: foo.acme.tld
280 Accept-Language: en-us
281 Connection: Keep-Alive
282 ```
283 ]
284
285 .col[
286 ### Response
287
288 ```http
289 HTTP/1.1 200 OK
290 Date: Thu, 07 Jul 2016 11:56:23 GMT
291 Server: nginx
292 Content-Length: 30
293 Content-Type: text/plain
294 Connection: Closed
295
296 Your IP address is 127.0.0.1.
297 ```
298 ]
299
300 .col[
301 1. Method, path, protocol/version
302 ]
303
304 ???
305 - Methods defined in HTTP/1.1: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT
306 - RFC 5789 defined PATCH in March 2010.
307
308 ---
309
310 .col[
311 ### Request
312
313 ```http
314 GET /ip HTTP/1.1
315 *User-Agent: Mozilla/5.0 (X11; Linux x86_64)
316 *Host: foo.acme.tld
317 *Accept-Language: en-us
318 *Connection: Keep-Alive
319 ```
320 ]
321
322 .col[
323 ### Response
324
325 ```http
326 HTTP/1.1 200 OK
327 Date: Thu, 07 Jul 2016 11:56:23 GMT
328 Server: nginx
329 Content-Length: 30
330 Content-Type: text/plain
331 Connection: Closed
332
333 Your IP address is 127.0.0.1.
334 ```
335 ]
336
337 .col[
338 1. Method, path, protocol/version
339 2. Headers (key-value pairs)
340 ]
341
342 ---
343
344 .col[
345 ### Request
346
347 ```http
348 GET /ip HTTP/1.1
349 User-Agent: Mozilla/5.0 (X11; Linux x86_64)
350 Host: foo.acme.tld
351 Accept-Language: en-us
352 Connection: Keep-Alive
353 ```
354 ]
355
356 .col[
357 ### Response
358
359 ```http
360 HTTP/1.1 200 OK
361 Date: Thu, 07 Jul 2016 11:56:23 GMT
362 Server: nginx
363 Content-Length: 30
364 Content-Type: text/plain
365 Connection: Closed
366
367 Your IP address is 127.0.0.1.
368 ```
369 ]
370
371 .col[
372 1. Method, path, protocol/version
373 2. Headers (key-value pairs)
374 3. Optional document (or "body")
375 ]
376
377 ---
378
379 .col[
380 ### Request
381
382 ```http
383 GET /ip HTTP/1.1
384 User-Agent: Mozilla/5.0 (X11; Linux x86_64)
385 Host: foo.acme.tld
386 Accept-Language: en-us
387 Connection: Keep-Alive
388 ```
389 ]
390
391 .col[
392 ### Response
393
394 ```http
395 *HTTP/1.1 200 OK
396 Date: Thu, 07 Jul 2016 11:56:23 GMT
397 Server: nginx
398 Content-Length: 30
399 Content-Type: text/plain
400 Connection: Closed
401
402 Your IP address is 127.0.0.1.
403 ```
404 ]
405
406 .col[
407 1. Method, path, protocol/version
408 2. Headers (key-value pairs)
409 3. Optional document (or "body")
410 ]
411
412 .col[
413 1. Protocol/version, status code, reason phrase
414 ]
415
416 ---
417
418 .col[
419 ### Request
420
421 ```http
422 GET /ip HTTP/1.1
423 User-Agent: Mozilla/5.0 (X11; Linux x86_64)
424 Host: foo.acme.tld
425 Accept-Language: en-us
426 Connection: Keep-Alive
427 ```
428 ]
429
430 .col[
431 ### Response
432
433 ```http
434 HTTP/1.1 200 OK
435 *Date: Thu, 07 Jul 2016 11:56:23 GMT
436 *Server: nginx
437 *Content-Length: 30
438 *Content-Type: text/plain
439 *Connection: Closed
440
441 Your IP address is 127.0.0.1.
442 ```
443 ]
444
445 .col[
446 1. Method, path, protocol/version
447 2. Headers (key-value pairs)
448 3. Optional document (or "body")
449 ]
450
451 .col[
452 1. Protocol/version, status code, reason phrase
453 2. Headers (key-value pairs)
454 ]
455
456 ---
457
458 .col[
459 ### Request
460
461 ```http
462 GET /ip HTTP/1.1
463 User-Agent: Mozilla/5.0 (X11; Linux x86_64)
464 Host: foo.acme.tld
465 Accept-Language: en-us
466 Connection: Keep-Alive
467 ```
468 ]
469
470 .col[
471 ### Response
472
473 ```http
474 HTTP/1.1 200 OK
475 Date: Thu, 07 Jul 2016 11:56:23 GMT
476 Server: nginx
477 Content-Length: 30
478 Content-Type: text/plain
479 Connection: Closed
480
481 *Your IP address is 127.0.0.1.
482 ```
483 ]
484
485 .col[
486 1. Method, path, protocol/version
487 2. Headers (key-value pairs)
488 3. Optional document (or "body")
489 ]
490
491 .col[
492 1. Protocol/version, status code, reason phrase
493 2. Headers (key-value pairs)
494 3. Optional document (or "body")
495 ]
496
497 ---
498
499 layout: false
500
501 ## Alternatives to PSGI
502
503 - CGI
504 - mod_perl
505 - FastCGI
506 - SCGI
507 - WSGI
508 - JSGI
509 - Rack
510 - ISAPI
511 - many more...
512
513
514 ???
515 - All of these still exist, and actually all of these are still in common use.
516 - We're going to take a closer look at three of these.
517
518 ---
519
520 ## Alternatives to PSGI
521
522 - .highlight[CGI]
523 - .highlight[mod_perl]
524 - .highlight[FastCGI]
525 - SCGI
526 - WSGI
527 - JSGI
528 - Rack
529 - ISAPI
530 - many more...
531
532 ---
533
534 class: cgi
535 layout: true
536
537 .top-right[
538 ![CGI](img/cgi.gif)
539 ]
540
541 ## CGI
542
543 ---
544
545 ### Common Gateway Interface
546
547 --
548 - Created by the NCSA in 1993.
549
550 ???
551 - NCSA = National Center for Supercomputing Applications
552
553 --
554 - More formally defined in [RFC 3875](https://tools.ietf.org/html/rfc3875) ("CGI Version 1.1") in October 2004.
555
556 ---
557
558 ```perl
559 my $client_ip = $ENV{'REMOTE_ADDR'};
560
561 print "Content-Type: text/plain\n";
562 print "Status: 200 OK\n";
563
564 print "\n";
565 print "Your IP address is ${client_ip}.";
566 ```
567
568 ---
569
570 ```perl
571 *my $client_ip = $ENV{'REMOTE_ADDR'};
572
573 print "Content-Type: text/plain\n";
574 print "Status: 200 OK\n";
575
576 print "\n";
577 print "Your IP address is ${client_ip}.";
578 ```
579
580 1. Gateway sets information about the request in the environment.
581
582 .condensed[
583 .col[
584 - AUTH_TYPE
585 - CONTENT_LENGTH
586 - CONTENT_TYPE
587 - GATEWAY_INTERFACE
588 - PATH_INFO
589 - PATH_TRANSLATED
590 - QUERY_STRING
591 - REMOTE_ADDR
592 - REMOTE_HOST
593 ]
594 .col[
595 - REMOTE_IDENT
596 - REMOTE_USER
597 - REQUEST_METHOD
598 - SCRIPT_NAME
599 - SERVER_NAME
600 - SERVER_PORT
601 - SERVER_PROTOCOL
602 - SERVER_SOFTWARE
603 - other "protocol-specific" variables
604 ]
605 ]
606
607 ---
608
609 ```perl
610 my $client_ip = $ENV{'REMOTE_ADDR'};
611
612 *print "Content-Type: text/plain\n";
613 *print "Status: 200 OK\n";
614
615 print "\n";
616 print "Your IP address is ${client_ip}.";
617 ```
618
619 1. Gateway sets information about the request in the environment.
620 2. Print response headers to `STDOUT`.
621
622 .condensed[
623 .col[
624 - Content-Type
625 - Location
626 - Status
627 - other "protocol-specific" header fields
628 ]
629 ]
630
631 ---
632
633 ```perl
634 my $client_ip = $ENV{'REMOTE_ADDR'};
635
636 print "Content-Type: text/plain\n";
637 print "Status: 200 OK\n";
638
639 *print "\n";
640 print "Your IP address is ${client_ip}.";
641 ```
642
643 1. Gateway sets information about the request in the environment.
644 2. Print response headers to `STDOUT`.
645 3. Print newline.
646
647 ---
648
649 ```perl
650 my $client_ip = $ENV{'REMOTE_ADDR'};
651
652 print "Content-Type: text/plain\n";
653 print "Status: 200 OK\n";
654
655 print "\n";
656 *print "Your IP address is ${client_ip}.";
657 ```
658
659 1. Gateway sets information about the request in the environment.
660 2. Print response headers to `STDOUT`.
661 3. Print newline.
662 4. Print response document (if any).
663
664 ---
665
666 ```perl
667 my $client_ip = $ENV{'REMOTE_ADDR'};
668
669 print "Content-Type: text/plain\n";
670 print "Status: 200 OK\n";
671
672 print "\n";
673 print "Your IP address is ${client_ip}.";
674 ```
675
676 1. Gateway sets information about the request in the environment.
677 2. Print response headers to `STDOUT`.
678 3. Print newline.
679 4. Print response document (if any).
680 5. Read request document from `STDIN` (if any).
681
682 ???
683 - CGI.pm helps cut down boilerplate by helping parse things like `QUERY_STRING` and `HTTP_COOKIE`,
684 producing correctly-formatted headers, and even producing HTML.
685 - CGI.pm was deprecated in perl 5.20 and remove from core in perl 5.22.
686
687 TODO make a slide for this
688 Good:
689 - Conceptually simple.
690 - Only requires the use of the most basic and primitive program constructs (stdin, stdout, env).
691
692 Bad:
693 - Details can get complicated.
694 - Although the de facto standard for years, modern web servers are choosing to not support it
695 directly any longer.
696 - There is too much overhead in forking and execing.
697
698 ---
699
700 class: fastcgi
701 layout: true
702
703 .top-right[
704 ![FastCGI](img/fastcgi.png)
705 ]
706
707 ## FastCGI
708
709 ---
710
711 ### a low-overhead variation on CGI
712
713 --
714 - Binary protocol with support for pipelining and multiplexing.
715
716 --
717 - Open Market wrote the [specification](http://www.mit.edu/~yandros/doc/specs/fcgi-spec.html) on 29 Apr 1996.
718
719 ???
720 - Open Market was an ecommerce startup based in Massachusetts.
721 - Developed one of the first HTTP servers.
722
723 ---
724
725 ```perl
726 use FCGI;
727
728 my $request = FCGI::Request();
729
730 while (0 <= $request->Accept()) {
731 my $client_ip = $ENV{'REMOTE_ADDR'};
732
733 print "Content-Type: text/html\n\n";
734 print "Your IP address is ${client_ip}.";
735 }
736 ```
737
738 ---
739
740 ```perl
741 *use FCGI;
742 *
743 *my $request = FCGI::Request();
744
745 while (0 <= $request->Accept()) {
746 my $client_ip = $ENV{'REMOTE_ADDR'};
747
748 print "Content-Type: text/html\n\n";
749 print "Your IP address is ${client_ip}.";
750 }
751 ```
752
753 1. Use `FCGI` and instantiate an object.
754
755 ---
756
757 ```perl
758 use FCGI;
759
760 my $request = FCGI::Request();
761
762 *while (0 <= $request->Accept()) {
763 my $client_ip = $ENV{'REMOTE_ADDR'};
764
765 print "Content-Type: text/html\n\n";
766 print "Your IP address is ${client_ip}.";
767 }
768 ```
769
770 1. Use `FCGI` and instantiate an object.
771 2. Loop on `Accept()` which blocks until the next request is received.
772
773 ---
774
775 ```perl
776 use FCGI;
777
778 my $request = FCGI::Request();
779
780 while (0 <= $request->Accept()) {
781 * my $client_ip = $ENV{'REMOTE_ADDR'};
782 *
783 * print "Content-Type: text/html\n\n";
784 * print "Your IP address is ${client_ip}.";
785 }
786 ```
787
788 1. Use `FCGI` and instantiate an object.
789 2. Loop on `Accept()` which blocks until the next request is received.
790 3. Otherwise appears similar to a CGI program.
791
792 ---
793
794 ```perl
795 use FCGI;
796
797 my $request = FCGI::Request();
798
799 while (0 <= $request->Accept()) {
800 my $client_ip = $ENV{'REMOTE_ADDR'};
801
802 print "Content-Type: text/html\n\n";
803 print "Your IP address is ${client_ip}.";
804 }
805 ```
806
807 1. Use `FCGI` and instantiate an object.
808 2. Loop on `Accept()` which blocks until the next request is received.
809 3. Otherwise appears similar to a CGI program.
810
811 * IPC actually happens over a socket!
812
813 ???
814 - Can be run unmodified as a CGI script by detecting that stdin is not a socket.
815 - Can read from stdin and write to stdout via the miracle of tied filehandles.
816
817 ---
818
819 class: mod_perl
820 layout: true
821
822 .top-right[
823 ![mod_perl](img/mod_perl.gif)
824 ]
825
826 ## mod_perl
827
828 ---
829
830 --
831 - First released on March 25, 1996.
832
833 ???
834 - Unlike the interfaces we have examined so far, mod_perl is code.
835
836 --
837 - Became an Apache Software Foundation project at ApacheCon 1999 in Orlando.
838
839 --
840
841 ```perl
842 package GetIP;
843
844 use Apache::RequestRec ();
845 use Apache::Connection ();
846 use Apache::Const -compile => qw(OK);
847
848 sub handler {
849 my $r = shift;
850 my $client_ip = $r->connection->remote_addr;
851
852 $r->content_type('text/plain');
853 $r->print("Your IP address is ${client_ip}.");
854 return Apache::Const::OK;
855 }
856
857 1;
858 ```
859
860 ???
861 - There's a separate mod_perl for nginx.
862
863 Good:
864 - Can run CGI programs as-is.
865
866 Bad:
867 - Can tie you to specific web servers.
868 - Code runs in the same process as the HTTP server -- kinda scary.
869 - Using Apache's API feels heavy.
870
871 ---
872
873 class: psgi
874 layout: true
875
876 ## PSGI
877
878 ---
879
880 ```perl
881 my $app = sub {
882 my $env = shift;
883 my $client_id = $env->{'REMOTE_ADDR'};
884
885 return [
886 '200',
887 [ 'Content-Type' => 'text/plain' ],
888 [ "Your IP address is ${client_id}." ], # or IO::Handle-like object
889 ];
890 };
891 ```
892
893 ---
894
895 ```perl
896 my $app = sub {
897 * my $env = shift;
898 * my $client_id = $env->{'REMOTE_ADDR'};
899
900 return [
901 '200',
902 [ 'Content-Type' => 'text/plain' ],
903 [ "Your IP address is ${client_id}." ], # or IO::Handle-like object
904 ];
905 };
906 ```
907
908 .col[
909 ### Request
910
911 1. Hashref of request information.
912 ]
913
914 ---
915
916 ```perl
917 my $app = sub {
918 my $env = shift;
919 my $client_id = $env->{'REMOTE_ADDR'};
920
921 return [
922 * '200',
923 [ 'Content-Type' => 'text/plain' ],
924 [ "Your IP address is ${client_id}." ], # or IO::Handle-like object
925 ];
926 };
927 ```
928
929 .col[
930 ### Request
931
932 1. Hashref of request information.
933 ]
934
935 .col[
936 ### Response
937
938 1. HTTP status code.
939 ]
940
941 ---
942
943 ```perl
944 my $app = sub {
945 my $env = shift;
946 my $client_id = $env->{'REMOTE_ADDR'};
947
948 return [
949 '200',
950 * [ 'Content-Type' => 'text/plain' ],
951 [ "Your IP address is ${client_id}." ], # or IO::Handle-like object
952 ];
953 };
954 ```
955
956 .col[
957 ### Request
958
959 1. Hashref of request information.
960 ]
961
962 .col[
963 ### Response
964
965 1. HTTP status code.
966 2. Arrayref of response headers.
967 ]
968
969 ???
970 - Why not a hashref?
971 - To support multiple headers (e.g. Set-Cookie)
972 - It more closely resembles how the WSGI folks did it (i.e. list of tuples).
973
974 ---
975
976 ```perl
977 my $app = sub {
978 my $env = shift;
979 my $client_id = $env->{'REMOTE_ADDR'};
980
981 return [
982 '200',
983 [ 'Content-Type' => 'text/plain' ],
984 * [ "Your IP address is ${client_id}." ], # or IO::Handle-like object
985 ];
986 };
987 ```
988
989 .col[
990 ### Request
991
992 1. Hashref of request information.
993 ]
994
995 .col[
996 ### Response
997
998 1. HTTP status code.
999 2. Arrayref of response headers.
1000 3. Response document.
1001 ]
1002
1003 ???
1004 - Body may be a list of chunks that are concatenated together or a handle to read from.
1005
1006 ---
1007
1008 layout: false
1009
1010 ## Benefits of PSGI
1011
1012 - Everything is a data structure (almost).
1013
1014 ???
1015 - Makes it easier to write tests because mocking either the app or server is clear.
1016 - Don't necessarily need to parse a bytestream to check stuff.
1017
1018 --
1019 - No global data or shared IO handles.
1020
1021 ???
1022 - This lets you service multiple requests asynchronously in the same process/thread.
1023
1024 --
1025 - Takes deployment details out of web frameworks.
1026
1027 ???
1028 - Web frameworks only need to target PSGI.
1029 - No need to worry about the boring stuff; they can focus on the abstractions that make them unique
1030 and useful.
1031
1032 --
1033 - End-users of your app have many deployment options for free.
1034
1035 ---
1036
1037 ## Web Frameworks
1038
1039 - [Catalyst](http://www.catalystframework.org/)
1040 - [Mojolicious](http://mojolicious.org/)
1041 - [Dancer](http://perldancer.org/)
1042 - [CGI::Application](http://cgi-app.org/)
1043 - [CGI::Ex](https://github.com/chazmcgarvey/CGI-Ex/tree/psgi-2)
1044 - [Web::Simple](https://metacpan.org/pod/Web::Simple)
1045 - [Amon2](https://metacpan.org/pod/Amon2)
1046 - [Poet](https://metacpan.org/pod/Poet)
1047 - [Kelp](https://metacpan.org/pod/Kelp)
1048 - [Raisin](https://metacpan.org/pod/Raisin)
1049 - many more...
1050
1051 ---
1052
1053 ## Plack
1054
1055 - Provides tools for building, running, and testing PSGI apps.
1056
1057 --
1058 - [Plack::Handler](https://metacpan.org/pod/Plack::Handler)
1059
1060 ???
1061 - Connects PSGI apps and web servers.
1062 - Takes a request from the server,
1063 - converts it to the PSGI-specified environment,
1064 - runs your app,
1065 - converts the response back to a format the server understands.
1066
1067 --
1068 - [Plack::Loader](https://metacpan.org/pod/Plack::Loader)
1069
1070 ???
1071 - Picks an appropriate Plack::Handler (based on ENV, loaded modules, or arguments) and loads it.
1072 - Can also do stuff like restart the loader when files change.
1073
1074 --
1075 - [Plack::Runner](https://metacpan.org/pod/Plack::Runner), [plackup](https://metacpan.org/pod/plackup)
1076
1077 ???
1078 - Run PSGI apps from the command-line.
1079
1080 --
1081 - [Plack::Middleware](https://metacpan.org/pod/Plack::Middleware)
1082
1083 ???
1084 - Create subroutines that run between the handler and your app.
1085 - Can alter the request your app receives and modify the response your app returns.
1086
1087 --
1088 - [Plack::Request](https://metacpan.org/pod/Plack::Request), [Plack::Response](https://metacpan.org/pod/Plack::Response)
1089
1090 ???
1091 - Request and response wrappers can help simplify writing middleware.
1092
1093 --
1094 - [Plack::Builder](https://metacpan.org/pod/Plack::Builder)
1095
1096 ???
1097 - Provides DSL for composing apps and middleware.
1098
1099 --
1100 - [Plack::Test](https://metacpan.org/pod/Plack::Test), [Plack::Test::Suite](https://metacpan.org/pod/Plack::Test::Suite)
1101
1102 ???
1103 - Use Plack::Test for testing apps.
1104 - Plack::Test::Suite is a series of tests for testing handlers.
1105
1106 --
1107 - [Plack::Util](https://metacpan.org/pod/Plack::Util)
1108
1109 ???
1110 - Provides random useful stuff for handler and middleware developers.
1111 - Stuff like determing the length of a document or getting PSGI response headers from the arrayref.
1112
1113 ---
1114
1115 class: plackup
1116
1117 ## plackup
1118
1119 - Run PSGI apps from the command-line.
1120
1121 ```sh
1122 # read your app from app.psgi file
1123 plackup
1124
1125 # choose .psgi file from ARGV[0] (or with -a option)
1126 plackup hello.psgi
1127
1128 # switch server implementation with --server (or -s)
1129 plackup --server HTTP::Server::Simple --port 9090 --host 127.0.0.1 test.psgi
1130
1131 # use UNIX socket to run FCGI daemon
1132 plackup -s FCGI --listen /tmp/fcgi.sock myapp.psgi
1133
1134 # launch FCGI external server on port 9090
1135 plackup -s FCGI --port 9090
1136 ```
1137
1138 ---
1139
1140 class: app-psgi
1141 layout: true
1142
1143 ## app.psgi
1144
1145 ---
1146
1147 ```perl
1148 #!/usr/bin/env perl
1149
1150 my $app = sub {
1151 my $env = shift;
1152 my $client_id = $env->{'REMOTE_ADDR'};
1153
1154 return [
1155 '200',
1156 [ 'Content-Type' => 'text/plain' ],
1157 [ "Your IP address is ${client_id}." ],
1158 ];
1159 };
1160 ```
1161
1162 ---
1163
1164 ```perl
1165 #!/usr/bin/env perl
1166
1167 *use Plack::Builder;
1168
1169 my $app = sub {
1170 my $env = shift;
1171 my $client_id = $env->{'REMOTE_ADDR'};
1172
1173 return [
1174 '200',
1175 [ 'Content-Type' => 'text/plain' ],
1176 [ "Your IP address is ${client_id}." ],
1177 ];
1178 };
1179
1180 *builder {
1181 * enable 'Runtime';
1182 * mount '/' => $app;
1183 *};
1184 ```
1185
1186 ???
1187 - The `Runtime` middleware adds an `X-Runtime` header to the response with the number of seconds it
1188 took to process the request.
1189
1190 ---
1191
1192 class: env
1193 layout: true
1194
1195 ## Plack `$env`
1196
1197 ---
1198
1199 .condensed[
1200 .col[
1201 - HTTP_ACCEPT
1202 - HTTP_ACCEPT_ENCODING
1203 - HTTP_ACCEPT_LANGUAGE
1204 - HTTP_CACHE_CONTROL
1205 - HTTP_CONNECTION
1206 - HTTP_DNT
1207 - HTTP_HOST
1208 - HTTP_USER_AGENT
1209 - PATH_INFO
1210 - QUERY_STRING
1211 - REMOTE_ADDR
1212 - REMOTE_PORT
1213 - REQUEST_METHOD
1214 - REQUEST_URI
1215 - SCRIPT_NAME
1216 ]
1217 .col[
1218 - SERVER_NAME
1219 - SERVER_PORT
1220 - SERVER_PROTOCOL
1221 - psgi.errors
1222 - psgi.input
1223 - psgi.multiprocess
1224 - psgi.multithread
1225 - psgi.nonblocking
1226 - psgi.run_once
1227 - psgi.streaming
1228 - psgi.url_scheme
1229 - psgi.version
1230 - psgix.harakiri
1231 - psgix.input.buffered
1232 - psgix.io
1233 ]
1234 ]
1235
1236 ---
1237
1238 .condensed[
1239 .col[
1240 - .highlight[HTTP_ACCEPT]
1241 - .highlight[HTTP_ACCEPT_ENCODING]
1242 - .highlight[HTTP_ACCEPT_LANGUAGE]
1243 - .highlight[HTTP_CACHE_CONTROL]
1244 - .highlight[HTTP_CONNECTION]
1245 - .highlight[HTTP_DNT]
1246 - .highlight[HTTP_HOST]
1247 - .highlight[HTTP_USER_AGENT]
1248 - .highlight[PATH_INFO]
1249 - .highlight[QUERY_STRING ]
1250 - .highlight[REMOTE_ADDR]
1251 - .highlight[REMOTE_PORT]
1252 - .highlight[REQUEST_METHOD]
1253 - .highlight[REQUEST_URI]
1254 - .highlight[SCRIPT_NAME]
1255 ]
1256 .col[
1257 - .highlight[SERVER_NAME]
1258 - .highlight[SERVER_PORT]
1259 - .highlight[SERVER_PROTOCOL]
1260 - psgi.errors
1261 - psgi.input
1262 - psgi.multiprocess
1263 - psgi.multithread
1264 - psgi.nonblocking
1265 - psgi.run_once
1266 - psgi.streaming
1267 - psgi.url_scheme
1268 - psgi.version
1269 - psgix.harakiri
1270 - psgix.input.buffered
1271 - psgix.io
1272 ]
1273 ]
1274
1275 ---
1276
1277 .condensed[
1278 .col[
1279 - HTTP_ACCEPT
1280 - HTTP_ACCEPT_ENCODING
1281 - HTTP_ACCEPT_LANGUAGE
1282 - HTTP_CACHE_CONTROL
1283 - HTTP_CONNECTION
1284 - HTTP_DNT
1285 - HTTP_HOST
1286 - HTTP_USER_AGENT
1287 - PATH_INFO
1288 - QUERY_STRING
1289 - REMOTE_ADDR
1290 - REMOTE_PORT
1291 - REQUEST_METHOD
1292 - REQUEST_URI
1293 - SCRIPT_NAME
1294 ]
1295 .col[
1296 - SERVER_NAME
1297 - SERVER_PORT
1298 - SERVER_PROTOCOL
1299 - .highlight[psgi.errors]
1300 - .highlight[psgi.input]
1301 - .highlight[psgi.multiprocess ]
1302 - .highlight[psgi.multithread ]
1303 - .highlight[psgi.nonblocking ]
1304 - .highlight[psgi.run_once ]
1305 - .highlight[psgi.streaming]
1306 - .highlight[psgi.url_scheme]
1307 - .highlight[psgi.version]
1308 - .highlight[psgix.harakiri]
1309 - .highlight[psgix.input.buffered]
1310 - .highlight[psgix.io]
1311 ]
1312 ]
1313
1314 ---
1315
1316 layout: false
1317
1318 ## Plack Handlers
1319
1320 - Can be found on the CPAN in the `Plack::Handler::` namespace.
1321 - [Apache1](https://metacpan.org/pod/Plack::Handler::Apache1), [Apache2](https://metacpan.org/pod/Plack::Handler::Apache2)
1322 - [CGI](https://metacpan.org/pod/Plack::Handler::CGI)
1323 - [FCGI](https://metacpan.org/pod/Plack::Handler::FCGI)
1324 - [HTTP::Server::PSGI](https://metacpan.org/pod/Plack::Handler::HTTP::Server::PSGI)
1325 - [SCGI](https://metacpan.org/pod/Plack::Handler::SCGI)
1326 - [Starman](https://metacpan.org/pod/Plack::Handler::Starman)
1327 - [Twiggy](https://metacpan.org/pod/Plack::Handler::Twiggy)
1328 - [AnyEvent::HTTPD](https://metacpan.org/pod/Plack::Handler::AnyEvent::HTTPD)
1329 - [Thrall](https://metacpan.org/pod/Plack::Handler::Thrall)
1330 - many more...
1331
1332 ---
1333
1334 class: middleware
1335 layout: true
1336
1337 ## Plack Middleware
1338
1339 ---
1340
1341 name: middleware-debug
1342
1343 ### [`Debug`](https://metacpan.org/pod/Plack::Middleware::Debug)
1344
1345 ```perl
1346 enable 'Debug';
1347 ```
1348
1349 ---
1350
1351 ### [`ReverseProxy`](https://metacpan.org/pod/Plack::Middleware::ReverseProxy)
1352
1353 ```perl
1354 enable 'ReverseProxy';
1355 ```
1356
1357 - Fixes `REMOTE_ADDR`, `HTTP_HOST`, `SERVER_PORT`, and `psgi.url_scheme` in the environment.
1358
1359 ---
1360
1361 ### [`LogDispatch`](https://metacpan.org/pod/Plack::Middleware::LogDispatch)
1362
1363 ```perl
1364 use Log::Dispatch;
1365
1366 my $logger = Log::Dispatch->new(
1367 outputs => [
1368 [
1369 'Syslog',
1370 min_level => 'debug',
1371 ident => 'myapp',
1372 ],
1373 ],
1374 );
1375
1376 enable 'LogDispatch', logger => $logger;
1377 ```
1378
1379 ---
1380
1381 ### [`XSRFBlock`](https://metacpan.org/pod/Plack::Middleware::XSRFBlock)
1382
1383 ```perl
1384 enable 'XSRFBlock';
1385 ```
1386
1387 - Blocking cross-site request forgery couldn't be easier.
1388
1389 ---
1390
1391 ### [`RedirectSSL`](https://metacpan.org/pod/Plack::Middleware::RedirectSSL)
1392
1393 ```perl
1394 enable 'RedirectSSL';
1395 ```
1396
1397 - Redirects from http to https (or backwards, if configured).
1398 - Can also set HSTS header with configurable `max-age`.
1399
1400 ---
1401
1402 layout: false
1403
1404 .top-right[
1405 ![CPAN](img/cpan.png)
1406 ]
1407
1408 ## Plack modules on the CPAN in July 2016
1409
1410 **10** `Plack-Handler-*` distributions
1411
1412 **55** `Plack-App-*` distributions
1413
1414 **253** `Plack-Middleware-*` distributions
1415
1416 ---
1417
1418 ## Parting Thoughts
1419
1420 - You should write middleware!
1421
1422 ???
1423 - The concept and implementation of middleware is cool.
1424 - You should consider writing parts of your app as middleware so that functionality is available
1425 under different web frameworks.
1426 - Stuff that makes sense as middleware:
1427 - Auth mechanisms
1428 - Logging
1429 - Error handling
1430 - Sessions
1431 - Rate limiters
1432
1433 --
1434 - PSGI also specifies a way to delay or stream responses to the server.
1435
1436 ???
1437 - It's kind of complicated, but you can read the spec to learn more.
1438 - Read the source code of various apps and middlewares to see how it works in practice.
1439
1440 ---
1441
1442 class: center, middle
1443 name: conclusion
1444
1445 ## Conclusion:
1446
1447 ### Understand PSGI & Plack, and use them!
1448
1449 ---
1450
1451 class: center, middle
1452 layout: false
1453 name: questions
1454
1455 ## Questions?
1456
1457 ---
1458
1459 class: center, middle
1460 name: last
1461
1462 .col.sep[
1463 ## Thank you
1464
1465 Email me: Charles McGarvey
1466 <cmcgarvey@bluehost.com>
1467 <chazmcgarvey@brokenzipper.com>
1468
1469 .talkqr.center[
1470 Leave me feedback, if you want:
1471
1472 ![Page on Joind.in](img/talkqr.svg)
1473
1474 <https://joind.in/talk/6e4d2>
1475 ]
1476 ]
1477
1478 .col[
1479 ## Credits
1480
1481 .left[
1482 - Thank you [Tatsuhiko Miyagawa](http://weblog.bulknews.net/) and other contributors for creating PSGI and Plack.
1483 ]
1484 ]
1485
1486 </textarea><script src="https://gnab.github.io/remark/downloads/remark-latest.min.js"></script><script>var slideshow = remark.create({countIncrementalSlides: true, highlightLanguage: 'perl', highlightLines: true, ratio: '16:9', /*slideNumberFormat: '',*/ navigation: {scroll: false, touch: false, click: false}})</script></body></html>
1487 <!-- vim: set ts=4 sts=4 sw=4 tw=120 et ft=markdown nowrap: -->
This page took 0.084461 seconds and 3 git commands to generate.