Slide 1

Slide 1 text

Introduction of Swift HTTP APIs Shun Takebayashi

Slide 2

Slide 2 text

History of Server-Side Swift

Slide 3

Slide 3 text

History of Server-Side Swift (Dec 2015) • Apple open-sourced Swift

Slide 4

Slide 4 text

History of Server-Side Swift ( - Mid 2016) • App frameworks • Perfect • Kitura • No standard APIs • Third-party API specifications • Nest • OpenSwift / S4

Slide 5

Slide 5 text

The Ecosystem of Web Development with Swift Shun Takebayashi takebayashi https://speakerdeck.com/takebayashi/the-ecosystem-of-web-development-with-swift

Slide 6

Slide 6 text

History of Server-Side Swift (Oct 2016) • Apple announces Server APIs Work Group • Working on 3 layers: • Base networking • Security / Encryption • HTTP / WebSocket

Slide 7

Slide 7 text

͜Ε͔Β͸͡ΊΔ αʔόʔαΠυ Swift Tokyo Server Side Swift Meetup #7 @ Speee Lounge - 2017.5.26 https://speakerdeck.com/jp_pancake/korekarahazimerusabasaido-swift

Slide 8

Slide 8 text

What are HTTP APIs?

Slide 9

Slide 9 text

HTTP APIs are NOT… • Web application frameworks • Stable specification

Slide 10

Slide 10 text

HTTP APIs are… • Low-level types and interfaces • Still under discussion

Slide 11

Slide 11 text

Exploring HTTP APIs

Slide 12

Slide 12 text

https://github.com/swift-server/http/blob/develop/API.md

Slide 13

Slide 13 text

Types in HTTP APIs • HTTPRequest • HTTPResponse • HTTPResponseWriter • HTTPBodyHandler • HTTPBodyProcessing • HTTPBodyChunk • etc…

Slide 14

Slide 14 text

Interfaces in HTTP APIs typealias WebApp = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing

Slide 15

Slide 15 text

Interfaces in HTTP APIs enum HTTPBodyProcessing { case discardBody case processBody(handler: HTTPBodyHandler) } typealias HTTPBodyHandler = (HTTPBodyChunk, inout Bool) -> Void

Slide 16

Slide 16 text

Sample App let myApp = { (req: HTTPRequest, res: HTTPResponseWriter ) -> HTTPBodyProcessing in res.writeResponse(HTTPResponse( httpVersion: req.httpVersion, status: .ok, transferEncoding: .chunked, headers: HTTPHeaders([("Content-Type", "text/plain")]) )) return .processBody { (chunk, stop) in switch chunk { case .chunk(let data, let finishedProcessing): res.writeBody(data: data) { _ in finishedProcessing() } case .end: res.done() default: stop = true res.abort() } } }

Slide 17

Slide 17 text

Implementations

Slide 18

Slide 18 text

BlueSocketHTTP • Prototype implementation of HTTP APIs • Based on IBM BlueSocket https://github.com/swift-server/http/tree/develop/Sources/BlueSocketHTTP

Slide 19

Slide 19 text

Deploying with BlueSocketHTTP import HTTP import BlueSocketHTTP let server = BlueSocketSimpleServer() do { try server.start(port: 8080, webapp: myApp) }

Slide 20

Slide 20 text

Summary

Slide 21

Slide 21 text

Swift HTTP APIs are… • Low-level types and functions • for handling/responding to requests • Unstable, but prototype implementations are available • You can try with BlueSocketHTTP

Slide 22

Slide 22 text

Happy Server-Side Swift