Haskellã®Yesodã§Webã¢ããªéçºå ¥é (1)
ãã®è¨äºã«ã¤ãã¦
ãµã¨æãç«ã£ã¦Haskellã§Webã¢ããªéçºãããããªã¨æã£ãã¨ãããä¹ãé ãã¦ããæã¯å¦ããªããã®ã®ãYesodã¨ãããã¬ã¼ã ã¯ã¼ã¯ãä¸çªæåãããã®ã§è§¦ã£ã¦ã¿ããã¨ã«ããã
ãã®è¨äºã¯ãç°å¢æ§ç¯ãªã©ã®æé ãå¿ãã¦ãã¾ãã¨å¿ä½ç¡ãã®ã§ãè¸ãã æé ãã¡ã¢ãã¦ããããã®ãã®ã§ãã
ãªã®ã§ãä¸å¿å°æ¥çã«ã¯Esqueletoã使ã£ã¦ã¡ããã¨ããTDDã§RESTfulãªWebã¢ããªã«ãããã¨æã£ã¦ãã¾ãããæ°åãå¤ããã°éä¸ã§æ¸ãã®ãããããããããªãã®ã§ãå ¥éè¨äºã¨ãã¦ã¯æå¾ ã§ããªããã¨æãã¾ãã
â» 2014å¹´9æ13æ¥ã« Mac OS XのためのHaskell開発環境 - プログラミング芸術論 ã¨ããè¨äºãæ¸ããé¢ä¿ã§ã使ç¨ãã¼ã¸ã§ã³ãå°å ¥æ¹æ³ã®æ å ±ãæ´æ°ãã¾ããã
ç°å¢
å½åã¯æå ã®MacBookã使ãã®ã§ã
- Mac OS X Mavericks (10.9.4)
- GHC version 7.8.3
- Yesod version 1.2.12.8
- Cabal library version 1.20.0.3
- cabal-install version 1.20.0.2
ãªç°å¢ã§ã®ã話ã§ãã
ã¡ãªã¿ã«ãçè ã®ã¹ããã¯ã¯
- O'Raillyã®Real World Haskellãããç¨åº¦ã¾ã§èªãã ããããå 容å¿ãã
- Webã¢ããªã±ã¼ã·ã§ã³éçºã¯Ruby on Rails Tutorialãèªãã§ãã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ããåç¨ããã°ã©ãã³ã°ãå°ãã ãå¿ è¦ã«è¿«ããã¦ãã£ããã¨ããã
ã¨ããç¨åº¦ãªã®ã§ã解説ã¯ã»ã¼ã§ããªããåç¶ã§ãã
éçºç°å¢ã®æ§ç¯
GHCãå§ãã¨ããHaskellã®éçºç°å¢ãcabalãªã©ã®ä½¿ãæ¹ã«ã¤ãã¦ã¯ Mac OS XのためのHaskell開発環境 - プログラミング芸術論 ã«ã¾ã¨ãã¦ããã¾ãã®ã§ããã¡ããã覧ãã ããã
Yesodã®ã¤ã³ã¹ãã¼ã«
ã¾ããYesodãã°ãã¼ãã«ã«ã¤ã³ã¹ãã¼ã«ãã¾ãã
ãã¨ãã¨ã¯ããã¸ã§ã¯ãã®sandboxãä½æãã¦ããYesodããã¼ã«ã«ã®ã¤ã³ã¹ãã¼ã«ãè¡ãã ãã«ãã¦ããã®ã§ãããããã ã¨Yesodã®å®è¡ãã¡ã¤ã«ã¸ã®ãã¹ãéããã«cabal exec yesod devel
ãåããªãã£ããããYesodã ãã¯ã°ãã¼ãã«ã«ã¤ã³ã¹ãã¼ã«ããããã«ãã¾ããã
ãã®cabal exec yesod devel
ã«ãã¹ãéããªã件ã¯Yesodã®Githubã¬ãã¸ããªã®issue Cannot use yesod-bin from within the cabal sandbox · Issue #737 · yesodweb/yesod · GitHub ã§ãè°è«ããã¦ãã¾ããããã®è¨äºãæ¸ãã¦ããç¾å¨ã§ã¯æå¹ãªè§£æ±ºçã¯è¦ã¤ãã£ã¦ããªããããªã®ã§ãããã¸ã§ã¯ãå
ã§ä½¿ãããã«è¨2åYesodã®é·æéã³ã³ãã¤ã«ãå³ãããã¨ã¨ãªãã¾ãããã°ãã¼ãã«ã«ã¤ã³ã¹ãã¼ã«ããããæ¹ãä¸çªåé¡ãªãæ¸ããã¨æãã¾ãã
$ cabal update $ cabal install yesod-bin --max-backjumps=-1 --reorder-goals -j
reorder-goals
ã¯ä»ã®ããã±ã¼ã¸ã®ä¾åãã¼ã¸ã§ã³ãªã©ã®é¢ä¿ã§ã¤ã³ã¹ãã¼ã«ããããã±ã¼ã¸ã®ãã¼ã¸ã§ã³ããããªãå ´åã«é å¼µã£ã¦ããããªãã·ã§ã³ã§ã--max-backjumps
ã«-1
ãæå®ãããã¨ã§ãã®è©¦è¡åæ°ãç¡å¶éã«è¨å®ãã¾ãã
ããããã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããã®ã§ãé©ãã»ã©æéãæããã¾ãã
ã¡ãªã¿ã«ç§ã®ç°å¢ã§ã¯ä¸è¨cabal install
å®è¡æã«haskell-platformã«ãã£ã¦ã¤ã³ã¹ãã¼ã«ããã¦ããalexã¨happyã使ããã¨ãã¦ãã¼ã¸ã§ã³ãå¤ãã¦ã¤ã³ã¹ãã¼ã«ãã³ã±ã¦ãã¾ã£ãã®ã§ãå¥åã«cabal install alex happy -j
ã¨ãã¦ããå¿
è¦ãããã¾ããã
ããã¸ã§ã¯ãã®ä½æ
æ°ããããã¸ã§ã¯ãã¯yesod init
ã§ä½æã§ãã対話å¼ã§ããã¸ã§ã¯ãåã¨ä½¿ç¨ãããã¼ã¿ãã¼ã¹ã½ãããå°ãããã¾ãã
$ yesod init Welcome to the Yesod scaffolder. I'm going to be creating a skeleton Yesod project for you. What do you want to call your project? We'll use this for the cabal name. Project name: yesod-sample Yesod uses Persistent for its (you guessed it) persistence layer. This tool will build in either SQLite or PostgreSQL or MongoDB support for you. We recommend starting with SQLite: it has no dependencies. s = sqlite p = postgresql pf = postgresql + Fay (experimental) mongo = mongodb mysql = MySQL simple = no database, no auth url = Let me specify URL containing a site (advanced) So, what'll it be? mysql That's it! I'm creating your files now... --------------------------------------- ___ {-) |\ [m,].-"-. / [][__][__] \(/\__/\)/ [__][__][__][__]~~~~ | | [][__][__][__][__][] / | [__][__][__][__][__]| /| | [][__][__][__][__][]| || | ~~~~ ejm [__][__][__][__][__]__,__, \__/ --------------------------------------- The foundation for your web application has been built. There are a lot of resources to help you use Yesod. Start with the book: http://www.yesodweb.com/book Take part in the community: http://yesodweb.com/page/community It's highly recommended to follow the quick start guide for installing Yesod: http://www.yesodweb.com/page/quickstart If your system is already configured correctly, please run: cd yesod-sample && cabal install -j --enable-tests --max-backjumps=-1 --reorder-goals && yesod devel
ãªãã人åºã¦ãã...
ä»åã¯yesod-sampleã¨ããååã®ããã¸ã§ã¯ãã«ãã¦ã使ç¨ããDBMSã¯MySQLãé¸ã³ã¾ããã
ä¾åããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«
ã¢ããªã±ã¼ã·ã§ã³ãèµ·åããã«ã¯ãä¾åããã±ã¼ã¸(åã³Yesodã®å ¨ä½ãå«ã...)ã®ã¤ã³ã¹ãã¼ã«ã¨ããã¸ã§ã¯ãå ¨ä½ã®ã³ã³ãã¤ã«ããã³ã¤ã³ã¹ãã¼ã«ãããå¿ è¦ãããã¾ãã
yesod init
ã®æå¾ã«åºã¦ããã³ãã³ãã§ã¯ã°ãã¼ãã«ã«ããã¸ã§ã¯ããã¤ã³ã¹ãã¼ã«ãã¦ãã¾ã£ã¦ãã¾ãããããã¸ã§ã¯ãã®ä¾åããã±ã¼ã¸ãã°ãã¼ãã«ã«ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã¨ä»ã®Haskellããã¸ã§ã¯ãã«å½±é¿ãåºãã®ã§ãcabalã®sandboxæ©è½ãæå¹åãã¦ããYesodãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ cd yesod-sample $ cabal sandbox init $ cabal install -j --enable-tests --max-backjumps=-1 --reorder-goals
ä¸ã®ã³ãã³ããå©ãã ãã§èªåçã«ããã¸ã§ã¯ãå ¨ä½ã®ã³ã³ãã¤ã«ãè¡ãããä¸ã§ãããã¸ã§ã¯ãå ¨ä½ãä»ã®cabalã§ã¤ã³ã¹ãã¼ã«ããã©ã¤ãã©ãªã¨åãããã«ã¤ã³ã¹ãã¼ã«ãããç¶æ ã«ãªãã¾ãã
--enable-tests
ã¯ãã¹ãã®ããã®ä¾åããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãããªãã·ã§ã³ã§ãã
yesod-sample.cabal
ãè¦ãã¨ä¾åããã±ã¼ã¸ãã³ã³ãã¤ã«æã«ä½¿ç¨ããããªãã·ã§ã³ãªã©ãããã©ã«ãã§ã©ãè¨å®ããã¦ããããããã¾ãã
ã¡ãªã¿ã«ãbuild-depends
ã®è¨è¿°ã«ããpersistent
ã£ã¦ããã®ããYesodã使ãéã«ç¾ç¶æãããã¥ã©ã¼ãªO/R mapperã¿ããã§ãããMySQLã ãã®æ§æãªã©ã«ã¯å¼±ãã¿ãããªã®ã§ãããããã¯ãã®æ¡å¼µã®Esqueletoã使ã£ã¦ã¿ãããªã¨æã£ã¦ãã¾ãã
ã§ããããããµã¼ãã®èµ·åã§ãããä»åã¯ãã¼ã¿ãã¼ã¹ã½ããã«MySQLãæå®ãã¦ãã¾ã£ãã®ã§ãå ã«ãã®è¨å®ãè¡ãå¿ è¦ãããã¾ãã
ãã¼ã¿ãã¼ã¹ã®è¨å®ã¨ã¢ããªã±ã¼ã·ã§ã³ã®èµ·å
ãã¼ã¿ãã¼ã¹ã®æ
å ±ã¯config/mysql.yml
ã«ãããRoRã®config/database.yml
ã¨ã»ã¼åããªã®ã§ãç¹ã«è¿·ããã¨ã¯ãªããã¨æãã¾ããã¨ããããDevelopmentç°å¢ã¨Testç°å¢ã®ãã¼ã¿ãã¼ã¹è¨å®ããã£ããã¨ãã£ã¦ãMySQLã§ã¦ã¼ã¶ã¨ãã¼ã¿ãã¼ã¹ã®ä½æãè¡ãªã£ã¦ããã°OKã§ãããã
ã¡ãªã¿ã«ããã¼ã¿ãã¼ã¹ã®è¨å®ãã¡ã¤ã«ã¯YAMLå½¢å¼ãªã®ã§ããã¹ã¯ã¼ããæ°åã ãã ã£ãã空ã ã£ããããå ´åã«ã¯ããã«ã¯ãªã¼ãã¼ã·ã§ã³ã§å²ãå¿ è¦ãããã¾ãã
è¨å®ãå®äºãããã以ä¸ã®ã³ãã³ãã§ã¢ããªã±ã¼ã·ã§ã³ãèµ·åãã¾ãã
$ yesod devel
èªåçã«ã½ã¼ã¹ã³ã¼ãããã«ããããScaffoldã«å«ã¾ãã¦ãããã¼ã¿ãã¼ã¹ãã¤ã°ã¬ã¼ã·ã§ã³ãè¡ãããå¾ãã¢ããªã±ã¼ã·ã§ã³ãµã¼ããèµ·åãã¾ãããªããçè
ã®ç°å¢ã§ã¯ãã¤ã°ã¬ã¼ã·ã§ã³ãå«ãä¸åç®ã®èµ·åã®éã«Devel application launched
ã®è¡¨ç¤ºããããªãã£ãã®ã§ãããåé¡ãªãèµ·åãã¦ãã¾ããã
localhost:3000ã«ã¢ã¯ã»ã¹ãã¦Scaffoldã®ãã¼ã ãã¼ã¸ã表示ããã¦ããã°ãèµ·åã¾ã§ã®ããã»ã¹ã¯å®äºã§ãã
ãããã«
ç¡äºç°å¢æ§ç¯ã¨Scaffoldã®ã¢ããªã±ã¼ã·ã§ã³ãèµ·åãããã¨ãã§ãã¾ããã
ãªããããã¸ã§ã¯ããåé¤ããéã«ã¯ä¾åããã±ã¼ã¸ãå ¨ã¦ããã¸ã§ã¯ããã£ã¬ã¯ããªä»¥ä¸ã«å«ã¾ãã¦ããã®ã§ãåã«ãã£ã¬ã¯ããªãåé¤ããã ãã§æ¸ã¿ã¾ãã
$ rm -r yesod-sample
次åã¯Scaffoldã§çæããããã¡ã¤ã«ã®ç¢ºèªã¨ãã³ãã©ã®è¿½å ã¾ã§ã§ãã