Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction of Swift HTTP APIs

Introduction of Swift HTTP APIs

Shun Takebayashi

June 27, 2017
Tweet

More Decks by Shun Takebayashi

Other Decks in Programming

Transcript

  1. History of Server-Side Swift ( - Mid 2016) • App

    frameworks • Perfect • Kitura • No standard APIs • Third-party API specifications • Nest • OpenSwift / S4
  2. The Ecosystem of Web Development with Swift Shun Takebayashi takebayashi

    https://speakerdeck.com/takebayashi/the-ecosystem-of-web-development-with-swift
  3. History of Server-Side Swift (Oct 2016) • Apple announces Server

    APIs Work Group • Working on 3 layers: • Base networking • Security / Encryption • HTTP / WebSocket
  4. ͜Ε͔Β͸͡ΊΔ αʔόʔαΠυ Swift Tokyo Server Side Swift Meetup #7 @

    Speee Lounge - 2017.5.26 https://speakerdeck.com/jp_pancake/korekarahazimerusabasaido-swift
  5. Types in HTTP APIs • HTTPRequest • HTTPResponse • HTTPResponseWriter

    • HTTPBodyHandler • HTTPBodyProcessing • HTTPBodyChunk • etc…
  6. Interfaces in HTTP APIs enum HTTPBodyProcessing { case discardBody case

    processBody(handler: HTTPBodyHandler) } typealias HTTPBodyHandler = (HTTPBodyChunk, inout Bool) -> Void
  7. 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() } } }
  8. BlueSocketHTTP • Prototype implementation of HTTP APIs • Based on

    IBM BlueSocket https://github.com/swift-server/http/tree/develop/Sources/BlueSocketHTTP
  9. Deploying with BlueSocketHTTP import HTTP import BlueSocketHTTP let server =

    BlueSocketSimpleServer() do { try server.start(port: 8080, webapp: myApp) }
  10. 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