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
540
はじめての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
CSC509 Lecture 13
javiergs
PRO
0
110
Remix on Hono on Cloudflare Workers
yusukebe
1
300
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Ethereum_.pdf
nekomatu
0
470
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.8k
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Outline View in SwiftUI
1024jp
1
340
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
210
Featured
See All Featured
Producing Creativity
orderedlist
PRO
341
39k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
How STYLIGHT went responsive
nonsquared
95
5.2k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
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ͰٞͷࢀՃ͕Մೳ