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

Haskell/Servantで行う安全かつ高速なAPI開発

 Haskell/Servantで行う安全かつ高速なAPI開発

Haskellを業務に導入し、ガッツリ使っている経験をもとに、技術スタックや開発スタイルを共有しつつ、Haskellのメリットや「何ができるのか?」を話します。
また、ライブラリの充実度も共有します。

お問い合わせはこちらのtwitterアカウントへ: @nakaji_dayo

Daishi Nakajima

November 10, 2018
Tweet

More Decks by Daishi Nakajima

Other Decks in Technology

Transcript

  1. 4FSWBOU "1*ͷܕ શମ͕ܕͰܨ͕ͬͨঢ়ଶ 3FRVFTU %# )33 )BOEMFS 4FSWJDF 7JFX 3FTQPOTF

    &OUJUZ )551 2VFSZ ܕͷ֎ͷੈք ܕͷ֎ͷੈք Y ઃܭ্ͷ੍໿ υΩϡϝϯτ౳ͷੜ੒
  2. "1*ΛܕͰఆٛ w ϢʔβʔҰཡͷऔಘ DVSM9(&5VTFST QBHF  w Ϣʔβʔ৘ใͷߋ৽ DVSM91"5$)VTFSTE\OBNFGPP^ type

    API = "users" :> QueryParam "page" Int :> Get '[JSON] GetUsersResponse :<|> "users" :> Capture "id" Int :> ReqBody '[JSON] PostUsersBody :> Patch '[JSON] Int type MyMap = Map Int String
  3. "1*ΛܕͰఆٛ w ϢʔβʔҰཡͷऔಘ DVSM9(&5VTFST QBHF  w Ϣʔβʔ৘ใͷߋ৽ DVSM91"5$)VTFSTE\OBNFGPP^ type

    API = "users" :> QueryParam "page" Int :> Get '[JSON] GetUsersResponse :<|> "users" :> Capture "id" Int :> ReqBody '[JSON] PostUsersBody :> Patch '[JSON] Int type MyMap = Map Int String 1BUI4FHNFOU΍1BSBNFUFS౳ ΤϯυϙΠϯτͷߏ੒Λͭͳ͙ ΤϯυϙΠϯτಉ࢜Λͭͳ͙ ୯७ͳUZQFBMJBT
  4. ରԠ͢Δ࣮૷ getUsers :: Maybe Int -> Handler GetUsersResponse getUsers mpage

    = undefined patchUser :: Int -> PatchUsersBody -> Handler Int patchUser userId body = undefined api :: Server API api = getUsers :<|> putUsers aఆٛͱ࣮૷ͷҰக͕อূ͞Ε͍ͯΔ
  5. )BOEMFSʹ͍ͭͯ w ෭࡞༻ͷ͋Δܭࢉ w #BE3FRVFTU౳ͷྫ֎ΛUISPX Handler runHandler' :: ExceptT ServantErr

    IO a f :: Handler String f = do rs <- liftIO $ httpRequest "who am i" when (rs == YouAreTeapot) $ throwError err418 return $ “my name is " ++ rs
  6. ࣮ࡍʹ"1*Λఆٛ͢Δ data HogeRequest = HogeRequest { _id :: Int64 ,

    _name :: String } type API = "hoge" :> ReqBody '[JSON] HogeRequest … api = undefined 3FRVFTU#PEZ΍3FTQPOTFͷܕ΋ఆٛ ΤϯυϙΠϯτΛఆٛ ͍͔ͭ͘ͷܕΫϥεͷΠϯελϯεʹ͓ͯ͘͠ UP+40/ GSPN+40/ %FGBVMU 5P4DIFNB "SCJUSBSZ FUD
  7. )BOEMFSͷ֦ு w )BOEMFSʹඞཁͳϞφυΛ߹੒͢Δ newtype AppM a = AppM { runAppM

    :: ReaderT Context Handler a } deriving (...) 3FBEFS άϩʔόϧʹࢀর͢Δ஋Λѻ͏  ઃఆ஋ɺ$POOFDUJPO1PPM 5SBOTBDUJPO؅ཧ ౳ 3FBEFSΛ߹੒
  8. ܕͰઃܭ্ͷ੍໿Λߦ͏ queryM :: MonadService m => a -> m b

    … renderUser :: MonadView m => User -> m UserView renderUser x = do imgs <- queryM UserImages x … ίϯύΠϧΤϥʔ
  9. 3&1-ͰԥΔ > runAppM’ $ postMessagesR $ def & text .~

    “hello” Right (Message {_id = 123, _userName = “hoge”, image=… postMessagesR :: PostMessageBody -> AppM Message ࣍ͷ)BOEMFSʢΤϯυϙΠϯτʣΛςετ͍ͨ͠ HIDJͰ؆୯ʹ࣮ߦ
  10. 4FSWBOU "1*ͷܕ શମ͕ܕͰܨ͕ͬͨঢ়ଶ 3FRVFTU %# )33 )BOEMFS 4FSWJDF 7JFX 3FTQPOTF

    &OUJUZ )551 2VFSZ ܕͷ֎ͷੈք ܕͷ֎ͷੈք Y ઃܭ্ͷ੍໿ υΩϡϝϯτ౳ͷੜ੒
  11. ଞɺར༻͍ͯ͠ΔϥΠϒϥϦ TFSWBOUTFSWFS TFSWBOUBVUITFSWFS TFSWBOUNPDL TFSWBOUNVMUJQBSU TFSWBOUTXBHHFS IUUQUZQFT JNBHFUZQF XBJ XBJDPST

    XBSQ 8FC BFTPO EBUBEFGBVMUDMBTT MFOT VVJE ZBNM UZQFMFWFMTFUT VOPSEFSFEDPOUBJOFST DBTTBWB WFDUPS QEGUPPMCPYEPDVNFOU VOJDPEFTIPX NJNFNBJM DPOUBJOFST σʔλ KPTF ҉߸ )%#$QPTUHSFTRM )%#$TFTTJPO SFMBUJPOBMRVFSZ SFTPVSDFQPPM TRMXPSET 3%# HPHPM HPHPMDPSF pSFCBTFBENJO BNB[POLB BNB[POLBDPSF BNB[POLBT BNB[POLBTFT IUUQDMJFOU IUUQDPOEVJU DPOOFDUJPO TTJHOFS CMPPEIPVOE ଞΫϥΠΞϯτ IFUFSPDFQIBMVT CMB[FNBSLVQ ςϯϓϨʔτ LBUJQ NPOBEMPHHFS ϩΨʔ
  12. ·ͱΊ w ҆શ w ੩తܕ෇͚ɺ෭࡞༻ͷ෼཭ʹΑΓόά๷͙ w %#ͳͲͱͷ݁߹Ͱͷόά΋๷͙ w υΩϡϝϯτͱ࣮૷ͷᴥᴪΛى͜͞ͳ͍ w

    ߴ଎ w ந৅౓ͷߴ͍ɺσʔλɾϩδοΫΛදݱɾ࣮૷ w ࣗ໌ͳςετΛॻ͔ͳͯ͘ྑ͍ঢ়ଶʹ͢Δ w ܕʹΑΔ੍໿ʹΑΓɺίʔυϨϏϡʔͷίετΛԼ͛Δ w 3&1-ͳͲ΋࢖ͬͨॊೈͳ։ൃελΠϧ