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

Java ラブなヌーラボにおける Scala + Playframework 体験記

Java ラブなヌーラボにおける Scala + Playframework 体験記

株式会社ヌーラボ

February 21, 2015
Tweet

More Decks by 株式会社ヌーラボ

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • Tsuyoshi Yoshizawa (@ussy00) • גࣜձࣾψʔϥϘ • 2009 -

    2012 • डୗ։ൃ / Backlog / Cacoo • 2013 - • Typetalk (project leader)
  2. ψʔϥϘͷྺ࢙ 2004 2006 2010 2014 Nulab Backlog Cacoo Backlog API

    v2 Java Java / Seasar2 Java / Seasar2 Scala / Play2 Typetalk Nulab Account Java8 / Spring Nulab Status Go
  3. Seasar2 ͱͷܾผ • 2012 ೥ Typetalk ϓϩτλΠϓΛझຯͰ։࢝ • Seasar2 ϝϯςφϯεϞʔυ

    • RESTful/Web API/WebSocket ਐԽ͸ࢭ·Βͳ ͍ • ଞͷϑϨʔϜϫʔΫΛ୳͢͜ͱʹ
  4. ϑϨʔϜϫʔΫ୳ࡧ • Ruby on Rails • ੩తܕ෇͚ʹ׳Ε͖ͬͯ͠·͍ͬͯͨ • ࣗ෼Ͱϝϯς͠ଓ͚ΒΕΔࣗ৴͕ͳ͔ͬͨ •

    Spring Framework (Spring MVC / JAX-RS) • Seasar2 Λஔ͖׵͑Δʹ࠷దͰ͸͕͋ͬͨɺ෺଍Γ ͳ͕͋ͬͨ͞
  5. • Playframework 1 ͷ޷͖উख͞͸஌͍ͬͯͨ • Scala ΛϝΠϯͱͨ͠ Web ϑϨʔϜϫʔΫ •

    Playframework 2.0 ਖ਼ࣜϦϦʔε௚લ • ৽نϓϩδΣΫτͰࢼߦࡨޡ͠΍ܹࢗ͘͢త
  6. RESTful GET  /users/:id  controllers.UserController.find(id:  Long) object  UserController  extends  Controller  {

         def  find(id:  Long)  =  Action  {  implicit  request  =>          User.findById(id).map  {  user  =>              Ok(Json.obj(                  “id”  -­‐>  user.id,                  “name”  -­‐>  user.name              ))          }.getOrElse  {              NotFound(Json.obj(                  “message”  -­‐>  “User  not  found”              ))          }      }   } routes controller
  7. Hot Reloading • ։ൃ͕͠΍͍͢ • LL ͷੈքͰ͸౰ͨΓલ • dev ϞʔυͰແઃఆͰಈ࡞͢Δ

    • Seasar2 ಉ༷ ClassLoader Λࠩ͠ସ࣮͑ͯݱ • play.runsupport.Reloader (2.4.x)
  8. Type-safe View • HTML Λฦͯ͠ JSON Λऔಘ͢Δ࣌୅͚ͩΕͲ΋ • ܕͷߏ଄Λมߋͨ͠ΒΤϥʔʹͳΔ҆৺ײ •

    ϝʔϧςϯϓϨʔτʹ΋࢖༻Մ • HTML ίʔμʔʹͱͬͯ͸ਏ͍ @(account:  Account)   <span  class="name">@account.name</span>
  9. Type-safe Reverse Routing ϝιουͷγάωνϟ͕ มΘΕ͹ίϯύΠϧΤϥʔ GET  /users/:id  controllers.UserController.find(id:  Long) <a

     href=“@routes.UserController.find(user.id)”>@user.name</a> template routes val  url  =  routes.UserController.find(user.id) Scala
  10. ޓ׵ੑ໰୊ • มߋʹޙํޓ׵ੑ͕ͳ͘ͳΔ͜ͱ͕͋Δ • Ϧʔυίϛολʔ͕ James Roper ͞Μ (Typesafe) ʹ

    ͳͬͯྑ͍ํ޲ʹ޲͔͍ͬͯΔ • ৽͍͠όʔδϣϯ͕ग़ͨΒɺͱʹ͔͘௥ਵ • ࣗ෼ͨͪͰ޻਺Λ֬อͰ͖Δ͔Βɺ΍Γ΍͔ͬͨ͢
  11. Build front-end • Playframework 2.3 SBT Web • ඇެࣜ Plugin

    ͷΞοϓάϨʔυͷอোੑ • Grunt/Gulp ʹൺ΂Δͱຬ଍͍͔ͳ͔ͬͨ • ΤίγεςϜʹ৐Δ΂͘ Gulp ʹશ໘Ҡߦ
  12. Build front-end with SBT • PlayRunHook Λ࣮૷͢Δ͜ͱͰɺ SBT ͱ࿈ܞ͞ ͤͯɺىಈ࣌ʹ

    grunt/gulp Λ࣮ߦ • http://hakobe932.hatenablog.com/entry/ 2014/04/02/220457 • https://www.playframework.com/ documentation/2.4.x/SBTCookbook
  13. Option def  findLang(lang:  String):  Lang  =   supportLangs.find  {  l

     =>  l.code  ==  lang  }.getOrElse(english)   findLang(“ja-­‐JP”)   • NullPointerException ͷ࣮ߦ࣌ྫ֎͕ൃੜ͠ͳ͍ • Scala ͸ݩ͔Β Option ͕͋ΔઃܭͳͨΊɺ֤ϥ ΠϒϥϦ͕ Option Λฦͯ͘͠ΕΔ҆৺ײ
  14. case class case  class  User(id:  Long,  name:  String,  isAdmin:  Boolean)

      val  newUser  =  User(1L,  “yoshizawa”,  false)   val  updatedUser  =  newUser.copy(name  =  “tsuyoshizawa”) • σϑΥϧτͰಡΈऔΓઐ༻ϓϩύςΟ͕෇༩ • equals/hashCode/toString/copy … ϝιουΛੜ੒ • copy ͰݩͷΦϒδΣΫτΛഁյͤͣʹίϐʔ • pattern match ΁ద༻ՄೳʹͳΔ
  15. pattern match • Option ͷ matching Ͱ໢ཏ͞Εͯͳ͍৔߹͸ܯࠂ • ΦϒδΣΫτΛ෼ղͯ͠஋ΛऔΓग़͢͜ͱ͕Ͱ͖Δ findById(id)

     match  {      case  Some(user)  if  user.isAdmin  =>  “administrator”      case  Some(user)  =>  “normal  user”      //  case  _  =>  “guest  user”  warning!   }   val  r  =  “""(\d{4})-­‐(\d{2})-­‐(\d{2})""".r   “2015-­‐02-­‐22”  match  {      case  r(year,  month,  day)  =>  something(year,  month,  day)      case  _  =>  throw  new  Exception(“format  exception”)   }   if Ψʔυઅ͕͋Δ ৔߹͸ବ໨
  16. ࣮ߦ࣌ྫ֎͕ൃੜ͢Δέʔε • Java • ϓϩάϥϚʔ͕ྫ֎ͳ஋ (null) Λฦ͞ΕΔ͜ͱΛ ༧૝Ͱ͖ͳ͔ͬͨ • Scala

    • Option#get, List#head Λҙਤతʹݺͼग़͢ Scala Ͱهड़͢Δ͜ͱͰέΞϨεϛεΛݮΒͤΒΕΔ
  17. Self annotation • API ϑΝʔετͰ։ൃ • ॲཧ͸ڞ௨Խ͠ೝূΛ Web ͱ API

    Ͱ෼͚͍ͨ • ೝূॲཧΛ trait ʹ࣮૷͠ɺॊೈʹ૊ΈࠐΈ͕Ͱ͖ͨ trait  MessageController  extends  Controller  {      self:  Auth  =>      //  API  methods   }   object  MessageWebController  extends  MessageController  with  WebAuth   object  MessageApiController  extends  MessageController  with  APIAuth ೝূํࣜΛޙ͔Βܾఆ
  18. Scala ϥΠϒϥϦͷधཁ • Typetalk / Backlog API v2 API ެ։

    • ࣮૷࣌ʹ Scala ޲͚ͷ OAuth2 Provider ϥΠϒϥϦ ͕ͳ͔ͬͨ • Github ʹެ։ • https://github.com/nulab/scala-oauth2-provider
  19. όΠφϦޓ׵ੑ໰୊ • ϝδϟʔόʔδϣϯ͕มΘΔͱόΠφϦޓ׵ੑ͕ ͳ͘ͳΔ • ಛఆϥΠϒϥϦʹҾ͖ͣΒΕ Scala ͷϝδϟʔ όʔδϣϯΞοϓ͕؆୯Ͱͳ͍৔߹͕͋Δ (2.9

    - > 2.10 -> 2.11 ͱରԠ͖ͯͨ͠ʣ • ར༻͍ͯ͠ΔϥΠϒϥϦʹ PR Λग़͍ͯ͘͠ • Ұ࣌తʹΠϯϋ΢εϦϙδτϦʹ্͛ͨΓ΋
  20. ίϯύΠϧ଎౓໰୊ • ผϓϩδΣΫτɺαϒϓϩδΣΫτԽ • Α͍εϖοΫͷ PC Λߪೖ͠ʢͯ΋Β͍ʣ·͠ΐ͏ • ScalaDoc ͕͍Βͳ͍ϓϩδΣΫτͰ͋Ε͹

       sources  in  (Compile,  doc)  :=  Nil      publishArtifact  in  (Compile,  packageDoc)  :=  false build.sbt
  21. clean compile (౰ࣾൺ) 0 50 100 67s 90s MBA 11-inch

    Mid 2012 (2.0 GHz intel Core i7) MBP Retina 15-inch Early 2013 (2.7 GHz Intel Core i7) ։ൃ࣌͸ຖճΫϦʔϯίϯ ύΠϧ͢ΔΘ͚Ͱ͸ͳ͍ SBT ͕ࠩ෼ίϯύΠϧͯ͠ ͘ΕΔ
  22. Scala ͬͯ೉ͦ͠͏ • ࣮ϓϩδΣΫτൃ଍࣌ʹ Scala ະܦݧϝϯόʔ ͕ࢀՃ • better Java

    Ͱ։࢝ • Scala ͷΑ͍ػೳͱෳࡶ͞ͷόϥϯεΛऔΔ • ίʔυϨϏϡʔͰࢦఠ͢Δ • ྑॻΛखʹऔΔ
  23. ·ͱΊ • Playframework + Scala ʹΑͬͯҎલΑΓ΋Α͍։ൃ ؀ڥ͕खʹೖͬͨʢಛผͳઃఆͳ͠ʹʣ • Java ΑΓ΋ݎ࿚ͳϓϩάϥϛϯά͕Մೳ

    • Scala ʹܞΘͬͨ։ൃऀ͕ͭ͘Δ৽͍͠ΞϓϦ ʢπʔϧʣ͸ɺ Scala ͕બ͹ΕͯΔ • ։ൃνʔϜɺҊ݅ΛݟۃΊͯಋೖͰ͖Ε͹Α͍