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

実践Server Side Swift

Yuki Takei
February 24, 2017

実践Server Side Swift

Swift実践入門刊行記念イベント Feb 24 2017

Yuki Takei

February 24, 2017
Tweet

More Decks by Yuki Takei

Other Decks in Technology

Transcript

  1. ࣮ફ Server Side Swift Yuki Takei Swift࣮ફೖ໳ץߦه೦Πϕϯτ Feb 24 2017

  2. None
  3. Twitter͸ @noppoman722 Ͱ΍ͬͯ·͢

  4. • 2015೥12݄ΑΓɺServer Side SwiftͷϥΠϒϥϦ։ൃ΍ί ϛϡχςΟ΁ͷߩݙΛελʔτ • 2016೥2݄ʹSlimaneΛϦϦʔε(ݱࡏ։ൃ͸Stop) • 2016೥3݄͔ΒTokyo Server

    Side Swift Meetup։࢝ • 2016೥4݄͔ΒOpen-SwiftɺZewoίϛϡχςΟʹࢀՃ • 2016೥10݄ΑΓServer Work GroupͷεςʔΫϗϧμʔʹ • 2017೥1݄ΑΓSwiftΛόοΫΤϯυαʔϏεͷݴޠͱͯ͠ɺ αʔϏε։ൃΛελʔτ ๻ͱServer Side SwiftͷาΈ
  5. https://tokyo-ss-swift.connpass.com/

  6. • Server Side Swiftͷ࢖͍ॴ • Server Side Swiftͷ։ൃ؀ڥΛ੔͑Δ • SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ

    • ࣮ફతͳWebΞϓϦέʔγϣϯΛ࡞Ζ͏ ΞδΣϯμ
  7. Server Side Swiftͷ࢖͍ॴ

  8. SwiftΛwebαʔϏεͰ࢖͏ϝϦοτ • ੩తݴޠͳΒͰ͸ͷ҆શੑɺॊೈͳݴޠ࢓༷ • ࣮ߦ଎౓ɺলΤω • VMɺGC͕ͳ͍ • iOSΞϓϦ։ൃʹ࢖ΘΕ͍ͯΔϥΠϒϥϦ͕ͦͷ ··࢖͑Δέʔε͕༗Δ

  9. • APIαʔόʔ(Node.js΍GoͰ࡞͍ͬͯΔͱ͜Ζ) • όονॲཧͳͲͷίϚϯυϥΠϯΞϓϦέʔγϣϯ • ը૾ॖখɺѹॖɺ҉߸ԽͳͲLLͰ͸࣌ؒͷ͔͔Δ෦෼ SwiftͷwebαʔϏεͰͷ࢖͍ॴ

  10. Server Side Swiftͷ։ൃ؀ڥΛ੔͑ͯΈΑ͏

  11. • Xcode8.2ΛࣄલʹΠϯετʔϧ͓͖ͯ͠·͠ΐ͏ ؀ڥߏஙͷલʹ….

  12. Server SideϓϩδΣΫτ͸ ίϚϯυϥΠϯΛ࢖ͬͯ࡞੒͍͖ͯ͠·͢

  13. Server Side SwiftϓϩδΣΫτͷ࡞੒ ϓϩδΣΫτ༻ͷσΟϨΫτϦ࡞੒

  14. εχϖοτ࡞੒ίϚϯυ Server Side SwiftϓϩδΣΫτͷ࡞੒

  15. Server Side Swiftͷ جຊతͳϑΝΠϧߏ੒ Server Side SwiftϓϩδΣΫτͷ࡞੒

  16. main.swiftͷத਎

  17. ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ ίϯύΠϧ & ϦϯΫ

  18. ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ .build/debugҎԼʹ࣮ߦՄೳͳϥΠϒϥ͕ग़དྷΔ

  19. ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ ࣮ߦ

  20. Swift Package ManagerΛ࢖ͬͯΈΑ͏

  21. • ͦͷ໊ͷ௨ΓɺSwift༻Package Manager(Appleެࣜ) • ௨শSPM • CocoaPods΍CarthageͱಉϨΠϠʔ • ϓϩδΣΫτ഑ԼͷPackage.swiftʹઃఆ΍ґଘΛॻ͖ ࠐΉ

    • ݱ࣌఺Ͱ͸ɺiOS΍MacͳͲͷUIΞϓϦ͸ະαϙʔτ Swift Package Managerͱ͸
  22. هड़ྫ

  23. …………… …………… …………… Ϗϧυ

  24. …………… …………… …………… ґଘϥΠϒϥϦͷclone Ϗϧυ

  25. …………… …………… …………… ίϯύΠϧ & ϦϯΫ Ϗϧυ

  26. XcodeͰ։ൃͯ͠ΈΑ͏

  27. SPMͰ࡞ͬͨϓϩδΣΫτ͸ɺ XcodeͰ։ൃ͢Δ͜ͱ΋ग़དྷ·͢ɻ

  28. XcodeͰ։ൃͯ͠ΈΑ͏ Package.swift͔ΒઃఆΛಡΈࠐΈɺ.xcodeprojΛ࡞੒͢Δ

  29. XcodeͰ։ൃͯ͠ΈΑ͏ ίϚϯυҰൃͰxcodeproj͕࡞੒͞ΕΔ

  30. XcodeͰ։ൃͯ͠ΈΑ͏ xcodeprojΛ։͖·͢

  31. Xcode্Ͱ࣮ߦ

  32. XCTest΋΋ͪΖΜ࢖͑·͢ʂ

  33. SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯ Λ࡞ΔϙΠϯτ

  34. SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ڧྗͳܕ҆શੑΛ׆͔͢

  35. σʔλϕʔε΍APIϨεϙϯεͷIO݁Ռ͸Anyܕ͕΄ͱΜͲ JSON MySQL

  36. σʔλϕʔε΍APIϨεϙϯεͷIO݁Ռ͸Anyܕ͕΄ͱΜͲ JSON MySQL AnyܕΛͦͷ··ѻ͏ͱ…

  37. σʔλϕʔε΍APIϨεϙϯεͷIO݁Ռ͸Anyܕ͕΄ͱΜͲ JSON MySQL unwrapɺόϦσʔγϣϯΛ౎౓ॻ͘͸Ίʹ…

  38. Server Side͸ؔΘΔਓ਺͕ଟ͍ͨΊɺ AnyܕΛ;ΜͩΜʹ࢖ͬͨίʔυ͸ϝϯς͕૬౰େม

  39. DBͷऔಘ݁ՌͳͲ͸͖ͪΜͱܕΛఆٛ͠·͠ΐ͏

  40. Enum ڧྗͳܕ҆શੑΛ׆͔͢ EnumʹΑΔύλʔϯϚονϯά͸ɺServer SideͰ΋༗ޮ HTTP Method HTTP Status

  41. Enum ڧྗͳܕ҆શੑΛ׆͔͢ ErrorͷύλʔϯϚονϯάͰɺΤϥʔϨεϙϯεΛॊೈʹ

  42. ܦݧ্ɺwebαʔϏεΛӡ༻͍ͯͯ͠ɺ αʔόʔ͕མͪΔେ͖ͳݪҼͷҰ͕ͭNullࢀর(ψϧϙ)Ͱ͢ɻ Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢

  43. Swiftʹ͸Optionalܕ͕͋ΓɺϓϩάϥϚ͸ͦͷ஋͕nullΛڐ༰ ͢Δ͔Ͳ͏͔Λࢹ֮తʹ൑அͰ͖·ͨ͠ ʢίϯύΠϥ͕ڭ͑ͯ͘ΕΔʣ Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢

  44. Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢ JavaScriptͰΫϥογϡ͢Δ͔΋͠Εͳ͍ྫ

  45. Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢ SwiftͰ҆શʹIO݁ՌΛѻ͏

  46. SwiftΛαʔόʔͰ࢖͏ϝϦοτͯ͠ɺ ͜ͷnullͷڐ༰ੑ͸ͱͯ΋େ͖ͳཁૉͰ͢ɻ

  47. SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ΫϥΠΞϯτͰϝδϟʔͳϥΠϒϥϦΛੵۃతʹ࢖͏

  48. • Swiftʹ͸iOSͳͲͰங͔ΕͨɺطଘͷΤίγες Ϝ͕͋Δ • UIΛ࣋ͨͳ͍௨৴ॲཧ΍σʔλγϦΞϥΠζʹ ಛԽͨ͠΋ͷ͸ߴ֬཰ͰServer SideͰ΋࢖͑Δ • SPMʹରԠ͍ͯ͠ͳ͍৔߹΋ɺগ͠ͷมߋͰ Server

    SideͰ࢖͑Δέʔε͕͋Δ SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ΫϥΠΞϯτͰϝδϟʔͳϥΠϒϥϦΛੵۃతʹ࢖͏
  49. • Himotoki • SwiftyJSON • swift-protobuf • RXSwift • JSONWebToken.swift

    αʔόʔαΠυͰ΋࢖͑ΔiOSͷϥΠϒϥϦʢ΄ΜͷҰ෦ʣ
  50. ंྠͷ࠶ൃ໌Λ߇͑ͯɺΤίγεςϜΛੵۃతʹ׆༻͠·͠ΐ͏

  51. SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ UbuntuͰಈ͘͜ͱΛ࠷༏ઌʹ

  52. • GoͷΑ͏ͳϙʔλϏϦςΟ͸ະͩSwiftʹ͸ͳ͍ • SwiftެࣜαϙʔτͷLinux͸Ubuntu • MacͰ͸࢖͑ͯ΋UbuntuͰ࢖͑ͳ͍API͸݁ߏ͋Δ • ςετίʔυΛඞͣUbuntuͰ΋૸Βͤ·͠ΐ͏ SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ UbuntuͰಈ͘͜ͱΛ࠷༏ઌʹ

  53. Linux൛SwiftͰϋϚͬͨ͜ͱᶃ CF**ͰDNSղܾΛࢼΈ͕ͨ…..

  54. CF**ͰDNSղܾΛࢼΈ͕ͨ….. Linux൛SwiftͰϋϚͬͨ͜ͱᶃ

  55. Linux൛Swiftʹ͸CF**Ͱ࢖͑ͳ͍΋ͷ͕͋Δ Linux൛SwiftͰϋϚͬͨ͜ͱᶃ

  56. Main thread͔Ͳ͏͔൑ఆ͠Α͏ͱͯ͠… Linux൛SwiftͰϋϚͬͨ͜ͱᶄ

  57. Main thread͔Ͳ͏͔൑ఆ͠Α͏ͱͯ͠… Linux൛SwiftͰϋϚͬͨ͜ͱᶄ

  58. Linux൛SwiftͰϋϚͬͨ͜ͱᶄ

  59. ࣮૷͞Εͯͳ͍wwwww

  60. ͜ͷΑ͏ʹɺະͩLinux൛SwiftͰ͸τϥοϓ͕ͪΒ΄Β

  61. MacͰಈ͍͔ͨΒͬͯ҆৺͠ͳ͍Α͏ʹʂ

  62. SwiftͰݎ࿚Ͱߴ଎ͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ GCDΛ׆༻ͯ͠ɺαʔόʔϦιʔεΛޮ཰తʹѻ͏

  63. GCDͱ͸ɺThread΍ඇಉظ໋ྩΛ؆୯ʹѻ͏ͨΊͷϥΠϒϥϦ GCDΛ͏·͘ѻ͏͜ͱ͕ɺSwiftͷServer SideΛ߈ུ͢Δ伴

  64. Dispatch Queue Serial Task Task Task Task Task queue.async {}

    Concurrent Task Task Task Task Task Task Task Task queue.async {} executing waiting balancing thread 1 thread 1 thread 2
  65. Dispatch Source Dispatch Source Socket ؂ࢹ Event Event Event ιέοτ΍ϑΝΠϧͷهड़ࢠΛ؂ࢹͯ͠ɺ

    ॻ͖ࠐΈ͕͋ͬͨλΠϛϯάͰΠϕϯτΛൃՐ select΍kqueueͱ͍ͬͨඇಉظܥͷsystem callΛந৅Խ͍ͯ͠Δ
  66. GCDΛۦ࢖ͯ͠ɺߴ଎ͳαʔόʔΛ࡞Γ·͠ΐ͏

  67. SwiftͰ࣮ફతͳWebΞϓϦέʔγϣϯ Λ࡞ͬͯΈΑ͏

  68. Ϣʔβʔ A Ϣʔβʔ B OAuth OAuth Callback Callback ͜Μͳײ͡ͷwebΞϓϦέʔγϣϯΛ࡞ͬͯΈ·ͨ͠ ChatϧʔϜ

    ᶃ ᶃ ᶄ ᶄ ᶅ ᶅ
  69. • GithubͷOAuthΛ࢖ͬͨϩάΠϯػೳ • JWTʹΑΔϢʔβʔೝূ(ηογϣϯ୅ସ) • σʔλ͸શͯMySQLʹอଘ͢Δ • ίʔυଆͰεΩʔϚ؅ཧ • View͸SPAͰఏڙ

    ͬ͘͟Γͱͨ͠ཁ݅
  70. https://github.com/noppoMan/SwiftJNChatApp

  71. σϞ

  72. https://www.youtube.com/watch?v=WI7BQQHVV_Y&feature=youtu.be ಈըURL͸ͪ͜Β

  73. • noppoMan/Prorosum …… HTTP Server • noppoMan/WebAppKit …… Routing, Middleware

    • noppoMan/SwiftKnex …… Mysql Client, Schema Migrator • apple/swift-protobuf …… Entities and JSON serializer • kylef/JSONWebToken.swift …… JWT encode/decode ࠓճ࢖ͬͨϥΠϒϥϦ
  74. noppoMan/Prorsum

  75. noppoMan/Prorosum • HTTP ServerͳͲͷSwiftඪ४Ͱఏڙ͞Ε͍ͯͳ͍ ωοτϫʔΫػೳΛิ׬͢Δ • LinuxͰಈ͘͜ͱΛલఏʹ։ൃ͞Ε͍ͯΔ • GoݴޠͷgoroutineͱChannelΛΠϯεύΠΞ͠ ͨɺඇಉظ/ಉظॲཧϞσϧ

  76. ProrsumͷαʔόʔΞʔΩςΫνϟ DispatchSource + DispatchQueueΛ࢖ͬͨ Πϕϯτۦಈ + WorkerThreadͷϋΠϒϦοτϞσϧ main thread DispatchSourceͰɺ

    listeningSocketΛ؂ࢹ Queue Queue Queue Queue Queue Queue Queue Queue Queue client DispatchQueue(concurrent) nonblocking I/O blocking I/O
  77. • Ͳ͏ͯ͠΋ɺCallbackϕʔεͷϑϩʔ੍ޚʹͳͬ ͯ͠·͏ • ConcurrentͰ࣮ߦ͢Δ৔߹ɺεϨουηʔϑΛҙ ࣝͯ͠ϓϩάϥϛϯάΛ͢Δඞཁ͕͋Δ DispatchQueueͷ໰୊఺

  78. thread Prorsumͷඇಉظ/ಉظॲཧϞσϧ Task1 Task2 Task3 send send send main thread

    receive Channel thread thread ProrsumͰ͸ݪଇͱͯ͠callbackʹΑΔϑϩʔ੍ޚ͸ߦΘͳ͍ɻ Prorosumͷఏڙ͢Δgoؔ਺ͱchannelʹΑΓɺฒྻॲཧͱͦͷಉظΛऔΔ Async Async Async Sync(Blocking)
  79. go()ͱChannelΛ࢖࢛ͬͨଇԋࢉͷྫ

  80. thread લճͷਤʹ౰ͯ͸ΊΔͱ Task1 Task2 Task3 send(1) send(2) send(3) main thread

    Channel thread thread go() go() go() receive(1) receive(2) receive(3)
  81. ProrsumͷύϑΥʔϚϯε Request/sec 0 17500 35000 52500 70000 Prorsum Kitura Go

    1.7 HTTP Server Express 14,769 64,768 17,144 29,436 ɾwrk -d 30s -t 4 -c 20 ɾResponded with the 10 length of random JSON array ɾMachine: MacOS Sierra, 8 logical cores, 8GB RAM
  82. Routing΍Middleware • noppoMan/WebAppKitΛ࢖͍ͬͯΔ • WebAppKit͸ϓϩʔαϜ޲͚ͷRoutingɺ MiddlewareΛఏڙ͢ΔϑϨʔϜϫʔΫ • Express΍SinatraͷΑ͏ͳ΋ͷ • ೝূͳͲ͸͜ͷMiddlewareͰॲཧ͍ͯ͠Δ

  83. APIͱͷ௨৴ (NS)URLSessionΛ࢖ͬͯAPIͱ௨৴͍ͯ͠Δɻ ͨͩ͠ɺίʔϧόοΫϕʔεͷϑϩʔ੍ޚʹͨ͘͠ͳ͍ͷͰɺdataTaskResumtSync ͱ͍͏ϝιουΛ௥Ճͯ͠ɺpthread_cond_waitͰϒϩοΫ͢ΔΑ͏ʹ͍ͯ͠Δ Routes/GithubRoute.swift

  84. ࣮͸APIKitΛ࢖͍͔ͨͬͨͷͰ͕͢ɺɺ

  85. objc_**͕LinuxͰ࢖͑ͳ͍…

  86. ͥͻLinuxରԠ͍ͨ͠Ͱ͢Ͷʂ

  87. • SwiftyJSONͳͲͷJSONϥΠϒϥϦ͸࢖͍ͬͯͳ͍ • EntityΛswift-protobufΛ࢖ͬͯఆ͍ٛͯ͠ΔͨΊɺͦ Ε͕ఏڙ͢ΔJSONγϦΞϥΠζػೳΛ࢖͍ͬͯΔ JSONͷऔѻ

  88. Swift͔ΒMySQLΛѻ͏ ͓ख੡ͷSwiftKnexͱ͍͏ϥΠϒϥϦΛ࢖͍ͬͯΔ

  89. • Mysql QueryBuilder • Schema Migration • Pure SwiftͰMySQLϓϩτίϧΛ࣮૷͍ͯ͠Δ noppoMan/SwiftKnex

  90. None
  91. None
  92. None
  93. ܕ҆શʹselect݁ՌΛѻ͏

  94. ίʔυଆͰεΩʔϚ؅ཧ SwiftKnexʹ͸ɺεΩʔϚ؅ཧ/ϚΠάϨʔγϣϯػೳ͕උΘ͍ͬͯΔ

  95. Migration ClassΛ࡞੒͢Δ $ ./.build/debug/SwiftKnexMigration create ResourceName ΛίϚϯυϥΠϯ͔Β࣮ߦ͢Δ͜ͱͰɺMigration ClassΛ࡞੒

  96. Migration/20170218035306_CreateUser.swift Migration Classͷఆٛ

  97. Migrationͷ࣮ߦ/ϩʔϧόοΫ

  98. Ϋϥ΢υ΁ͷσϓϩΠͳͲɺ ࿩͍ͨ͜͠ͱ͸ଞʹ΋୔ࢁ͋ΔͷͰɺͥͻ࠙਌ձͰʂ

  99. SwiftͰͷαʔόʔαΠυ։ൃ͸ເ෺ޠͰ͸ͳ͘ɺ ͢ͰʹϦΞϧͳ΋ͷͰ͋Δͱ͍͏͜ͱ͕఻ΘΕ͹خ͍͠Ͱ͢ʂ

  100. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠