Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Server Side Swift の個人的な利用シーン

Yuki Takei
February 07, 2018

Server Side Swift の個人的な利用シーン

Vapor Meetup 1st in Tokyo 2018/02/06

Yuki Takei

February 07, 2018
Tweet

More Decks by Yuki Takei

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ ໊લ: ෢Ҫ༏ݾ (noppoMan) ॴଐ: ϑϦʔϥϯε, iDEAKITT Twitter: @noppoMan722 GitHub

    noppoMan ओͳ෼໺͸ɺΠϯϑϥɺαʔόʔαΠυ։ൃશൠɺσʔλղੳͰ͢ɻ iOSΞϓϦέʔγϣϯ։ൃ΋΍Γ·͢ɻ ࠓɺFGOʹϋϚͬͯ·͢ʂww
  2. ࣌୅͸Ϋϥ΢υɺBFF (Backend for Frontend) • Ϋϥ΢υͷ୆಄ʹΑΓɺόοΫΤϯυͷ࣮૷޻਺͸େ෯ ࡟ݮ • Ϋϥ΢υαʔϏε΁ͷ஌ࣝͷՁ஋͕ඇৗʹߴ·͍ͬͯΔ •

    SPA, ωΠςΟϒ + APIͱ͍͏ߏ੒͕ଟ͘ͳΓɺSEOΛআ ͘SSRͷػձ͸ݮͬͨ • ٕज़͸దࡐదॴʹͳ͖ͬͯͨ(ΤίγεςϜͷൃలతʹ) • ΑͬͯɺϑϧελοΫϑϨʔϜϫʔΫΛඞͣ͠΋બ୒͢Δ ඞཁ͸ͳ͍࣌୅
  3. Service A Service B Service C Auth Server (Swift) ɾEmail

    & Password ɾFacebook ɾTwitter ɾGoogle ͜Μͳ΍ͭ
  4. • Swift͸Liunx্ͰϏϧυ͢Δ͜ͱͰɺELFʹͳΔ • Amazon Linux͸CentOSͷܑఋͳͷͰɺجຊతʹSwift͸ಈ͘ (Foundationͱ͔͸᠘͋Δ͔΋ʁ) • σϓϩΠ͸ϓϩάϥϜΛLinuxͰϏϧυͯ͠ɺELFΛS3ʹ Ξοϓϩʔυ •

    Lambdaؔ਺಺ͷNode.js΍Python͔ΒSwiftͷELFΛαϒϓ ϩηεͱ࣮ͯ͠ߦ • LambdaʹHTTPͰΞΫηεͰ͖ΔΑ͏ɺAPI-GatewayΛར༻ αʔόʔϨεʹSwiftΛ࣮ߦ͢Δʢࠓճ͸Lambdaʣ
  5. And

  6. ࣮ࡍʹӡ༻͍ͯ͠Δߏ੒͕ͪ͜Β Proxy Execute Hexaville as CGI Lambda (Node.js 6.1) API

    Gateway DynamoDB ɾEnable TTL ɾEnable Autoscale Other Services Read/Write Temporary Data HTTP/1.1 OAuth
  7. • ݴޠ΍ඪ४ϥΠϒϥϦͦͷ΋ͷ͸LambdaͰ໰୊ͳ͘ಈ࡞ • Lambda, API-Gateway, DynamoDBશ͕ͯϑϧϚωʔδυ ͳͷͰɺ99%ӡ༻Ϩε • σʔϞϯԽ͞Εͳ͍ͷͰɺGCDͰCPUΛޮ཰Α͘࢖͍੾ Εͳ͍ͱ͔ؾʹ͠ͳ͍͍ͯ͘

    • σϝϦοτͱͯ͠ɺαϒϓϩηεੜ੒ͷίετ͕ߴ͖ͭ͘ɺ ؆୯ͳϓϩάϥϜͳΒNode.js͚ͩͷ΄͏͕଎͍ => ͱ͸͍͑ମײͰ஗͍ͱ͸ײͮ͡Β͍ αʔόʔϨεͰSwiftΛӡ༻ͯ͠Έͯ
  8. • ωΠςΟϒόΠφϦʹͳΔͷͰσϓϩΠ͸༰қ • σʔϞϯԽ͢Δ৔߹ɺVMɺGC͕ແ͍ͨΊ͓͡͞Μ͍Βͣ • ݴ͏·Ͱ΋ͳ͘ಈ࡞͸ߴ଎ • ڧྗͳ੩తܕ෇͚ͱNull҆શੑͷԸܙͰɺ࣮ߦ࣌Τϥʔ͸͔ͳ Γى͜ΓͮΒ͍ •

    ڞ༗ϥΠϒϥϦԽग़དྷΔ • UIKit΍Objective-Cʹґଘ͠ͳ͍iOS༻ͷϥΠϒϥϦ͕ར༻Մೳ • iOSͰ͸ඞਢͷͨΊ৽͍͠ݴޠʹͯ͠͸։ൃऀ͕ଟΊ SwiftΛόοΫΤϯυ(Linux)Ͱར༻͢ΔϝϦοτ
  9. Swiftͷؔ਺γϯϘϧͷऔಘ $ nm -gU .build/x86_64-apple-macosx10.10/debug/libNativeExtensionInSwift.dylib 0000000000001180 T __T022NativeExtensionInSwiftAAV10printHelloyyF 00000000000010f0 T

    __T022NativeExtensionInSwiftAAV9fibonaccis5Int32VAEF 0000000000001230 T __T022NativeExtensionInSwiftAAVABycfC 0000000000001360 T __T022NativeExtensionInSwiftAAVMa 0000000000003af0 S __T022NativeExtensionInSwiftAAVMn 0000000000004310 S __T022NativeExtensionInSwiftAAVN
  10. Swift Node.js C++ ϑϩʔ 3͔Βͷଓ͖ 5ͷؔ਺ϙΠϯλΛNodeͷ಺෦తͳҾ਺ɺ ฦΓ஋ΛฦͤΔΑ͏ʹϥοϓ 6 SwiftͷϓϩάϥϜΛ ಈతɾ੩తϥΠϒϥϦͱͯ͠༧ΊϏϧυ

    4 swift.cc಺Ͱɺ1Ͱ࡞੒ͨ͠ ڞ༗ϥΠϒϥϦΛdl_open 2Ͱऔಘͨ͠γϯϘϧΛdl_symʹ౉͠ɺ 4͔Βؔ਺ϙΠϯλΛऔಘ 5
  11. • dylib΍soͷಡΈࠐΈ͸ॳճͷҰճͷΈͳͷͰɺΦʔόʔ ϔουΛؾʹ͢Δඞཁ͸ͳ͍ • ͜ͷಛੑ͸αʔόʔϨεͰඇৗʹ׆͖Δ => αϒϓϩηεੜ੒ͷίετ͕ແ͘ͳΓSwiftΛར༻͢Δ σϝϦοτ͕ղܾ • Swift͸Cͷܕ΋্ख͘ѻ͑Δ

    => αϯϓϧͰ͸ɺCIntܕΛ࢖͍ͬͯΔ͕ɺSwiftͷ @convention(c)Λར༻͢Ε͹ɺIntܕ΍StringܕΛউखʹ CInt΍CStringͱͯ͠ѻͬͯ͘ΕΔ ωΠςΟϒΤΫεςϯγϣϯ։ൃݴޠͱͯ͠ͷSwift