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
270
Server Side Swift の個人的な利用シーン
noppoman
2
580
Serverless Server Side Swift
noppoman
2
1.6k
Practical Hexaville(ja)
noppoman
0
470
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
420
Server Side Framework Slimaneの紹介
noppoman
1
1.1k
Other Decks in Technology
See All in Technology
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
460
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
120
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
組織に自動テストを書く文化を根付かせる戦略(2024冬版) / Building Automated Test Culture 2024 Winter Edition
twada
PRO
17
4.6k
.NET 9 のパフォーマンス改善
nenonaninu
0
1k
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
170
TSKaigi 2024 の登壇から広がったコミュニティ活動について
tsukuha
0
160
kargoの魅力について伝える
magisystem0408
0
210
UI State設計とテスト方針
rmakiyama
2
620
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
180
Featured
See All Featured
Navigating Team Friction
lara
183
15k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Rails Girls Zürich Keynote
gr2m
94
13k
Building Adaptive Systems
keathley
38
2.3k
A designer walks into a library…
pauljervisheath
204
24k
The Cost Of JavaScript in 2023
addyosmani
45
7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Code Reviewing Like a Champion
maltzj
520
39k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
GitHub's CSS Performance
jonrohan
1030
460k
Code Review Best Practice
trishagee
65
17k
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ͰͷαʔόʔαΠυ։ൃເޠͰͳ͘ɺ ͢ͰʹϦΞϧͳͷͰ͋Δͱ͍͏͜ͱ͕ΘΕخ͍͠Ͱ͢ʂ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠