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
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
180
AIのコンプラは何故しんどい?
shujisado
1
190
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
120
アップデート紹介:AWS Data Transfer Terminal
stknohg
PRO
0
170
20241220_S3 tablesの使い方を検証してみた
handy
3
240
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
240
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
370
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
CustomCopを使ってMongoidのコーディングルールを整えてみた
jinoketani
0
220
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
150
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
1
110
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
520
39k
Become a Pro
speakerdeck
PRO
26
5k
Bash Introduction
62gerente
608
210k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
A Tale of Four Properties
chriscoyier
157
23k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Practical Orchestrator
shlominoach
186
10k
A better future with KSS
kneath
238
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ͰͷαʔόʔαΠυ։ൃເޠͰͳ͘ɺ ͢ͰʹϦΞϧͳͷͰ͋Δͱ͍͏͜ͱ͕ΘΕخ͍͠Ͱ͢ʂ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠