ãµã€ã¯ã«ã®ãªãªãžãã«èšäºã®èè ã®èš±å¯ãåŸãŠãHabréã«ãµã€ã¯ã«ãå ¬éããŸãã
pragmaticperl.comãã¬ãžã³Webãµã€ãã®ãªãªãžãã«èšäº
PSGI / Plackã¯ãPerl Webã¢ããªã±ãŒã·ã§ã³ãèšè¿°ããããã®ææ°ã®æ¹æ³ã§ãã ã»ãšãã©ãã¹ãŠã®ãã¬ãŒã ã¯ãŒã¯ã¯ããã®ãã¯ãããžãŒãäœããã®æ¹æ³ã§ãµããŒããŸãã¯äœ¿çšããŸãã ãã®èšäºã§ã¯ããã°ããããã²ãŒãããŠå ã«é²ãã®ã«åœ¹ç«ã€ç°¡åãªçŽ¹ä»ãæäŸããŸãã
ç§ãã¡ã¯ãWebéçºã®åéã«ããããã¯ãããžãŒãšã¢ãããŒããæ¥éã«å€åããŠããæ代ã«çããŠããŸãã æåã«CGIãããã次ã«ååã§ã¯ãªãã£ããšãã«FastCGIãçŸããŸããã FastCGIã¯CGIã®äž»ãªåé¡ã解決ããŸããã CGIã§ã¯ãåŒã³åºãããšã«ãµãŒããŒããã°ã©ã ãåèµ·åããå¿ èŠããããSTDINãšSTDOUTã䜿çšããŠããŒã¿ã亀æãããŸããã FastCGIã§ã¯ããµãŒããŒãšã®éä¿¡ã¯TCP / IPãŸãã¯Unixãã¡ã€ã³ãœã±ãããä»ããŠè¡ãããŸãã ããã§PSGIãã§ããŸããã
ããã¯äœã§ãã
PSGIã¯ãéçºè ã®å®®å·é圊ãèšãããã«ããWebãã¬ãŒã ã¯ãŒã¯ãšWebãµãŒããŒã®ããã®ããŒã«å€§éºŠã®æ¥çå€ãã§ãã æãè¿ã芪Theã¯ãWSGIïŒPythonïŒãšRackïŒRubyïŒã§ãã ã¢ã€ãã¢ã¯ããã§ãã éçºè ã¯ã¢ããªã±ãŒã·ã§ã³ãã§ããã ãå€ãã®ãšã³ãžã³ã«é©å¿ãããããã«éåžžã«å€ãã®æéãè²»ããããšãå€ããPSGIã¯ããŸããŸãªãµãŒããŒãæäœããããã®åäžã®ã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããããã«ããäœæ¥ãå€§å¹ ã«ç°¡çŽ åãããŸãã
ç¹åŸŽ
ãã¡ããããã®èšäºã®åœ¢åŒã§ã¯ããã¹ãŠã®ãã¥ã¢ã³ã¹ãå®å šã«èª¬æããããšã¯ã§ããªãããã以éã¯éèŠãªç¹ã®ã¿ã説æããŸãã
- $ envã¯ãã¯ã©ã€ã¢ã³ããšãµãŒããŒéã§æ å ±ã亀æããããã«äœ¿çšãããŸãïŒããã·ã¥ãžã®ãªã³ã¯ã§ãïŒã
- PSGIã¢ããªã±ãŒã·ã§ã³-$ envããã©ã¡ãŒã¿ãŒãšããŠäœ¿çšããPerlé¢æ°ãžã®ãªã³ã¯ã
- ãã®é¢æ°ã¯ã3ã€ã®èŠçŽ ã§æ§æãããé åãžã®ãªã³ã¯ãè¿ããŸãïŒHTTPã¹ããŒã¿ã¹ã[HTTPããããŒ]ã[ã¬ã¹ãã³ã¹ããã£];
- é¢æ°ã¯å¥ã®é¢æ°ãžã®ãªã³ã¯ãè¿ãå ŽåããããŸãããããã«ã€ããŠã¯ä»ã®è©³çŽ°ãªèšäºã§èª¬æããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã®ã¹ã¿ãŒãã¢ããã³ãŒããå«ããã¡ã€ã«ã®æ¡åŒµåã¯.psgiã§ãªããã°ãªããŸããã
ãã®æ®µéã§ã¯ãã³ãŒãã®çŽæ¥åŠçãéå§ããããã«å¿ èŠãªããšã¯ããã ãã§ãã
PSGIã¢ããªã±ãŒã·ã§ã³
以äžã¯ãæãåçŽãªPSGIã¢ããªã±ãŒã·ã§ã³ã®ã³ãŒãã§ãã
my $app = sub { my $env = shift; # $env return [200, ['Content-Type' => 'text/plain'], ["hello, world\n"]]; };
ãã®ã¢ããªã±ãŒã·ã§ã³ãapp.psgiãã¡ã€ã«ããŸãã¯psgiæ¡åŒµåãæã€ãã®ä»ã®ãã¡ã€ã«ã«ä¿åããŸãã æ©èœã調ã¹ãŸãã 次ã«ãã³ãŒãã«ã ãã®åŸãåã³æ©èœã«ã€ããŠã ããã¯ãã¹ãŠäžç·ã«åãŸããŸãã å§ããŸãã
perl app.psgiãèµ·åãããšãéãã«å®è¡ãããŸãããã¢ããªã±ãŒã·ã§ã³ã¯å®è¡ãããŠããŸããã
ãã©ã€ããªPSGIãµãŒããŒ
PSGIã¢ããªã±ãŒã·ã§ã³ãå®è¡ããã«ã¯ãPSGIãµãŒããŒãå¿ èŠã§ãã çŸåšãããã€ãã®ãµãŒããŒããããŸãã
- ãã€ã®ãŒ
- ã¹ã¿ãŒãã³
- ãã§ã«ã¹ã
- ã³ãã
PSGIãµãŒããŒã«ã€ããŠç°¡åã«
- Starman-äºååå²ãµãŒããŒã éåžžã«é«éã«åäœããããã«å€ãã®ããšãå®è¡ã§ããŸããããšãã°ãUNIXãã¡ã€ã³ãœã±ããã®ãµããŒãã
- Twiggy-AnyEventã«åºã¥ãéåæãµãŒããŒã
- Feersum-䞻芳çã«ããã®ãªã¹ãå šäœã§æéã ã¡ã€ã³éšåã¯XSã¢ãžã¥ãŒã«ãšããŠå®è£ ãããŸãã EVã«åºã¥ã;
- Coronaã¯ãCoroã«åºã¥ãéåæãµãŒããŒã§ãã
ãããã®ãµãŒããŒã¯ãã¹ãŠCPANã§å©çšã§ããŸãã å°æ¥çã«ã¯ãã¹ã¿ãŒãã³ã䜿çšããTwiggyã«å€æŽããŠãããFeersumã«å€æŽããŸãã åã¿ã¹ã¯ã«ã¯ç¬èªã®ãµãŒããŒããããŸãã
ã¢ããªã±ãŒã·ã§ã³ã®èµ·å
ã¢ããªã±ãŒã·ã§ã³ã¯ããããã®ãµãŒããŒã®ãããã§ããŸã£ããåãããã«å®è¡ãããŸããã³ããã§ã¯ããããã«å€æŽããå¿ èŠããããŸãã ãµãŒããŒãã€ã³ã¹ããŒã«ããåŸïŒãã®å Žåã¯StarmanïŒãstarmanå®è¡å¯èœãã¡ã€ã«ã¯/ usr / binãŸãã¯/ usr / local / binã«è¡šç€ºãããŸãã èµ·åã¯ã次ã®ã³ãã³ãã«ãã£ãŠå®è¡ãããŸãã
/usr/local/bin/starman app.psgi
ããã©ã«ãã§ã¯ãPSGIãµãŒããŒã¯5000ããŒãã䜿çšããŸãã ããšãã°ãã¢ããªã±ãŒã·ã§ã³ã--port 8080ã¹ã€ããã§å®è¡ããããšã§å€æŽã§ããŸãã PSGIã¯ä»æ§ã§ããããšãæãåºããŠãã ããã ãã®å Žåããã®ä»æ§ã䜿çšããŠåçŽãªWebã¢ããªã±ãŒã·ã§ã³ãäœæããŸããã æããã«ãéåžžã®éçºã§ã¯ãGETãã©ã¡ãŒã¿ãŒã®åä¿¡ããCookieããŒã¿ã®åä¿¡ãŸã§ãå€ãã®è£å©æ©èœãå®è£ ããå¿ èŠããããŸãã ããã¯ãã¹ãŠãå¿ èŠãªæ©èœããªããã°å®çŸããªãã£ãã§ãããã
ãã©ãã¯
Plackã¯PSGIå®è£ ã§ãïŒPerlã«ã¯æšæºã®Packã¢ãžã¥ãŒã«ããããããå®è£ ã®ååã¯Plackã§ãïŒã Plackã¯éçºè ãšããŠã®ç§ãã¡ã®ç掻ã楜ã«ããŸãã $ envãæäœããããã®èšå€§ãªæ°ã®é¢æ°ãå«ãŸããŠããŸãã
åºæ¬æ§æã§ã¯ãPlackã¯ããªãå€æ°ã®ã¢ãžã¥ãŒã«ã§æ§æãããŠããŸãã ãã®æ®µéã§ã¯ããããã«ã®ã¿èå³ããããŸãã
- ãã©ãã¯
- Plack ::ãªã¯ãšã¹ã
- Plack ::å¿ç
- Plack ::ãã«ããŒ
- Plack ::ããã«ãŠã§ã¢
Plack :: Requestããã³Plack :: Responseã¯ãHash :: MultiValueåã®ç°ãªãå€ãè¿ããŸããã泚æãã䟡å€ããããŸãã
ããã·ã¥:: MultiValue
åããå®®å·é圊ã«ãã£ãŠäœæããããã®ã¢ãžã¥ãŒã«ã¯ããã·ã¥ã§ããã埮åŠãªéãããããŸãã 1ã€ã®ããŒã«è€æ°ã®å€ãä¿åã§ããŸãã äŸïŒ$ hash-> getïŒ 'key'ïŒã¯å€ãè¿ããããŒã«è€æ°ã®å€ãããå Žåã¯æåŸãè¿ããŸãããã¹ãŠã®å€ãå¿ èŠãªå Žåã¯ãé¢æ°$ hash-> get_allïŒ 'key'ïŒã䜿çšããŠãçµæãè¿ããŸãïŒ 'value1'ã 'value2'ïŒã«ãªããŸãã Hash :: MultiValueã§ã¯ãåŒã³åºãã®ã³ã³ããã¹ããèæ ®ãããããã泚æãå¿ èŠã§ãã
Plack ::ãªã¯ãšã¹ã
ã¯ã©ã€ã¢ã³ãèŠæ±ãåŠçããããã®æ©èœãå«ãã¢ãžã¥ãŒã«ã ããã«ã¯å€ãã®ã¡ãœãããå«ãŸããŠãããCPANã§ãã€ã§ãèŠã€ããããšãã§ããŸãã ããã«ããã®èšäºã®ãã¬ãŒã ã¯ãŒã¯ã§ã¯ã次ã®ã¡ãœããã䜿çšããŸãã
- env-$ envãè¿ããŸãã
- method-èŠæ±ã¡ãœãããè¿ããŸãïŒGETãPOSTãOPTIONSãHEADãªã©ã
- path_infoã¯éèŠãªã¡ãœããã§ãã çŸåšã®ã¹ã¯ãªãããžã®ããŒã«ã«ãã¹ãè¿ããŸãã
- parameters-Hash :: MultiValueã®åœ¢åŒã§ãã©ã¡ãŒã¿ãŒïŒx-www-form-url-encodedãã¢ãã¬ã¹ããŒãã©ã¡ãŒã¿ãŒïŒãè¿ããŸãã
- uploads-ããã·ã¥:: MultiValueã®åœ¢åŒã§ããã©ã¡ãŒã¿ãŒïŒmultipart-form-dataã䜿çšããŠè»¢éïŒãè¿ããŸãã
Plack ::å¿ç
- status-ã¹ããŒã¿ã¹ïŒHTTPå¿çã³ãŒãïŒãèšå®ããŸãããã©ã¡ãŒã¿ãŒãªãã§åŒã³åºããããšã以åã«èšå®ãããã¹ããŒã¿ã¹ãè¿ããŸãã
- headers-å¿çããããŒãèšå®ããŸã;
- finalize-åºå£ç¹ãã¢ããªã±ãŒã·ã§ã³ã®æåŸã®æ©èœã ä»æ§ã«åŸã£ãŠPSGIå¿çãè¿ããŸãã
Plack ::ãã«ããŒ
ã¡ãœããã¯èæ ®ããŸãããããã¯éåžžã«æè»ãªã«ãŒã¿ãŒã§ããããšã«æ³šæããŠãã ããã ããšãã°ãããŒã«ã«ã¢ãã¬ã¹ã«ãã³ãã©ãŒïŒPSGIã¢ããªã±ãŒã·ã§ã³ïŒãã€ã³ã¹ããŒã«ã§ããŸãã
my $app = builder { mount "/" => builder { $my_cool_app; }; };
çµæ-/ãžã®åŒã³åºãã¯ã察å¿ããPSGIã¢ããªã±ãŒã·ã§ã³ã«ãªãã€ã¬ã¯ããããŸãã ãã®å Žåã$ my_cool_appã§ãã
ã«ãŒãã¯ãããšãã°æ¬¡ã®ããã«ãã¹ãã§ããŸãã
my $app = builder { mount "/" => builder { mount "/another" => builder { $my_another_cool_app; }; mount "/" => builder { $my_cool_app; }; }; };
ãããŠããããã®ã«ãŒãã¯ãã¹ãã§ããŸãã ãã®äŸã§ã¯ã/ã«è©²åœããªããã®ã¯ãã¹ãŠ/ã«éä¿¡ãããŸãã
Plack ::ããã«ãŠã§ã¢
ããã«ãŠã§ã¢ã¢ããªã±ãŒã·ã§ã³ãäœæããããã®åºæ¬ã¯ã©ã¹ã ããã«ãŠã§ã¢ã¯ãããã«ãŠã§ã¢ãã§ãã PSGIèŠæ±ãŸãã¯æºåãã§ããPSGIå¿çãå€æŽããå¿ èŠãããå Žåã«äœ¿çšãããã¢ããªã±ãŒã·ã§ã³ã®ç¹å®ã®éšåãèµ·åããããã®ç¹å®ã®æ¡ä»¶ãæäŸããŸãã
Plackã§ã¢ããªã±ãŒã·ã§ã³ãæžãæãã
use strict; use Plack; use Plack::Request; my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); $res->body('Hello World!'); return $res->finalize(); };
ããã¯Plackã䜿çšããæãåçŽãªã¢ããªã±ãŒã·ã§ã³ã§ãã ããã¯ãã®ä»äºã®åçãå®å šã«ç€ºããŠããŸãã
ããªãã泚æãæãå¿ èŠããããã®ã $ appã¯é¢æ°ãžã®ãªã³ã¯ã§ãã éåžžã«å€ãã®å Žåãé¡äŒŒãããã®ã®ç°¡åãªã€ã¥ãããããšãèšå·ãå¿ããããŸãã é¢æ°ãžã®ãªã³ã¯ãçµäºãããã$ envãæž¡ããã«Plack :: RequestãäœæããåŸã 泚æãã䟡å€ããããŸãã
perl -c app.psgiã䜿çšããŠæ§æã確èªã§ããŸãã
PSGIã¢ããªã±ãŒã·ã§ã³ã®äœæã«é¢ãããã1ã€ã®éèŠãªãã€ã³ãã¯æ¬¡ã®ãšããã§ããå¿çæ¬æãäœæãããšãã¯ãæåã§ã¯ãªããã€ãïŒUTF-8ãªã©ïŒãããããšã確èªããŠãã ããã ãã®ãšã©ãŒã¯éåžžã«é£ããããšãããããŸãã ãã®ååšã¯ãpsgi.errorã®ãšã©ãŒã§ç©ºã®ãµãŒããŒå¿çã«ã€ãªãããŸãã
ãsyswriteã§ã®ã¯ã€ãæåã
ç§ãã¡ã®ã¢ããªã±ãŒã·ã§ã³ã¯åã®ã¢ããªã±ãŒã·ã§ã³ãšåæ§ã«èµ·åããŸãã
- $ reqã¯Plack :: Requeståã®ãªããžã§ã¯ãã§ãã $ reqã«ã¯ã¯ã©ã€ã¢ã³ãèŠæ±ããŒã¿ãå«ãŸããŸãã é¢æ°ã«æž¡ããã$ envããã·ã¥ããããããååŸããŸãã
- $ res-Plack ::å¿çãããã¯ã¯ã©ã€ã¢ã³ããžã®å¿çã§ãã new_responseã¡ãœããã䜿çšããèŠæ±ã«åºã¥ããŠæ§ç¯ãããå¿çã³ãŒãïŒãã®å Žåã¯200ïŒããã©ã¡ãŒã¿ãŒãšããŠåãåããŸãã
- body-å¿çæ¬æãèšå®ããŸãã
- finalize-å¿çãªããžã§ã¯ããPSGIå¿çã®é åãžã®åç §ã«å€æããŸãïŒäžèšã®ããã«ãå¿çã®ã¹ããŒã¿ã¹ãããããŒãããã³æ¬æã§æ§æãããŸãïŒã
ã¯ããHello worldã¯ç¢ºãã«æªãã¯ãããŸããããããŸãæ©èœçã§ã¯ãããŸããã ããã§ããã¹ãŠã®ããŒã«ã䜿çšããŠãæãåçŽãªã¢ããªã±ãŒã·ã§ã³ãäœæããããšããŸãïŒãã ããã¯ããã«äŸ¿å©ã§ãïŒã
3ã€ã®æ©èœãå®è£ ããAPIãäœæããŸãã
- æåã®ãã®ã¯ãã®è¡ãå ¥åãã©ã¡ãŒã¿ãŒãšããŠåãåãããã®è¡ãæ£åžžã«åãå ¥ãããããšèšããŸãã é£çµ¡å ã®ã¢ãã¬ã¹-localhostïŒ8080 /;
- 2çªç®ã®é¢æ°ã¯ããã©ã¡ãŒã¿ãŒãšããŠæååãåããããšãã°ããã®æååãåæïŒäž¡åŽã§åãããã«èŠããåèªãŸãã¯ãã¬ãŒãºãããšãã°ããã¢ã«ãŒã³ãã³ãé»äººãèªããïŒãã©ãããè¿ããŸãã ããŒã«ã«ãã¹ãã«é 眮ãããŸãïŒ8080 / palindrome;
- 3çªç®ã®é¢æ°ã¯ãã©ã¡ãŒã¿ãŒãšåãæååãåããéããŸã«è¿ããŸãã localhostïŒ8080 / reverseã«é 眮ãããŸãã
ã³ãŒããèšè¿°ããçµæã次ã®ããšãå®è¡ã§ãããã®ãååŸããå¿ èŠããããŸãã
- æååãã©ã¡ãŒã¿ãŒãæž¡ãããå Žåããã¹ãŠãæ£åžžã§ããããšã«ã¢ã¯ã»ã¹/å¿çãããšã;
- ããªã³ãããŒã ã«é£çµ¡ãããšãã¯ãæååãã©ã¡ãŒã¿ãŒã®ååšã確èªããããªã³ãããŒã ãã©ãããçããŸãã
- ã¢ã¯ã»ã¹/ãªããŒã¹ãããšããå転æååãæå®ããŸãã
è¡ãéã«ããã«ã¯ã次ã®æ§æã䜿çšããŸãã
$string = scalar reverse $string;
æååãåæã§ãããã©ãããå€æããã«ã¯ã次ã®é¢æ°ã䜿çšããŸãã
sub palindrome { my $string = shift; $string = lc $string; $string =~ s/\s//gs; if ($string eq scalar reverse $string) { return 1; } else { return 0; } }
ã¢ããª
Plack :: Requestã§ã¯ãparametersã¡ãœããã䜿çšããŠãã©ã¡ãŒã¿ãŒãåãåãããšãã§ããŸãã
my $params = $req->parameters();
ã¢ããªã±ãŒã·ã§ã³ãå®æããããã©ãŒã ã«è¿œå ããŸãã
use strict; use Plack; use Plack::Request; my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); my $params = $req->parameters(); my $body; if ($params->{string}) { $body = 'string exists'; } else { $body = 'empty string'; } $res->body($body); return $res->finalize(); };
å§ããŸãã æåã®éšåã®æºåãã§ããŸããã
ã¢ãã¬ã¹localhost ïŒ8080 /ïŒString = 1ã«ç§»åãããšãè¡ãããããšã瀺ãåçã衚瀺ãããŸãã localhost ïŒ8080 /ã«ç§»åãããšããšã©ãŒãè¿ãããŸãã
ããžãã¯ã®æ®ãã®éšåã¯åãã¢ããªã±ãŒã·ã§ã³ã«çŽæ¥å®è£ ã§ããçŸåšã®ãã¹ãå«ãpath_infoã§ããžãã¯ãåå²ã§ããŸãã åèã®ããã«ãpath_info解æã¯æ¬¡ã®ããã«å®è£ ã§ããŸãã
my @path = split '\/', $req->path_info(); shift @path;
ãããŠä»ã$ path [0]ã«å¿ èŠãªãã¹ããããŸãã
éèŠïŒã³ãŒããå€æŽããåŸããµãŒããŒãåèµ·åããå¿ èŠããããŸãïŒ
Plack ::ãã«ããŒ
ä»ãã«ãŒã¿ãŒã詳ããèŠã䟡å€ããããŸãã
ä»ã®PSGIã¢ããªã±ãŒã·ã§ã³ãã³ã³ããŒãã³ããšããŠäœ¿çšããããšãã§ããŸãã ããã«ãŠã§ã¢ãæ¥ç¶ããæ©èœã¯éåžžã«äŸ¿å©ã§ãã
ã«ãŒã¿ãŒã䜿çšããããã«ãæåã®ã¢ããªã±ãŒã·ã§ã³ãäœãçŽããŸãããã
use strict; use Plack; use Plack::Request; use Plack::Builder; my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); $res->header('Content-Type' => 'text/html', charset => 'Utf-8'); my $params = $req->parameters(); my $body; if ($params->{string}) { $body = 'string exists'; } else { $body = 'empty string'; } $res->body($body); return $res->finalize(); }; my $main_app = builder { mount "/" => builder { $app; }; };
ããã§ã$ main_appãã¡ã€ã³ã®PSGIã¢ããªã±ãŒã·ã§ã³ã«ãªããŸããã $ã¢ããªã¯/ã§åå ããŸãã ããã«ãå¿çã«ããããŒãèšå®ããé¢æ°ãè¿œå ãããŸããïŒããããŒã¡ãœããã䜿çšïŒã éèŠãªæ³šæãæã䟡å€ããããŸãããã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãç°¡åã«ããããã«ããã¹ãŠã®æ©èœã1ã€ã®ãã¡ã€ã«ã«é 眮ãããŠããŸãã ããè€éãªã¢ããªã±ãŒã·ã§ã³ã®å Žåãããã¯ãã¡ããæšå¥šãããŸããã
次ã«ãã³ã³ããŒãã³ããæ¥ç¶ããŠãã¢ããªã±ãŒã·ã§ã³ã®åœ¢åŒã§è¡ãæå¹ã«ããŸããã¢ããªã±ãŒã·ã§ã³ã¯ã localhost ïŒ8080 / reverseã«é 眮ãããŸãã
use strict; use Plack; use Plack::Request; use Plack::Builder; my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); $res->header('Content-Type' => 'text/html', charset => 'Utf-8'); my $params = $req->parameters(); my $body; if ($params->{string}) { $body = 'string exists'; } else { $body = 'empty string'; } $res->body($body); return $res->finalize(); }; my $reverse_app = sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); my $params = $req->parameters(); my $body; if ($params->{string}) { $body = scalar reverse $params->{string}; } else { $body = 'empty string'; } $res->body($body); return $res->finalize(); }; my $main_app = builder { mount "/reverse" => builder { $reverse_app }; mount "/" => builder { $app; }; };
æ€èšŒã¢ãã¬ã¹ã¯localhost ïŒ8080 / reverseïŒString = testïŒ 20stringã§ãã
2/3ã¿ã¹ã¯ãå®äºããŸããã ãã ãããã®å Žåã$ appãš$ reverse_appã¯éåžžã«ãã䌌ãŠããŸãã å°ããªãã¡ã¯ã¿ãªã³ã°ããŸãããã å¥ã®é¢æ°ãè¿ãé¢æ°ãäœæããŸãããïŒãã以å€ã®å Žåãé«éé¢æ°ïŒã
ããã§ãã¢ããªã±ãŒã·ã§ã³ã¯æ¬¡ã®ããã«ãªããŸãã
use strict; use Plack; use Plack::Request; use Plack::Builder; sub build_app { my $param = shift; return sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); $res->header('Content-Type' => 'text/html', charset => 'Utf-8'); my $params = $req->parameters(); my $body; if ($params->{string}) { if ($param eq 'reverse') { $body = scalar reverse $params->{string}; } else { $body = 'string exists'; } } else { $body = 'empty string'; } $res->body($body); return $res->finalize(); }; } my $main_app = builder { mount "/reverse" => builder { build_app('reverse') }; mount "/" => builder { build_app() }; };
ã¯ããã«è¯ãã 次ã«ãæåŸã®3çªç®ã®é¢æ°ãAPIã«è¿œå ããæçµçã«ã¢ããªã±ãŒã·ã§ã³ãçµäºããŸãã ãã¹ãŠã®æ¹åã®çµæããã©ãŒã ã®ã¢ããªã±ãŒã·ã§ã³ãååŸãããŸããã
use strict; use Plack; use Plack::Request; use Plack::Builder; sub build_app { my $param = shift; return sub { my $env = shift; my $req = Plack::Request->new($env); my $res = $req->new_response(200); $res->header('Content-Type' => 'text/html', charset => 'Utf-8'); my $params = $req->parameters(); my $body; if ($params->{string}) { if ($param eq 'reverse') { $body = scalar reverse $params->{string}; } elsif ($param eq 'palindrome') { $body = palindrome($params->{string}) ? 'Palindrome' : 'Not a palindrome'; } else { $body = 'string exists'; } } else { $body = 'empty string'; } $res->body($body); return $res->finalize(); }; } sub palindrome { my $string = shift; $string = lc $string; $string =~ s/\s//gs; if ($string eq scalar reverse $string) { return 1; } else { return 0; } } my $main_app = builder { mount "/reverse" => builder { build_app('reverse') }; mount "/palindrome" => builder { build_app('palindrome') }; mount "/" => builder { build_app() }; };
æ€èšŒãªã³ã¯ïŒ
localhost ïŒ8080 / palindromeïŒstring = argentinaïŒ 20ManitïŒ 20negra
ããã«è©³çŽ°ãªãããã¯ã§ã¯ãããã«ãŠã§ã¢ãã»ãã·ã§ã³ãCookieããµãŒããŒã®æŠèŠãç¹å®ã®å°ããªãã³ãããŒã¯ã®äŸãPSGI / Plackã®æ©èœãšåŸ®åŠããè² è·ã®ããã£ãPSGIãPSGIã¢ããªã±ãŒã·ã§ã³ã®å±éæ¹æ³ã®æŠèŠãPSGIãã¬ãŒã ã¯ãŒã¯ããããã¡ã€ãªã³ã°ãåãäžããŸãã ãã¹ã¿ãŒãã³+ NginxãPSGIã¢ãŒãã§ã®CGIã¹ã¯ãªããã®å®è¡ããŸãã¯ãCGIã¢ããªã±ãŒã·ã§ã³ã¯ãããPSGIã欲ããããªã©ã