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

Practical Hexaville(ja)

Practical Hexaville(ja)

Tokyo Server Side Swift Meetup #8

9ebab3d4f66a62a859ad238e7e97597f?s=128

Yuki Takei

July 05, 2017
Tweet

Transcript

  1. ࣮ફHexaville Yuki Takei@Tokyo Server Side Swift Meetup #8 27 Jun,

    2017 ʙ 5෼Ͱ࡞ΔαʔόʔϨεͳSwift੡SNSೝূαʔόʔ ʙ
  2. ࣗݾ঺հ

  3. • Hexavilleͷ঺հ • 5෼Ͱ࡞ΔSwift੡SNSೝূαʔόʔ • HexavilleͰwebαʔϏεΛ࡞Δ্Ͱͷϊ΢ϋ΢ • CIɺσϓϩΠͷࣗಈԽ • ࠓޙͷϩʔυϚοϓ

    ΞδΣϯμ
  4. Hexavilleͷ঺հ

  5. None
  6. ࠓྲྀߦΓͷαʔόʔϨε αʔόʔϨείϯϐϡʔςΟϯάͱ͸ɺΫϥ΢ υϓϩόΠμʔ͕ΞϓϦέʔγϣϯʹඞཁͳϚ γϯϦιʔεͷׂΓ౰ͯΛಈతʹ؅ཧ͠ɺࣄલ ʹߪೖ͞Εͨ͜ͱʹجͮ͘੥ٻͰ͸ͳ͘ɺ֤Ξ ϓϦέʔγϣϯཁٻΛຬͨͨ͢Ίʹ࢖༻͞ΕΔ ݸʑͷϦιʔεྔʹج͍ͮͨ੥ٻΛߦ͏Α͏ͳ Ϋϥ΢υίϯϐϡʔςΟϯάϞσϧɻ wikipedia αʔόϨεɾίϯϐϡʔςΟϯά

    ΑΓ
  7. Hexavilleͱ͸ • Swift੡ͷαʔόʔϨε؀ڥΛߏங͢ΔϑϨʔϜϫʔΫͱͦͷ πʔϧ܈ (ݱࡏ͸AWS Lambda + API GatewayͷΈ) •

    ίʔυϕʔε͕SwiftͷͨΊɺiOSΞϓϦέʔγϣϯΛॻ͘Ԇ௕ ͰαʔόʔϓϩάϥϜΛهड़Ͱ͖Δɻ • ී௨ͷwebΞϓϦέʔγϣϯΛ࡞ͬͯσϓϩΠ͢Δ͚ͩ • ίϚϯυҰͭͰΫϥ΢υ্ʹσϓϩΠ͞Εɺαʔόʔӡ༻ͳ ͠ͰαʔϏεΛ։࢝Ͱ͖Δ • XcodeͰͷ։ൃ؀ڥΛఏڙɺInstrumentsɺLLDBΛ࢖ͬͨσ όοά΋Մೳ
  8. Πϯετʔϧ

  9. 1ίϚϯυͰΠϯετʔϧՄೳ

  10. HexavilleͷϓϩδΣΫτΛ࡞੒͢Δ

  11. HexavilleͷϓϩδΣΫτΛ࡞੒͢Δ

  12. ઃఆϑΝΠϧ(Hexavillefile.yml)

  13. HexavilleΛ࢖ͬͨWebΞϓϦέʔγϣϯͷهड़ํ๏

  14. HexavilleΛ࢖ͬͨWebΞϓϦέʔγϣϯͷهड़ํ๏ ϑϨʔϜϫʔΫͷΠϯελϯεΛ࡞੒

  15. HexavilleΛ࢖ͬͨWebΞϓϦέʔγϣϯͷهड़ํ๏ Middlewareͷొ࿥ʢඞਢͰ͸ͳ͍ʣ

  16. HexavilleΛ࢖ͬͨWebΞϓϦέʔγϣϯͷهड़ํ๏ RoutingͱͦͷURIͷϓϩάϥϜͷ࣮૷

  17. HexavilleΛ࢖ͬͨWebΞϓϦέʔγϣϯͷهड़ํ๏ ΞϓϦέʔγϣϯͷىಈ

  18. $ hexaville deploy Hello σϓϩΠ

  19. $ hexaville deploy Hello Start to build swift... Building application....

    Generating Routing Manifest file.... Sending build context to Docker daemon 176 MB Step 1/11 : FROM ubuntu:14.04 ---> 132b7427a3b4 Step 2/11 : RUN apt-get update -y ---> Using cache ---> fd9a040329d6 Step 3/11 : RUN apt-get install -y clang libicu-dev libbsd-dev uuid-dev git libxml2-dev libxslt1-dev python-dev libcurl4-openssl-dev wget ---> Using cache ---> c4890f13de61 Step 4/11 : ENV SWIFT_DOWNLOAD_URL https://swift.org/builds/swift-3.1-release/ ubuntu1404/swift-3.1-RELEASE/swift-3.1-RELEASE-ubuntu14.04.tar.gz ---> Using cache ---> a5ac353284e3 Step 5/11 : ENV SWIFTFILE swift-3.1-RELEASE-ubuntu14.04 ---> Using cache Ϗϧυ։࢝ σϓϩΠ
  20. σϓϩΠͷ׬ྃ Deploying to staging ###################################################### Information ApplicationName: AuthService Endpoint: https://bma4gjwu36.execute-api.ap-northeast-1.amazonaws.com/staging

    Stage: staging ###################################################### All Done.
  21. Routingͷ֬ೝ $ hexaville routes

  22. Routingͷ֬ೝ $ hexaville routes Endpoint: https://bma4gjwu36.execute-api.ap-northeast-1.amazonaws.com/staging Routes: GET /auth/twitter/callback GET

    /auth/twitter GET / GET /auth/facebook GET /auth/facebook/callback
  23. XcodeΛ࢖ͬͨɺMac্Ͱͷ։ൃͱDebugαʔόʔͷىಈ https://youtu.be/Lj7Jg_XlmIU σϞ

  24. None
  25. 5෼Ͱ࡞ΔSwift੡SNSೝূαʔόʔ

  26. webαʔϏεΛ࡞Δ্ͰɺSNSΞΧ΢ϯτΛ࢖ͬͨ ϩάΠϯػೳ͸ࠓ΍ແͯ͘͸ͳΒͳ͍΋ͷʹ…

  27. ͨͩɺຖ౓ຖ౓࡞Δͷ͸໘౗

  28. ֤αʔϏε͕SNSೝূػೳΛ͍࣋ͬͯΔέʔε Service 1 Service 2 DB DB

  29. Service3, Service4ͱ૿͍͑ͯͬͨΒͲ͏ͳͬͪΌ͏ʁ

  30. ͦΜͳͱ͖͸ɺαʔόʔϨεͷग़൪

  31. HexavilleΛ࢖ͬͯೝূαʔϏεΛαʔόʔϨεԽ DB Hexaville Auth Service Service 2 DB Service 1

  32. DB Hexaville Auth Service Service 2 DB Service 1 ֤αʔϏε͕ೝূϓϩάϥϜΛ࣋ͨͳ͍͍ͯ͘

    HexavilleΛ࢖ͬͯೝূαʔϏεΛαʔόʔϨεԽ
  33. DB Hexaville Auth Service Service 2 DB Service 1 ೝূαʔϏε͕Ұͭʹ·ͱ·Δ্ɺӡ༻ෆཁʹʂ

    HexavilleΛ࢖ͬͯೝূαʔϏεΛαʔόʔϨεԽ
  34. ೝূϓϩάϥϜΛ֤αʔϏεʹ࣮૷͢Δ࣌୅͸ɺ ΋͏ऴΘΓͭͭ͋Δ

  35. Ͱ͸ૣ଎ɺ5෼Ͱαʔόʔӡ༻͕ෆཁͳೝূαʔϏεΛ ࡞ͬͯΈ·͠ΐ͏

  36. None
  37. • Hexavilleʹ༷ʑͳೝূػೳΛఏڙ͢ΔϓϥάΠϯ • OAuth1, OAuth2ΫϥΠΞϯτΛఏڙ • FacebookɺGithubɺGoogle, TwitterͷOAuthΛσϑΥϧτͰ ఏڙ •

    HexavilleͷRouterͱͯ͠ಈ࡞͢Δ • ηογϣϯΛར༻Մೳ(Memory, Redis, Dynamodb(࣮૷த)) • ಠࣗͷOAuthΫϥΠΞϯτΛϓϩτίϧʹԊͬͯ؆୯ʹ࡞੒ Մೳ HexavilleAuth
  38. Facebook(OAuth2)ͷྫ

  39. HexavilleAuthͷΠϯελϯε࡞੒ Facebook(OAuth2)ͷྫ

  40. FacebookAuthorizationProviderͷΠϯελϯε࡞੒ Facebook(OAuth2)ͷྫ

  41. PATHɿೝূURL(͜͜ʹདྷͨΒfacebookʹϦμΠϨΫτ) Facebook(OAuth2)ͷྫ

  42. consumerKey, SecretɿFBΞϓϦ༻ͷΫϨσϯγϟϧ Facebook(OAuth2)ͷྫ

  43. callbackURL: OAuth੒ޭޙͷભҠઌ Facebook(OAuth2)ͷྫ

  44. scope: OAuth2ͷείʔϓ Facebook(OAuth2)ͷྫ

  45. OAuth2ͷೝՄ͕௨ͬͨ৔߹ͷcallbackɻ credential(access-token)ɺೝՄuserͳͲͷ৘ใ ͕औಘͰ͖Δ Facebook(OAuth2)ͷྫ

  46. HexavilleAuthͷΠϯελϯεʹ facebookProviderΛ௥Ճ Facebook(OAuth2)ͷྫ

  47. Facebook(OAuth2)ͷྫ HexavilleAuthͷΠϯελϯεΛrouting͢Δ

  48. ͋ͱ͸hexaville deployίϚϯυͰɺ σϓϩΠ͢Δ͚ͩʂ

  49. https://youtu.be/Uv-vma234UQ σϞ

  50. 5෼ͰೝূαʔϏε͕࡞Εͯ͠·͍·ͨ͠ ͔͠΋SwiftͰʂ

  51. https://github.com/Hexaville/HexavilleAuth/blob/master/ Sources/HexavilleAuthExample/main.swift ͦͷଞɺTwitter΍GoogleͳͲͷαϯϓϧ͸ͪ͜Β͔Β

  52. • device tokenͳͲͷొ࿥αʔόʔ • CRUDͷAPI΍JSON-RPCαʔόʔ • HTML഑৴αʔόʔ • ը૾ϦαΠζαʔόʔ(SwiftͱCϥΠϒϥϦͱͷ ਌࿨ੑͷߴ͞΋˓)

    ͦͷଞʹHexaville͕޲͍͍ͯΔαʔϏε
  53. αʔόʔϨε͸ར఺Λ্͛Ε͹ΩϦ͕ͳ͍Ͱ͕͢ɺ൓໘ɺ ໰୊఺΋͍͔ͭ͘ଘࡏ͠·͢ɻ

  54. HexavilleͰαʔόʔϨεΞϓϦέʔγϣϯΛ࡞Δࡍʹɺ ؾΛ͚ͭΔ఺΍ͦͷଞͷϊ΢ϋ΢Λ͝঺հ͠·͢ɻ

  55. HexavilleͰwebαʔϏεΛ࡞Δ্Ͱͷϊ΢ϋ΢

  56. Server ैདྷͷwebΞϓϦέʔγϣϯͷ࣮ߦϞσϧ DB connection pool

  57. Lambda Function Lambda Function Lambda Function Lambda Function Lambda Function

    αʔόʔϨε ίϯϐϡʔςΟϯάͷ࣮ߦϞσϧ
  58. DB Lambda Function Lambda Function Lambda Function Lambda Function Lambda

    Function αʔόʔϨε ίϯϐϡʔςΟϯάͷ࣮ߦϞσϧ(DB઀ଓ༗Γ)
  59. αʔόʔϨε ίϯϐϡʔςΟϯάͷ࣮ߦϞσϧ(DB઀ଓ༗Γ) DB Lambda Function Lambda Function Lambda Function Lambda

    Function Lambda Function ΫϥΠΞϯτͷίωΫγϣϯ਺ || DBͷίωΫγϣϯ઀ଓ਺
  60. αʔόʔϨε ίϯϐϡʔςΟϯάͷ࣮ߦϞσϧ(DB઀ଓ༗Γ) DB Lambda Function Lambda Function Lambda Function Lambda

    Function Lambda Function DB͸1connection = 1threadͰ ड͚෇͚Δ࣮૷͕ଟ͍
  61. αʔόʔϨε ίϯϐϡʔςΟϯάͷ࣮ߦϞσϧ(DB઀ଓ༗Γ) DB Lambda Function Lambda Function Lambda Function Lambda

    Function Lambda Function ॏ͍ΫΤϦΛ౤͍͛ͯͳͯ͘΋ɺ େͨ͠਺ͷΫΤϦΛ౤͍͛ͯͳͯ͘΋ɺ ͋ͳͨͷDB͸൵໐Λ্͛Δ͜ͱʹͳΓ·͢ɻ
  62. αʔόʔϨε ίϯϐϡʔςΟϯάͷ࣮ߦϞσϧ(DB઀ଓ༗Γ) DB Lambda Function Lambda Function Lambda Function Lambda

    Function Lambda Function RedisͳͲͷϊϯϒϩοΩϯάͳNoSQLσʔλ ϕʔε΋ྫ֎ʹ΋ΕͣɺίωΫγϣϯ਺΍ಉ࣮࣌ ߦ਺͕૿͑Ε͹ͦͷ෼DB΁ͷෛՙ΋্͕Δɻ ·ͨɺ઀ଓՄೳͳΫϥΠΞϯτ਺͸Ϛγϯεϖο Ϋʹґଘ͢ΔͨΊɺແବʹNode͕૿͑ΔՄೳੑ ΋ɻ
  63. • ͦ΋ͦ΋ͦͷσʔλ͸ͦͷDBʹอଘ͢Δඞཁ͸͋Δͷ͔ʁ • DynamodbΛར༻͢Δ(ΩϟύγςΟʔϢχοτ͕Φʔτε έʔϧରԠ͠ɺίωΫγϣϯ਺Λجຊతʹؾʹ͢Δඞཁ͕ ແ͘ͳͬͨ) • DBίωΫγϣϯϓʔϧ༻ͷαʔόʔΫϥελʔΛ࡞Γɺ lambda͔Β͸ͦͷΫϥελʔʹ઀ଓ͠ɺΫϥελʔ಺ͷΠ ϯελϯε͕DB΁ͷ௨৴Λ୅ߦ͢Δɻ

    • SQSΛ࢖ͬͨδϣϒΩϡʔ+ϫʔΧʔύλʔϯͰඇಉظʹ DBΠϯαʔτ(SQSͷฒྻ࣮ޮੑೳະݕূ) ΫϥΠΞϯτͷίωΫγϣϯ਺ = DB઀ଓ਺ͷղܾҊ
  64. ίʔϧυελʔτ໰୊ ॳճىಈ࣌΍ɺ͠͹Β͘ΞΫηε͕ແ͍৔߹ɺlambda͸ίʔ ϧυঢ়ଶʹҠߦ͢Δɻ ࣍ͷϦΫΤετͰίʔϧυঢ়ଶ͔Β෮ؼ͢Δ·ͰͷΞΠυϧ࣌ ͕ؒ͜ͷίʔϧυελʔτ໰୊Ͱ͋Δɻ(ίϯςφ΍ωοτ ϫʔΫΠϯλʔϑΣʔεͷηοτΞοϓʹ͕͔͔࣌ؒΔ) ࠷େͷ໰୊͸ɺϦΫΤετ͕λΠϜΞ΢τ͢Δ͔΋ͱ͍͏఺ (10ඵ)

  65. ίʔϧυελʔτ໰୊ͷղܾҊ • ͦ΋ͦ΋ਓʹΑͬͯ͸໰୊ʹͳΒͳ͍έʔε΋ • τϥϑΟοΫ͕ৗ࣌͋Δ৔߹͸΄ͱΜͲൃੜ͠ ͳ͍ͱͷ͜ͱʢAWSެࣜεϥΠυΑΓʣ • lambdaͷεέδϡʔϦϯάػೳ(cronʹ૬౰)Λ ࢖ͬͯɺఆظతʹΤϯυϙΠϯτʹϦΫΤετ Λ౤͛Δ͜ͱͰɺ΢ΥʔϜঢ়ଶΛอͭ

  66. • Hexaville͸όΠφϦίϯςϯπͷϨεϙϯε͕Մೳɻ= SwiftProtobufͱNSURLSessionͰ؆୯ɺ҆શʹ௨৴ग़དྷΔ • SPM੡ͷϥΠϒϥϦ͕ͦͷ··࢖͑Δ • ΫϥΠΞϯτϥΠϒϥϦ΋ͦͷ··࢖͑Δέʔε΋ (SwiftyJSONɺSwiftProtoBuf etc..) •

    VPC΍ΧελϜυϝΠϯ͕࢖͑Δ • Session͕࢖͑Δ • Linux͔Β΋deployͰ͖Δ ͦͷଞHexavilleͰग़དྷΔ͜ͱʢൈਮʣ
  67. • Hexaville͸όΠφϦίϯςϯπͷϨεϙϯε͕Մೳɻ= SwiftProtobufͱNSURLSessionͰ؆୯ɺ҆શʹ௨৴ग़དྷΔ • SPM੡ͷϥΠϒϥϦ͕ͦͷ··࢖͑Δ • ΫϥΠΞϯτϥΠϒϥϦ΋ͦͷ··࢖͑Δέʔε΋ (SwiftyJSONɺSwiftProtoBuf etc..) •

    VPC΍ΧελϜυϝΠϯ͕࢖͑Δ • Session͕࢖͑Δ • Linux͔Β΋deployͰ͖Δ ͦͷଞϊ΢ϋ΢
  68. ͱ͍͏͜ͱ͸…!

  69. CIɺσϓϩΠͷࣗಈԽ

  70. ݱࡏCI؀ڥ͸ܶతʹ޲্͠ɺ ແྉαʔϏε͕୆಄͠·͍ͬͯ͘·͢Ͷɻ

  71. Hexaville͸ϝδϟʔͳCIαʔϏεͰಈ࡞͢Δ

  72. Hexaville͸Linux͔Β΋σϓϩΠՄೳͳͷͰɺ ແྉͰCI؀ڥΛ࡞Δ͜ͱ͕ग़དྷ·͢ɻ

  73. Travis͔ΒࣗಈσϓϩΠ͢Δྫ

  74. Hexaville͸ϩʔΧϧͰ΋αʔόʔΛىಈͰ͖ΔͷͰɺ APIͷςετίʔυ΋؆୯ʹॻ͚·͢

  75. αʔόʔϨεͰ΋؆୯ʹCIΛಋೖՄೳʂ

  76. ࠓޙͷϩʔυϚοϓ

  77. • Dynamodb΁ͷ؆қΞΫηεAPI • MysqlɺRedisͷίωΫγϣϯϓʔϦϯάαʔόʔΛECSαʔϏεͱ ͯ͠ఏڙ • ϩάू໿ϓϥάΠϯͷ։ൃ • Dockerίϯςφ಺Ͱςετίʔυ࣮ߦͷαϙʔτ(CI؀ڥʹSwiftࣗ ମͷΠϯετʔϧΛ͠ͳͯ͘ࡁΉΑ͏ʹ)

    • grpcͷΑ͏ͳܗͰΫϥΠΞϯτ <-> αʔόʔؒͷαʔϏεͷΠλʔ ϑΣʔεΛఆٛͰ͖ΔΑ͏ʹʢTLS+http1.1ʣ • SwiftެࣜHTTP APIͱͷ౷߹ ࠓޙͷϩʔυϚοϓ
  78. HexavilleΛ࢖ͬͯɺ Server SideͰ΋ΨϯΨϯSwiftॻ͖·͠ΐ͏ʂ

  79. ڵຯͷ͋Δํ͸ɺ͓ؾܰʹ։ൃʹ͝ࢀՃԼ͍͞ʂ

  80. ͓஌Βͤ

  81. None
  82. None
  83. builderscon 2017Ͱ࿩͠·͢ʂ 8݄5೔ PM3:50ʙ

  84. ઌఔͷϩʔυϚοϓΛؚΊ࣮ͨફతͳ Hexaville։ൃ/ӡ༻ͷ࿩ͷ΄͔ɺHTTP APIͱͷ౷߹΍ Swiftαʔόʔ։ൃͷ࠷લઢʹ͍ͭͯൃද͠·͢ɻ

  85. None
  86. νέοτͷൢച͸ऴྃͯ͠͠·ͬͨ໛༷Ͱ͕͢ɺ ௥Ճൢച΋༧ఆ͍ͯ͠ΔʁΈ͍ͨͳͷͰνέοτ ແ͍͚Ͳ౰೔ࢀՃ͍ͨ͠ͱ͍͏ํ͸ɺ #buidersconΛ͚ͭͯTweetͯ͠ΈͯԼ͍͞ʂ

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