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
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
cmp.Or に感動した
otakakot
1
120
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
役立つログに取り組もう
irof
28
9.6k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
890
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Navigating Team Friction
lara
183
14k
Facilitating Awesome Meetings
lara
50
6.1k
Docker and Python
trallard
40
3.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Agile that works and the tools we love
rasmusluckow
327
21k
Being A Developer After 40
akosma
86
590k
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ͰٞͷࢀՃ͕Մೳ