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

はじめてのSwift Server API / Beginning Swift Server API

はじめてのSwift Server API / Beginning Swift Server API

Shun Takebayashi

September 15, 2017
Tweet

More Decks by Shun Takebayashi

Other Decks in Programming

Transcript

  1. HTTP APIͱ͸ public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing

    public protocol HTTPServing : class { func start(port: Int, handler: @escaping HTTPRequestHandler) throws // ... }
  2. 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 }
  3. 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 }
  4. 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 }
  5. 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 }
  6. 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() } } }
  7. 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() } } }
  8. 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() } } }
  9. 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() } } }
  10. 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() } } }
  11. 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() } } }
  12. 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() } } }
  13. 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 // ... }
  14. 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"]), ] )
  15. ·ͱΊ • Swift Server API͸ϙʔλϒϧͳαʔόʔαΠυSwiftΛ࣮ݱ͢ΔͨΊͷAPI • HTTP API͸HTTPΞϓϦέʔγϣϯͷͨΊͷڞ௨ΠϯλʔϑΣʔε • ΞϓϦέʔγϣϯ͸HTTPRequestHandlerΛ࣮૷

    • αʔόʔ͸HTTPServingΛ࣮૷ • ೚ҙͷαʔόʔͱΞϓϦέʔγϣϯΛ૊Έ߹Θ࣮ͤͯߦՄೳ • ·ͩਖ਼ࣜϦϦʔεલͷ࢓༷Ͱ͋ΓɺML౳Ͱٞ࿦΁ͷࢀՃ͕Մೳ