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
はじめてのSwift Server API / Beginning Swift Server API
Search
Shun Takebayashi
September 15, 2017
Programming
1
530
はじめてのSwift Server API / Beginning Swift Server API
Shun Takebayashi
September 15, 2017
Tweet
Share
More Decks by Shun Takebayashi
See All by Shun Takebayashi
Introduction of Swift HTTP APIs
takebayashi
0
280
The Ecosystem of Web Development with Swift
takebayashi
4
1.2k
Other Decks in Programming
See All in Programming
EventSourcingの理想と現実
wenas
6
2.1k
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
1.7k
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
160
C#/.NETのこれまでのふりかえり
tomokusaba
1
150
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.3k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
9
980
Identifying User Idenity
moro
6
7.7k
Vue.js学習の振り返り
hiro_xre
2
130
Server Driven Compose With Firebase
skydoves
0
390
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
150
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
664
120k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
7
150
YesSQL, Process and Tooling at Scale
rocio
167
14k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
The World Runs on Bad Software
bkeepers
PRO
65
11k
Teambox: Starting and Learning
jrom
132
8.7k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
32
2.4k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Designing for Performance
lara
604
68k
Transcript
͡ΊͯͷSwift Server API Shun Takebayashi
ΞδΣϯμ • Swift Server APIͱ • HTTP APIͱ • αϯϓϧʹֶͿHTTP
API • HTTP APIͷݱঢ় • ·ͱΊ
Swift Server APIͱ
Swift Server APIͱ αʔόʔαΠυͰඞཁͱͳΔίϯϙʔωϯτΛඪ४Խͨ͠APIͷ૯শ
Swift Server APIͱ త • ૬ޓӡ༻ੑͷ֬อ • ϚϧνϓϥοτϑΥʔϜͷରԠ
Swift Server APIͱ Χόʔ͢ΔྖҬ • ωοτϫʔΫ • ηΩϡϦςΟ • HTTP
Swift Server APIͱ Χόʔ͢ΔྖҬ • ωοτϫʔΫ • ηΩϡϦςΟ • HTTP
HTTP APIͱ
HTTP APIͱ αʔόʔαΠυͰಈ͘HTTPΞϓϦέʔγϣϯͷͨΊͷجຊతͳίϯϙʔωϯτ • ܕΠϯλʔϑΣʔε • σϑΥϧτͷ࣮ ΣϒΞϓϦέʔγϣϯϑϨʔϜϫʔΫͰͳ͍
HTTP APIͱ ΞϓϦέʔγϣϯ ϦΫΤετ Ϩεϙϯε αʔό ϦΫΤετ Ϩεϙϯε ΫϥΠΞϯτ
HTTP APIͱ ΞϓϦέʔγϣϯ ϦΫΤετ Ϩεϙϯε αʔό ϦΫΤετ Ϩεϙϯε ΫϥΠΞϯτ
HTTP APIͱ HTTPRequestHandler HTTPRequest HTTPResponseStatus, … HTTPServing ϦΫΤετ Ϩεϙϯε ΫϥΠΞϯτ
HTTP APIͱ public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing
public protocol HTTPServing : class { func start(port: Int, handler: @escaping HTTPRequestHandler) throws // ... }
αϯϓϧʹֶͿHTTP API
let app: HTTPRequestHandler = { (request, responseWriter) in let headers:
HTTPHeaders = ["Content-Type": "text/plain"] responseWriter.writeHeader(status: .ok, headers: headers) responseWriter.writeBody("Hello, world!") return .discardBody }
let app: HTTPRequestHandler = { (request, responseWriter) in let headers:
HTTPHeaders = ["Content-Type": "text/plain"] responseWriter.writeHeader(status: .ok, headers: headers) responseWriter.writeBody("Hello, world!") return .discardBody }
let app: HTTPRequestHandler = { (request, responseWriter) in let headers:
HTTPHeaders = ["Content-Type": "text/plain"] responseWriter.writeHeader(status: .ok, headers: headers) responseWriter.writeBody("Hello, world!") return .discardBody }
let app: HTTPRequestHandler = { (request, responseWriter) in let headers:
HTTPHeaders = ["Content-Type": "text/plain"] responseWriter.writeHeader(status: .ok, headers: headers) responseWriter.writeBody("Hello, world!") return .discardBody }
try! HTTPServer().start(port: 8080, handler: app)
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
let app: HTTPRequestHandler = { (request, responseWriter) in responseWriter.writeHeader(status: .ok)
return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): responseWriter.writeBody(data) { _ in finishedProcessing() } case .end: responseWriter.done() default: stop = true responseWriter.abort() } } }
try! HTTPServer().start(port: 8080, handler: app)
public typealias HTTPBodyHandler = (HTTPBodyChunk, inout Bool) -> Void public
enum HTTPBodyProcessing { case discardBody case processBody(handler: HTTPBodyHandler) } public enum HTTPBodyChunk { case chunk(data: DispatchData, finishedProcessing: ()-> Void) case end // ... }
αϯϓϧʹֶͿHTTP API جຊతͳྲྀΕ • HTTPRequestHandlerΛ࣮͢Δ • HTTPRequestΛड͚औΓɺHTTPResponseWriterʹॻ͖ࠐΉ • ϦΫΤετϘσΟΛॲཧ͢Δ߹HTTPBodyHandlerΛ࣮͢Δ •
ෳճ࣮ߦ͞ΕΔՄೳੑ͕͋Δ (Chunked Transfer)
HTTP APIͷݱঢ়
HTTP APIͷݱঢ় ਖ਼ࣜϦϦʔεલ ·ͩਖ਼ࣜϦϦʔε͞Ε͓ͯΒͣɺʑվળ͞Ε͍ͯΔ • https://github.com/swift-server/http •
[email protected]
ഁյతͳมߋ͕ߦΘΕΔՄೳੑ͋Δ
HTTP APIͷݱঢ় HTTP APIʹґଘͨ͠ΞϓϦέʔγϣϯͷϏϧυํ๏ λά͕ৼΒΕ͍ͯͳ͍ͨΊɺSwift 4ͷPackage Manager͕ඞཁ let package =
Package( name: "swift-http-example", dependencies: [ .package(url: "https://github.com/swift-server/http.git", .branch("develop")), ], targets: [ .target(name: "swift-http-example", dependencies: ["SwiftServerHTTP"]), ] )
·ͱΊ
·ͱΊ • Swift Server APIϙʔλϒϧͳαʔόʔαΠυSwiftΛ࣮ݱ͢ΔͨΊͷAPI • HTTP APIHTTPΞϓϦέʔγϣϯͷͨΊͷڞ௨ΠϯλʔϑΣʔε • ΞϓϦέʔγϣϯHTTPRequestHandlerΛ࣮
• αʔόʔHTTPServingΛ࣮ • ҙͷαʔόʔͱΞϓϦέʔγϣϯΛΈ߹Θ࣮ͤͯߦՄೳ • ·ͩਖ਼ࣜϦϦʔεલͷ༷Ͱ͋ΓɺMLͰٞͷࢀՃ͕Մೳ