ãµã¼ããµã¤ãã¨ã³ã¸ãã¢ã®å®®æã§ãã
ã«ã¼ãã®è² è·è©¦é¨ã«ã¤ãã¦ã第3å¼¾ã®è¨äºã§ãã æåã®è¨äº ã§ã触ãã¾ããããä»åã®è² è·è©¦é¨å®æ½ã«ããããå¤é¨ãµã¼ãã¹ã模æ¬ããã¢ãã¯ãµã¼ãã¹ãä½æãã¾ããã
å¤é¨ãµã¼ãã¹ã¸æ¥ç¶ããè² è·è©¦é¨ãè¡ãã«ã¯
ä»åãè² è·è©¦é¨ã®å¯¾è±¡ã¨ããã®ã¯ãBASEã®ã«ã¼ãã·ã¹ãã ã§ãã
ã«ã¼ãã·ã¹ãã ã«ã¯æ±ºæ¸æã«å¤é¨ãµã¼ãã¹ãå©ç¨ããç®æããããé常ã®éçºæã«ã¯ãå¤é¨ãµã¼ãã¹ããæä¾ããã¦ããæ¤è¨¼ç°å¢ã¸æ¥ç¶ãã¦ãã¾ãã
ä¸è¬çã«æä¾ããã¦ããæ¤è¨¼ç°å¢ã¯ãæ¬çªç°å¢ã»ã©ã®æ§è½ã§ã¯ãªãã£ãããä»ã®å©ç¨è ã¨å ±ç¨ãã¦ããã¨ãããã®ã§ã¯ãªãã§ããããããã®ãããå©ç¨è ã®é½åã§èªç±ã«é«è² è·ãããããã¨ã¯é£ããã¨è¨ãã¾ãã è² è·è©¦é¨ã®ãããªé«è² è·ãããããã¨ãå¯è½ãå¦ããã¾ãå¯è½ãªå ´åãèªç±ã«å®æ½ãã¦ããããäºåç³è«ãå¿ è¦ããªã©ã確èªãããããªãã®æºåãè¡ãå¿ è¦ãããã¾ãã
ã¾ãå©ç¨ãå¯è½ã ã£ãã¨ãã¦ãå¤é¨ãµã¼ãã¹è¦å ã§ããã©ã¼ãã³ã¹å½±é¿ãåºãæ¸å¿µãéä¿¡ãããªã¯ã¨ã¹ãã®å¶ç´ãªã©ãå¤é¨ãµã¼ãã¹ã®æä¾ããç°å¢ã使ç¨ããå ´åã«èæ ®ããªããã°ãªããªãç¹ãããã¤ããããã¨ãããã£ã¦ãã¾ããã
å¤é¨ãµã¼ãã¹ã¸æ¥ç¶ããªã
ä¸æ¹ã§ãæä¾ããã¦ããæ¤è¨¼ç°å¢ã使ç¨ããã«è² è·è©¦é¨ãå®æ½ãããã¨ã«ã¤ãã¦æ¤è¨ãã¾ãã
- æä¾ããã¦ããæ¤è¨¼ç°å¢ã¨åçã®åä½ãããã¢ãã¯ãµã¼ãã¹ãç¨æããæ¥ç¶å ãå¤æ´ãã
- æä¾ããã¦ããæ¤è¨¼ç°å¢ã¸æ¥ç¶ããå¦çããå¤é¨æ¥ç¶ããã«å¦çãå®äºããããã«å¤æ´ãã
- ãã£ãã®ãã¨ãã¹ã¦ãæ¬çªç°å¢ã§è¡ã
æ¬çªç°å¢ã¨ã®å·®ç°ãå°ããããã¨ããç¹ã«ããã¦ã3ãé¤ãã°ãããã°ã©ã ã®å¤æ´ãå°ãªããªã1ã®æ¹æ³ãæå©ã¨è¨ãããã§ãã 稼åä¸ã®ãµã¼ãã¹ã§3ãè¡ãã®ã¯ããªã¹ã¯ãé«ãä¸ã«ãæ¤è¨¼çã®æºåãå«ããã¨ã³ã¹ããä½ãã¨ã¯è¨ãã¾ããã
調æ»ãè¡ã£ãã¨ãããç¨æããã¢ãã¯ãµã¼ãã¹ã¯ã
- BASEã®ãµã¼ãã¹ããã®ãªã¯ã¨ã¹ãã«å¯¾ãã¦
- å¦çå¯è½ãªå½¢å¼ã®ã¬ã¹ãã³ã¹ã
- é©åãªæéãããã¦è¿ãã¦ãããã°åå
ã¨ããè¦éããæã¤ãã¨ãã§ãã¾ããã
ããã§ããã°ã¢ãã¯ãµã¼ãã¹ãæºåãããã¨ãæåãªé¸æè¢ã«ãªãã ããã¨ãããã¨ã«ãªããæ¤è¨ãå§ãã¾ããã
ã¢ãã¯ãµã¼ãã¹ã¨ãã¦Swaggerãæ¤è¨ãã
ãã¦ãã¢ãã¯ãµã¼ãã¹ãã©ã®ããã«æºåãããã¨ãããã¨ã«ãªãã調æ»ãéå§ãã¾ããã
ãã¨ãã° Swagger ã¯ã決ã¾ã£ãã¬ã¹ãã³ã¹ãè¿ãã¢ãã¯ãµã¼ãã¨ãã¦åä½ãã¦ãããããã§ããä½æããAPIå®ç¾©ãããã¥ã¡ã³ãã¨ãã¦æ®ããªãããããè¯ãããã«æãã¾ãã ãã ãããé©åãªæéãããã¦è¿ããã¨ããé¨åã解決ã§ããªãããã§ãæ¡ç¨ãè¦éããã¨ã«ãã¾ããã
ããã§ãµã¨ã sleep(1);
ãããã ãã§ããã°ãPHPã§æ¸ãã¦ãã¾ãã°ããã®ã§ã¯ï¼ã¨æãç´ããPHPã§ä½ã£ã¦ã¿ããã¨ã«ãã¾ããã
PHPãªãæ®æ®µããæ¸ãã¦ãã¾ãã®ã§ããã£ããã¢ããã®ã³ã¹ãã¯ä¸è¦ã§ãã
FastRouteã§è¡çºã«é«éãªã¢ãã¯ãµã¼ãã¹ãå®è£ ãã
ç°¡åãªPHPã§æ¸ããã ããã¨é«ãæ¬ã£ã¦ãããããç¹ã«ã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã¯ä½¿ç¨ããã«å®è£ ãã¯ããã¾ããã
ä»åç¨æãããã¢ãã¯ãµã¼ãã¹ã®ã¨ã³ããã¤ã³ãã8ã¤ã»ã©ããã¾ãã ããã¤ãä¼¼ããã®ã¯ãããã®ã®ãããªã¨ã¼ã·ã§ã³ã®ããURIã§ãã£ããããã«ã¼ãã£ã³ã°ãããã¨ãããªã¨æãã¾ããã
ãã㧠FastRoute ã使ããã¨ã«ãã¾ãããããã§ã«ã¼ã«ã®ä¸å®ããªãã¨ã³ããã¤ã³ãã§ããä»®ã«ã¨ã³ããã¤ã³ããå¢ããå ´åã«ãæ¡å¼µã容æã«ãªãã¾ããããã¶ã楽ã«ãªãã¾ããã
<? $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { $r->addRoute('GET', '/users', 'get_all_users_handler'); // {id} must be a number (\d+) $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler'); // The /{title} suffix is optional $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler'); });
ãããªæãã§ç°¡åã«æ¸ãã¾ãããï¼ãµã³ãã«ããå¼ç¨ï¼
ãã¨ã¯ãäºåã«èª¿æ»ãã¦ãããè³æããã¨ã«ãã¨ã³ããã¤ã³ããã¨ã®ã¬ã¹ãã³ã¹ã¨å¿çæéãè¨å®ãã¦ããã¾ãããããã¯å°éãªä½æ¥ã§ãã
ä¸éãæ¸ãä¸ãã£ãã¨ããã§éçºç°å¢ã®æ¥ç¶å ãå¤æ´ããçé確èªãè¡ãã¾ããHTTPãªã¯ã¨ã¹ããããã使ç¨ãã¦ããç®æãªã©ã®ããã¤ãã®ä¿®æ£ãè¡ããã¤ãã«æ³¨æãå®äºã§ããããã«ãªãã¾ããã
ããã§ãè¨ç½®ãã¦composer installããã ãã§ä½¿ããã¢ãã¯ãµã¼ãã¹ã®å®æã§ãã
å®æ¦æå ¥
è² è·è©¦é¨å®æ½ã®éã«ã¯ãAWSã®EC2ã«è¨ç½®ããæå¾ éãã®ã¬ã¹ãã³ã¹ãè¿ãã¦ãããã確èªããªãã使ãã¾ããã
試é¨ä¸ãä½åº¦ãæå¾ éãã®ã¬ã¹ãã³ã¹ãåºãªããã¨ãæ¤ç¥ããããã¨ãããã¾ããããApacheã®è¨å®å¤ã®èª¿æ´ãªã©ãè¡ããã¨ã§æå¾ éãã®å½¹å²ãæããã¦ããã¾ããã
ã¢ãã¯ãµã¼ãã¹ãªã®ã§ãå®å¨ããã¯ã¬ã¸ããã«ã¼ãçªå·ã使ãå¿ è¦ããªããã°ãã¡ã¼ã«ã¢ãã¬ã¹ã®éè¤ãæ°ã«ããå¿ è¦ãããã¾ããã ã·ã³ãã«ãªãã¹ãã·ããªãªãç¹°ãè¿ãå®è¡ãããã¨ãã§ãã便å©ã«ä½¿ç¨ãããã¨ãã§ãã¾ããã
ã¾ã¨ã
- æä¾ããã¦ããæ¤è¨¼ç°å¢ã使ããã«èªåã§ã¢ãã¯ãµã¼ãã¹ãä½æã使ç¨ãããã¨ããè² è·è©¦é¨æã®é¸æè¢ã¨ãã¦ã¢ãªã ã¨æãã¾ããã
- ã¢ãã¯ãµã¼ãã¹ãèªç±ã«ã§ãããã¨ã§ãã¢ããªã±ã¼ã·ã§ã³ããã¹ãã·ããªãªãã·ã³ãã«ã«ã§ããç¹ãè¯ãã¨æãã¾ããã
- ä»å¾ãä»ã®å¤é¨ãµã¼ãã¹é£æºã«ããããé¨åã®è² è·è©¦é¨ãè¡ãéããåãè¦é ã§ã¢ãã¯ãµã¼ãã¹ãæ´»ç¨ãããã¨ãã§ãããã§ãã
æå¾ã«
ããã¾ã§3åã«ããããè² è·è©¦é¨é¢é£ã®ãããã¯ãç´¹ä»ããã¦ããã ãã¾ãããèå³ãããæ¹ã¯ãéå»ã®è¨äºãåç §ããã ããã¨å¬ããã§ãã
ããã©ã¼ãã³ã¹ã«é¢ããæ¹åã«çµããã¯ãªããã¾ãå¥ã®èª²é¡ã«åãã¦åãçµãã§ããæä¸ã§ãã BASEã§ã¯ãã«ã¼ãã®ããã©ã¼ãã³ã¹ã¢ããã«èå³ãããæ¹ããå¾ ã¡ãã¦ããã¾ãï¼