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
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
190
Go の GC の不得意な部分を克服したい
taiyow
2
770
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
740
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
2
180
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
return文におけるstd::moveについて
onihusube
1
1k
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Making Projects Easy
brettharned
116
5.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Typedesign – Prime Four
hannesfritz
40
2.4k
Building an army of robots
kneath
302
44k
Scaling GitHub
holman
458
140k
Mobile First: as difficult as doing things right
swwweet
222
9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
GitHub's CSS Performance
jonrohan
1030
460k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
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ͰٞͷࢀՃ͕Մೳ