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

Scotty + Aeson + Persistentで作るJSON Web API

Scotty + Aeson + Persistentで作るJSON Web API

Fujimura Daisuke

July 07, 2012
Tweet

More Decks by Fujimura Daisuke

Other Decks in Programming

Transcript

  1. Scotty + Aeson + PersistentͰ࡞ΔJSON Web API
    גࣜձࣾAiming ౻ଜେհ
    @ Yesodษڧձୈ2ճ, 2012-07-07

    View full-size slide

  2. ࣗݾ঺հ
    • ౻ଜେհ
    • http://fujimuradaisuke.com @ffu_
    • גࣜձࣾAimingॴଐ
    • Haskell޷͖ͷRuby on RailsϓϩάϥϚʔ
    • ࠷ۙ͸JavaScript/CoffeeScript΋Α͘ॻ͍͍ͯΔ
    • ΞδΞͷྉཧͱΞϝϦΧͷϏʔϧɺΞϧβεͷϫΠϯ͕޷͖
    • ϥςϯΞϝϦΧจֶ΋޷͖

    View full-size slide

  3. ͱ͜ΖͰɺͳͥࠓJSON Web APIͳͷ͔
    • ͳΜ͔ͩ࠷ۙJSON Web APIΛ࡞Δػձ͕ଟ͍
    • ҰͭͷΞϓϦέʔγϣϯΛෳ਺ΫϥΠΞϯτͰಈ͔͢ࣄྫ͕૿͖͑ͯͨ
    • Mobile, PC, iOS/Android Nativeશ෦ʹల։ͱ͔
    • ϑϩϯτΤϯυͰUIΛߏங͢Δٕज़ͷൃୡɾਁಁ
    • ΫϥΠΞϯτɾαʔόʔͷ෼ۀ͸ਐΈͦ͏

    View full-size slide

  4. ಥવͰ͕͢Yesod͸͍·͍ͪͩͱࢥ͏
    • ࠷ۙWebσβΠϯ/ϑϩϯτΤϯυ։ൃ͕Ұஈͱ೉͘͠ͳ͖ͬͯͨؾ͕͢Δ
    • ͳͷͰΫϥΠΞϯτʗαʔόʔͰ෼ۀ͕Ͱ͖ͳ͍ͱݫ͍͠
    • Έͳ͞ΜHamletͱCassiusͰϓϩμΫγϣϯϨϕϧͷϨεϙϯγϒͳϚʔΫ
    ΞοϓͰ͖·͔͢ʁ
    • Α͏΍͘sass/less͕ਁಁ͖ͯͨ͠ͷʹ·ͨ৽͍ٕ͠ज़Λ…
    • ͔͠΋΄ͱΜͲҰॹͩ͠…
    • Web APIͱͯ͠࢖͏ͱͳΔͱϑϧελοΫͷ౓͕ա͗Δ
    • ͳͥ࢖͏༧ఆͷͳ͍ೝূϥΠϒϥϦͷΠϯετʔϧΛٻΊΒΕΔͷ͔

    View full-size slide

  5. ͦ͜ͰWeb API͸scottyͰ࡞Ζ͏Ͱ͸ͳ͍͔
    • Web API࡞Γ͸DBΞΫηεͱϧʔςΟϯά͑͞Ͱ͖Ε͹جຊOK
    • ྫ͑͹RubyͩͱWeb API࡞Δ࣌RailsͰ͸ͳ͘sinatraͬͯͷ͸Α͋͘Δ
    • ϥΠϒϥϦͷґଘؔ܎͸গͳ͍΄͏͕Կ͔ͱ޾ͤ
    • DB͸persistentɺJSON͸aesonͰ΍ͬͯΈΑ͏

    View full-size slide

  6. scottyʹ͍ͭͯ
    • RubyͷsinatraΠϯεύΠΞͷWeb Application Framework
    • https://github.com/xich/scotty/
    • γϯϓϧͳAPI
    • Yesodͱಉ͘͡waiϕʔε

    View full-size slide

  7. aesonʹ͍ͭͯ
    • Real World HaskellͷஶऀɺBryan O'Sullivan͞Μͷ࡞ͬͨJSONϥΠϒϥϦ
    • https://github.com/bos/aeson
    • ·ΔͰJSONͷΑ͏ͳߏจͰॻ͚ΔDSL(?)͕େมศར
    • ଎͍Β͍͠

    View full-size slide

  8. persistentʹ͍ͭͯ
    • ͝ଘ஌YesodಉࠝͷORM
    • (͔͠͠ɺ”Object”ʹϚοϐϯά͞ΕͯΔؾ഑͸શ͘ແ͍ʣ
    • http://www.yesodweb.com/book/persistent
    • PostgreSQL, SQLite, MongoDB, MySQLʹରԠ
    • QuasiQuoteͰςʔϒϧఆٛɺϚΠάϨʔγϣϯ΋͋Δ
    • ܕνΣοΫ෇͖ͷSQLͬͯײ͡Ͱ࢖͍΍͍͢

    View full-size slide

  9. Ͱ͖·ͨ͠
    • ݁࿦͔Βݴ͏ͱͱͯ΋޾ͤͳ૊Έ߹ΘͤͰͨ͠

    View full-size slide

  10. persistentͱaesonͷ૬ੑ͕͢͹Β͔ͬͨ͠(1)
    • ςʔϒϧఆٛͨ͠ΒɺͦΕΛToJSON/FromJSONͷΠϯελϯεʹ͢Δ

    View full-size slide

  11. persistentͱaesonͷ૬ੑ͕͢͹Β͔ͬͨ͠(2)
    • ͢ΔͱɺDBͷߦΛJSONͱͯ͠ѻ͑Δ(ToJSONͷΠϯελϯεʹͳΔͷͰʣ
    • ͳͷͰɺscottyͰͦͷ··JSONϨεϙϯεͱͯ͠ฦͤΔ

    View full-size slide

  12. persistentͱaesonͷ૬ੑ͕͢͹Β͔ͬͨ͠(3)
    • ࠓ౓͸ςʔϒϧͱಉ͡εΩʔϚͷJSONΛPOST͢Δͱɺ
    • ͦͷ··ߦͱͯ͠DBʹೖΕΒΕΔʢFromJSONͷߦͱͯ͠ड͚औΕΔ͔Βʣ

    View full-size slide

  13. ͜Ε͸͔ͳΓΤϨΨϯτͳͷͰ͸
    • ͜ͷهड़ྔͰDB͔Βͷऔಘ/DB΁ͷ௥Ճ͕Ͱ͖·͢
    • ͍͢͝ʂ

    View full-size slide

  14. ΩϥʔϥΠϒϥϦͷ༧ײ
    • ґଘؔ܎͕গͳ͍ͷͰίϯύΠϧ΋ָ
    • ܕΫϥεΛ׆͔ͤΔ
    • Web APIͳΒHaskellͰ΋ΫϥΠΞϯτ։ൃͱͷڠಇ΋໰୊ͳ͠
    • Ή͠ΖϥϯλΠϜΤϥʔ͕ແ͍ͷͰΫϥΠΞϯτ։ൃ͸޾ͤ

    View full-size slide

  15. ༨ஊ
    • ͪͳΈʹҐஔ৘ใܥͷWebαʔϏεΛ࡞Ζ͏ͱͯ͠·ͨ͠
    • ΫϥΠΞϯτ͸Backbone.js, CoffeeScriptɺGoogle Maps͋ͨΓΛར༻
    • ࣮ࡍͷͱ͜ΖɺΫϥΠΞϯτͷίʔυΛॻ͍ͯΔํ͕௕͔ͬͨ…
    • HTMLςϯϓϨʔτ͸mustacheΛ࢖ͬͨɻ͜Ε΋ྑ͔ͬͨ
    • ·ͩෳࡶͳ͜ͱΛ΍ͬͯͳ͍ͷͰɺམͱ͕݀͋͠Γͦ͏
    • ೝূ·ΘΓ͸·ͩະணखɻམͱ݀͠͸͔͜͜…ʁ
    • Template HaskellΛ࢖ͬͨΒToJSON/FromJSONʹ͢Δͷ΋ࣗಈͰͰ͖ͦ͏ͳ
    ؾ͕…

    View full-size slide

  16. ͓ΘΓ
    • ιʔείʔυ https://github.com/fujimura/spot
    • ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ

    View full-size slide