Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
実践Server Side Swift
Search
Yuki Takei
February 24, 2017
Technology
13
4.1k
実践Server Side Swift
Swift実践入門刊行記念イベント Feb 24 2017
Yuki Takei
February 24, 2017
Tweet
Share
More Decks by Yuki Takei
See All by Yuki Takei
VAR モデルによる OSS プロジェクト同士が生存性に与える 影響の分析
noppoman
0
240
Server Side Swift の個人的な利用シーン
noppoman
2
580
Serverless Server Side Swift
noppoman
2
1.6k
Practical Hexaville(ja)
noppoman
0
460
Server Side Swiftを使った サービス開発とその運用を考える
noppoman
4
1.3k
Swiftに適したサーバーアーキテクチャを再考して実装までしてみる
noppoman
7
2.8k
Swift3 Framework Slimane and Server Side Swift (ja)
noppoman
2
2.2k
About Deploying and Process Management for Slimane
noppoman
2
410
Server Side Framework Slimaneの紹介
noppoman
1
1.1k
Other Decks in Technology
See All in Technology
ガチ勢によるPipeCD運用大全〜滑らかなCI/CDを添えて〜 / ai-pipecd-encyclopedia
cyberagentdevelopers
PRO
3
200
Automated Promptingを目指すその前に / Before we can aim for Automated Prompting
rkaga
0
110
フルカイテン株式会社 採用資料
fullkaiten
0
36k
omakaseしないための.rubocop.yml のつくりかた / How to Build Your .rubocop.yml to Avoid Omakase #kaigionrails
linkers_tech
3
730
AWSコンテナ本出版から3年経った今、もし改めて執筆し直すなら / If I revise our container book
iselegant
15
3.9k
ネット広告に未来はあるか?「3rd Party Cookie廃止とPrivacy Sandboxの効果検証の裏側」 / third-party-cookie-privacy
cyberagentdevelopers
PRO
1
130
カメラを用いた店内計測におけるオプトインの仕組みの実現 / ai-optin-camera
cyberagentdevelopers
PRO
1
120
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
460
生成AIとAWS CDKで実現! 自社ブログレビューの効率化
ymae
2
330
Apple/Google/Amazonの決済システムの違いを踏まえた定期購読課金システムの構築 / abema-billing-system
cyberagentdevelopers
PRO
1
210
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
It's Worth the Effort
3n
183
27k
Making the Leap to Tech Lead
cromwellryan
132
8.9k
YesSQL, Process and Tooling at Scale
rocio
167
14k
Automating Front-end Workflow
addyosmani
1365
200k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Designing for Performance
lara
604
68k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
RailsConf 2023
tenderlove
29
880
Building Adaptive Systems
keathley
38
2.2k
Transcript
࣮ફ Server Side Swift Yuki Takei Swift࣮ફೖץߦه೦Πϕϯτ Feb 24 2017
None
Twitter @noppoman722 Ͱͬͯ·͢
• 201512݄ΑΓɺServer Side SwiftͷϥΠϒϥϦ։ൃί ϛϡχςΟͷߩݙΛελʔτ • 20162݄ʹSlimaneΛϦϦʔε(ݱࡏ։ൃStop) • 20163݄͔ΒTokyo Server
Side Swift Meetup։࢝ • 20164݄͔ΒOpen-SwiftɺZewoίϛϡχςΟʹࢀՃ • 201610݄ΑΓServer Work GroupͷεςʔΫϗϧμʔʹ • 20171݄ΑΓSwiftΛόοΫΤϯυαʔϏεͷݴޠͱͯ͠ɺ αʔϏε։ൃΛελʔτ ͱServer Side SwiftͷาΈ
https://tokyo-ss-swift.connpass.com/
• Server Side Swiftͷ͍ॴ • Server Side Swiftͷ։ൃڥΛ͑Δ • SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ
• ࣮ફతͳWebΞϓϦέʔγϣϯΛ࡞Ζ͏ ΞδΣϯμ
Server Side Swiftͷ͍ॴ
SwiftΛwebαʔϏεͰ͏ϝϦοτ • ੩తݴޠͳΒͰͷ҆શੑɺॊೈͳݴޠ༷ • ࣮ߦɺলΤω • VMɺGC͕ͳ͍ • iOSΞϓϦ։ൃʹΘΕ͍ͯΔϥΠϒϥϦ͕ͦͷ ··͑Δέʔε͕༗Δ
• APIαʔόʔ(Node.jsGoͰ࡞͍ͬͯΔͱ͜Ζ) • όονॲཧͳͲͷίϚϯυϥΠϯΞϓϦέʔγϣϯ • ը૾ॖখɺѹॖɺ҉߸ԽͳͲLLͰ࣌ؒͷ͔͔Δ෦ SwiftͷwebαʔϏεͰͷ͍ॴ
Server Side Swiftͷ։ൃڥΛ͑ͯΈΑ͏
• Xcode8.2ΛࣄલʹΠϯετʔϧ͓͖ͯ͠·͠ΐ͏ ڥߏஙͷલʹ….
Server SideϓϩδΣΫτ ίϚϯυϥΠϯΛͬͯ࡞͍͖ͯ͠·͢
Server Side SwiftϓϩδΣΫτͷ࡞ ϓϩδΣΫτ༻ͷσΟϨΫτϦ࡞
εχϖοτ࡞ίϚϯυ Server Side SwiftϓϩδΣΫτͷ࡞
Server Side Swiftͷ جຊతͳϑΝΠϧߏ Server Side SwiftϓϩδΣΫτͷ࡞
main.swiftͷத
ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ ίϯύΠϧ & ϦϯΫ
ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ .build/debugҎԼʹ࣮ߦՄೳͳϥΠϒϥ͕ग़དྷΔ
ίϚϯυϥΠϯ্ͰͷϏϧυͱ࣮ߦ ࣮ߦ
Swift Package ManagerΛͬͯΈΑ͏
• ͦͷ໊ͷ௨ΓɺSwift༻Package Manager(Appleެࣜ) • ௨শSPM • CocoaPodsCarthageͱಉϨΠϠʔ • ϓϩδΣΫτԼͷPackage.swiftʹઃఆґଘΛॻ͖ ࠐΉ
• ݱ࣌ͰɺiOSMacͳͲͷUIΞϓϦະαϙʔτ Swift Package Managerͱ
هड़ྫ
…………… …………… …………… Ϗϧυ
…………… …………… …………… ґଘϥΠϒϥϦͷclone Ϗϧυ
…………… …………… …………… ίϯύΠϧ & ϦϯΫ Ϗϧυ
XcodeͰ։ൃͯ͠ΈΑ͏
SPMͰ࡞ͬͨϓϩδΣΫτɺ XcodeͰ։ൃ͢Δ͜ͱग़དྷ·͢ɻ
XcodeͰ։ൃͯ͠ΈΑ͏ Package.swift͔ΒઃఆΛಡΈࠐΈɺ.xcodeprojΛ࡞͢Δ
XcodeͰ։ൃͯ͠ΈΑ͏ ίϚϯυҰൃͰxcodeproj͕࡞͞ΕΔ
XcodeͰ։ൃͯ͠ΈΑ͏ xcodeprojΛ։͖·͢
Xcode্Ͱ࣮ߦ
XCTestͪΖΜ͑·͢ʂ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯ Λ࡞ΔϙΠϯτ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ڧྗͳܕ҆શੑΛ׆͔͢
σʔλϕʔεAPIϨεϙϯεͷIO݁ՌAnyܕ͕΄ͱΜͲ JSON MySQL
σʔλϕʔεAPIϨεϙϯεͷIO݁ՌAnyܕ͕΄ͱΜͲ JSON MySQL AnyܕΛͦͷ··ѻ͏ͱ…
σʔλϕʔεAPIϨεϙϯεͷIO݁ՌAnyܕ͕΄ͱΜͲ JSON MySQL unwrapɺόϦσʔγϣϯΛॻ͘Ίʹ…
Server SideؔΘΔਓ͕ଟ͍ͨΊɺ AnyܕΛ;ΜͩΜʹͬͨίʔυϝϯς͕૬େม
DBͷऔಘ݁ՌͳͲ͖ͪΜͱܕΛఆٛ͠·͠ΐ͏
Enum ڧྗͳܕ҆શੑΛ׆͔͢ EnumʹΑΔύλʔϯϚονϯάɺServer SideͰ༗ޮ HTTP Method HTTP Status
Enum ڧྗͳܕ҆શੑΛ׆͔͢ ErrorͷύλʔϯϚονϯάͰɺΤϥʔϨεϙϯεΛॊೈʹ
ܦݧ্ɺwebαʔϏεΛӡ༻͍ͯͯ͠ɺ αʔόʔ͕མͪΔେ͖ͳݪҼͷҰ͕ͭNullࢀর(ψϧϙ)Ͱ͢ɻ Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢
SwiftʹOptionalܕ͕͋ΓɺϓϩάϥϚͦͷ͕nullΛڐ༰ ͢Δ͔Ͳ͏͔Λࢹ֮తʹஅͰ͖·ͨ͠ ʢίϯύΠϥ͕ڭ͑ͯ͘ΕΔʣ Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢
Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢ JavaScriptͰΫϥογϡ͢Δ͔͠Εͳ͍ྫ
Optionalܕ ڧྗͳܕ҆શੑΛ׆͔͢ SwiftͰ҆શʹIO݁ՌΛѻ͏
SwiftΛαʔόʔͰ͏ϝϦοτͯ͠ɺ ͜ͷnullͷڐ༰ੑͱͯେ͖ͳཁૉͰ͢ɻ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ΫϥΠΞϯτͰϝδϟʔͳϥΠϒϥϦΛੵۃతʹ͏
• SwiftʹiOSͳͲͰங͔ΕͨɺطଘͷΤίγες Ϝ͕͋Δ • UIΛ࣋ͨͳ͍௨৴ॲཧσʔλγϦΞϥΠζʹ ಛԽͨ͠ͷߴ֬ͰServer SideͰ͑Δ • SPMʹରԠ͍ͯ͠ͳ͍߹ɺগ͠ͷมߋͰ Server
SideͰ͑Δέʔε͕͋Δ SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ ΫϥΠΞϯτͰϝδϟʔͳϥΠϒϥϦΛੵۃతʹ͏
• Himotoki • SwiftyJSON • swift-protobuf • RXSwift • JSONWebToken.swift
αʔόʔαΠυͰ͑ΔiOSͷϥΠϒϥϦʢ΄ΜͷҰ෦ʣ
ंྠͷ࠶ൃ໌Λ߇͑ͯɺΤίγεςϜΛੵۃతʹ׆༻͠·͠ΐ͏
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ UbuntuͰಈ͘͜ͱΛ࠷༏ઌʹ
• GoͷΑ͏ͳϙʔλϏϦςΟະͩSwiftʹͳ͍ • SwiftެࣜαϙʔτͷLinuxUbuntu • MacͰ͑ͯUbuntuͰ͑ͳ͍API݁ߏ͋Δ • ςετίʔυΛඞͣUbuntuͰΒͤ·͠ΐ͏ SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ UbuntuͰಈ͘͜ͱΛ࠷༏ઌʹ
Linux൛SwiftͰϋϚͬͨ͜ͱᶃ CF**ͰDNSղܾΛࢼΈ͕ͨ…..
CF**ͰDNSղܾΛࢼΈ͕ͨ….. Linux൛SwiftͰϋϚͬͨ͜ͱᶃ
Linux൛SwiftʹCF**Ͱ͑ͳ͍ͷ͕͋Δ Linux൛SwiftͰϋϚͬͨ͜ͱᶃ
Main thread͔Ͳ͏͔ఆ͠Α͏ͱͯ͠… Linux൛SwiftͰϋϚͬͨ͜ͱᶄ
Main thread͔Ͳ͏͔ఆ͠Α͏ͱͯ͠… Linux൛SwiftͰϋϚͬͨ͜ͱᶄ
Linux൛SwiftͰϋϚͬͨ͜ͱᶄ
࣮͞Εͯͳ͍wwwww
͜ͷΑ͏ʹɺະͩLinux൛SwiftͰτϥοϓ͕ͪΒ΄Β
MacͰಈ͍͔ͨΒͬͯ҆৺͠ͳ͍Α͏ʹʂ
SwiftͰݎ࿚ͰߴͳwebΞϓϦέʔγϣϯΛ࡞ΔϙΠϯτ GCDΛ׆༻ͯ͠ɺαʔόʔϦιʔεΛޮతʹѻ͏
GCDͱɺThreadඇಉظ໋ྩΛ؆୯ʹѻ͏ͨΊͷϥΠϒϥϦ GCDΛ͏·͘ѻ͏͜ͱ͕ɺSwiftͷServer SideΛ߈ུ͢Δ伴
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
Dispatch Source Dispatch Source Socket ࢹ Event Event Event ιέοτϑΝΠϧͷهड़ࢠΛࢹͯ͠ɺ
ॻ͖ࠐΈ͕͋ͬͨλΠϛϯάͰΠϕϯτΛൃՐ selectkqueueͱ͍ͬͨඇಉظܥͷsystem callΛநԽ͍ͯ͠Δ
GCDΛۦͯ͠ɺߴͳαʔόʔΛ࡞Γ·͠ΐ͏
SwiftͰ࣮ફతͳWebΞϓϦέʔγϣϯ Λ࡞ͬͯΈΑ͏
Ϣʔβʔ A Ϣʔβʔ B OAuth OAuth Callback Callback ͜Μͳײ͡ͷwebΞϓϦέʔγϣϯΛ࡞ͬͯΈ·ͨ͠ ChatϧʔϜ
ᶃ ᶃ ᶄ ᶄ ᶅ ᶅ
• GithubͷOAuthΛͬͨϩάΠϯػೳ • JWTʹΑΔϢʔβʔೝূ(ηογϣϯସ) • σʔλશͯMySQLʹอଘ͢Δ • ίʔυଆͰεΩʔϚཧ • ViewSPAͰఏڙ
ͬ͘͟Γͱͨ͠ཁ݅
https://github.com/noppoMan/SwiftJNChatApp
σϞ
https://www.youtube.com/watch?v=WI7BQQHVV_Y&feature=youtu.be ಈըURLͪ͜Β
• 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 ࠓճͬͨϥΠϒϥϦ
noppoMan/Prorsum
noppoMan/Prorosum • HTTP ServerͳͲͷSwiftඪ४Ͱఏڙ͞Ε͍ͯͳ͍ ωοτϫʔΫػೳΛิ͢Δ • LinuxͰಈ͘͜ͱΛલఏʹ։ൃ͞Ε͍ͯΔ • GoݴޠͷgoroutineͱChannelΛΠϯεύΠΞ͠ ͨɺඇಉظ/ಉظॲཧϞσϧ
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
• Ͳ͏ͯ͠ɺCallbackϕʔεͷϑϩʔ੍ޚʹͳͬ ͯ͠·͏ • ConcurrentͰ࣮ߦ͢Δ߹ɺεϨουηʔϑΛҙ ࣝͯ͠ϓϩάϥϛϯάΛ͢Δඞཁ͕͋Δ DispatchQueueͷ
thread Prorsumͷඇಉظ/ಉظॲཧϞσϧ Task1 Task2 Task3 send send send main thread
receive Channel thread thread ProrsumͰݪଇͱͯ͠callbackʹΑΔϑϩʔ੍ޚߦΘͳ͍ɻ Prorosumͷఏڙ͢ΔgoؔͱchannelʹΑΓɺฒྻॲཧͱͦͷಉظΛऔΔ Async Async Async Sync(Blocking)
go()ͱChannelΛ࢛ͬͨଇԋࢉͷྫ
thread લճͷਤʹͯΊΔͱ Task1 Task2 Task3 send(1) send(2) send(3) main thread
Channel thread thread go() go() go() receive(1) receive(2) receive(3)
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
RoutingMiddleware • noppoMan/WebAppKitΛ͍ͬͯΔ • WebAppKitϓϩʔαϜ͚ͷRoutingɺ MiddlewareΛఏڙ͢ΔϑϨʔϜϫʔΫ • ExpressSinatraͷΑ͏ͳͷ • ೝূͳͲ͜ͷMiddlewareͰॲཧ͍ͯ͠Δ
APIͱͷ௨৴ (NS)URLSessionΛͬͯAPIͱ௨৴͍ͯ͠Δɻ ͨͩ͠ɺίʔϧόοΫϕʔεͷϑϩʔ੍ޚʹͨ͘͠ͳ͍ͷͰɺdataTaskResumtSync ͱ͍͏ϝιουΛՃͯ͠ɺpthread_cond_waitͰϒϩοΫ͢ΔΑ͏ʹ͍ͯ͠Δ Routes/GithubRoute.swift
࣮APIKitΛ͍͔ͨͬͨͷͰ͕͢ɺɺ
objc_**͕LinuxͰ͑ͳ͍…
ͥͻLinuxରԠ͍ͨ͠Ͱ͢Ͷʂ
• SwiftyJSONͳͲͷJSONϥΠϒϥϦ͍ͬͯͳ͍ • EntityΛswift-protobufΛͬͯఆ͍ٛͯ͠ΔͨΊɺͦ Ε͕ఏڙ͢ΔJSONγϦΞϥΠζػೳΛ͍ͬͯΔ JSONͷऔѻ
Swift͔ΒMySQLΛѻ͏ ͓खͷSwiftKnexͱ͍͏ϥΠϒϥϦΛ͍ͬͯΔ
• Mysql QueryBuilder • Schema Migration • Pure SwiftͰMySQLϓϩτίϧΛ࣮͍ͯ͠Δ noppoMan/SwiftKnex
None
None
None
ܕ҆શʹselect݁ՌΛѻ͏
ίʔυଆͰεΩʔϚཧ SwiftKnexʹɺεΩʔϚཧ/ϚΠάϨʔγϣϯػೳ͕උΘ͍ͬͯΔ
Migration ClassΛ࡞͢Δ $ ./.build/debug/SwiftKnexMigration create ResourceName ΛίϚϯυϥΠϯ͔Β࣮ߦ͢Δ͜ͱͰɺMigration ClassΛ࡞
Migration/20170218035306_CreateUser.swift Migration Classͷఆٛ
Migrationͷ࣮ߦ/ϩʔϧόοΫ
ΫϥυͷσϓϩΠͳͲɺ ͍ͨ͜͠ͱଞʹࢁ͋ΔͷͰɺͥͻ࠙ձͰʂ
SwiftͰͷαʔόʔαΠυ։ൃເޠͰͳ͘ɺ ͢ͰʹϦΞϧͳͷͰ͋Δͱ͍͏͜ͱ͕ΘΕخ͍͠Ͱ͢ʂ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠