Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Swift3 Framework Slimane and Server Side Swift ...

Yuki Takei
August 19, 2016

Swift3 Framework Slimane and Server Side Swift (ja)

iOSDC 2016 Aug, 19

Yuki Takei

August 19, 2016
Tweet

More Decks by Yuki Takei

Other Decks in Technology

Transcript

  1. • C7ɿCore standards for Swift • S4: HTTP standards for

    Swift • D5: Database standards for Swift ҎԼͷ3ͭͷϨϙδτϦ͕ଘࡏ͢Δ
  2. C7 • Stream • Connection • Data • URI •

    JSON Stream(TCPɺPipe etc..)ɺURIɺDataߏ଄ͳͲͷɺ ΞϓϦέʔγϣϯͷجຊ෦෼ͷ࢓༷ɺΠϯλʔϑΣʔε
  3. • Request • Response • HTTP Status Code • Server

    • Middleware • Responder S4 WebͷͨΊͷ࢓༷ɺΠϯλʔϑΣʔε(rubyͷrackʹ͍ۙ)
  4. • ExpressΛinspireͨܰ͠ྔWAF/Web Server • Node.jsͷ5ഒɺRevelͷ2ഒߴ଎ʢͩͬͨʣ • ΠϕϯτϧʔϓϞσϧɺϊϯϒϩοΩϯάI/O • Master/WorkerύλʔϯʹΑΔϚϧνεϨουରԠ •

    Core Foundationʹґଘ͠ͳ͍ • ϚΠΫϩαʔϏε޲͚ͷAPIαʔόʔ΍SwiftͰඇಉظωοτ ϫʔΫϓϩάϥϛϯάΛߦ͏͜ͱ͕ओ໨త Slimaneͱ͸
  5. Layer system call thread pool event loop Swift bindings of

    libuv Async net working platform TCP, UDP, Process, FS, DNS etc.. HTTP Server WAF(Routing, Middleware)
  6. Runtime EVENT LOOP FILE SYSTEM NETWORK NETWORK System Operation &

    Register Callback COMPUTATION Operation Complete Trigger Callback Requests Suv(libuv wapper) epoll kqueue worker threads
  7. ➜ slimane git:(master) ✗ slimane new TestApp [~/Documents/program/oss/slimane/slimane] /Users/yuki/Documents/program/oss/Slimane/Slimane/TestApp ├──

    Makefile ├── Package.swift ├── Sources │ ├── app.swift │ └── main.swift └── public └── images └── Slimane_logo.jpg 3 directories, 5 files ➜ TestApp git:(master) ✗ slimane build ここでSPMの依存解決、コンパイルが走る。(結構時間かかります。) ➜ TestApp git:(master) ✗ .build/debug/TestApp [~/Documents/program/oss/slimane/slimane/TestApp] The server is listening at 0.0.0.0:3000 [pid:50928] Monday, August 01 2016 00:11:48 /
  8. ➜ slimane git:(master) ✗ slimane new TestApp [~/Documents/program/oss/slimane/slimane] /Users/yuki/Documents/program/oss/Slimane/Slimane/TestApp ├──

    Makefile ├── Package.swift ├── Sources │ ├── app.swift │ └── main.swift └── public └── images └── Slimane_logo.jpg 3 directories, 5 files ➜ TestApp git:(master) ✗ slimane build ここでSPMの依存解決、コンパイルが走る。(結構時間かかります。) ➜ TestApp git:(master) ✗ .build/debug/TestApp [~/Documents/program/oss/slimane/slimane/TestApp] The server is listening at 0.0.0.0:3000 [pid:50928] Monday, August 01 2016 00:11:48 / ϓϩδΣΫτͷεχϖοτΛ࡞੒
  9. ➜ slimane git:(master) ✗ slimane new TestApp [~/Documents/program/oss/slimane/slimane] /Users/yuki/Documents/program/oss/Slimane/Slimane/TestApp ├──

    Makefile ├── Package.swift ├── Sources │ ├── app.swift │ └── main.swift └── public └── images └── Slimane_logo.jpg 3 directories, 5 files ➜ TestApp git:(master) ✗ slimane build ここでSPMの依存解決、コンパイルが走る。(結構時間かかります。) ➜ TestApp git:(master) ✗ .build/debug/TestApp [~/Documents/program/oss/slimane/slimane/TestApp] The server is listening at 0.0.0.0:3000 [pid:50928] Monday, August 01 2016 00:11:48 /
  10. ➜ slimane git:(master) ✗ slimane new TestApp [~/Documents/program/oss/slimane/slimane] /Users/yuki/Documents/program/oss/Slimane/Slimane/TestApp ├──

    Makefile ├── Package.swift ├── Sources │ ├── app.swift │ └── main.swift └── public └── images └── Slimane_logo.jpg 3 directories, 5 files ➜ TestApp git:(master) ✗ slimane build ここでSPMの依存解決、コンパイルが走る。(結構時間かかります。) ➜ TestApp git:(master) ✗ .build/debug/TestApp [~/Documents/program/oss/slimane/slimane/TestApp] The server is listening at 0.0.0.0:3000 [pid:50928] Monday, August 01 2016 00:11:48 / SPMɺModuleMapͷґଘղܾ / ίϯύΠϧ
  11. ➜ slimane git:(master) ✗ slimane new TestApp [~/Documents/program/oss/slimane/slimane] /Users/yuki/Documents/program/oss/Slimane/Slimane/TestApp ├──

    Makefile ├── Package.swift ├── Sources │ ├── app.swift │ └── main.swift └── public └── images └── Slimane_logo.jpg 3 directories, 5 files ➜ TestApp git:(master) ✗ slimane build ここでSPMの依存解決、コンパイルが走る。(結構時間かかります。) ➜ TestApp git:(master) ✗ .build/debug/TestApp [~/Documents/program/oss/slimane/slimane/TestApp] The server is listening at 0.0.0.0:3000 [pid:50928] Monday, August 01 2016 00:11:48 /
  12. ➜ slimane git:(master) ✗ slimane new TestApp [~/Documents/program/oss/slimane/slimane] /Users/yuki/Documents/program/oss/Slimane/Slimane/TestApp ├──

    Makefile ├── Package.swift ├── Sources │ ├── app.swift │ └── main.swift └── public └── images └── Slimane_logo.jpg 3 directories, 5 files ➜ TestApp git:(master) ✗ slimane build ここでSPMの依存解決、コンパイルが走る。(結構時間かかります。) ➜ TestApp git:(master) ✗ .build/debug/TestApp [~/Documents/program/oss/slimane/slimane/TestApp] The server is listening at 0.0.0.0:3000 [pid:50928] Monday, August 01 2016 00:11:48 / Ϗϧυޙʹग़དྷ্͕ͬͨόΠφϦΛ࣮ߦ
  13. struct HelloWorldResponser: AsyncResponder { public func respond(to request: Request, result:

    ((Void) throws -> Response) -> Void) result { Response(body: “Hello world") } } } Responder
  14. MiddlewareͷίʔυʢϩάΠϯνΣοΫʣ public struct AuthenticationMiddleware: AsyncMiddleware { public func respond(to request:

    Request, chainingTo next: AsyncResponder, result: ((Void) throws -> Response) -> Void) { if request.session?["isAuthorized"] != nil { return next.respond(to: request, result: result) } result { Response(status: .unauthorized) } } }
  15. MiddlewareͷίʔυʢϩάΠϯνΣοΫʣ public struct AuthenticationMiddleware: AsyncMiddleware { public func respond(to request:

    Request, chainingTo next: AsyncResponder, result: ((Void) throws -> Response) -> Void) { if request.session?["isAuthorized"] != nil { return next.respond(to: request, result: result) } result { Response(status: .unauthorized) } } } ϩάΠϯ͍ͯͨ͠Β࣍ͷMiddlewareʹνΣΠϯ
  16. MiddlewareͷίʔυʢϩάΠϯνΣοΫʣ public struct AuthenticationMiddleware: AsyncMiddleware { public func respond(to request:

    Request, chainingTo next: AsyncResponder, result: ((Void) throws -> Response) -> Void) { if request.session?["isAuthorized"] != nil { return next.respond(to: request, result: result) } result { Response(status: .unauthorized) } } } ϩάΠϯ͍ͯ͠ͳ͚Ε͹ɺ401Λฦ͢
  17. ૊Έ߹ΘͤΔͱ…. import Slimane let app = Slimane() app.use { req,

    next, result in print("middleware") next.respond(to: req, result: result) } app.get("/") { req, responder in responder { Response(body: "Hello iOSDC") } } try! app.listen() // => listening at 0.0.0.0:3000
  18. Express Slimane const express = require('express'); const app = express();

    app.use((req, res, next) => { console.log("middleware"); next(); }); app.get("/", (req, res) => { res.send("Hello iOSDC"); }); app.listen(); // => listening at 0.0.0.0:3000 import Slimane let app = Slimane() app.use { req, next, result in print("middleware") next.respond(to: req, result: result) } app.get("/") { req, responder in responder { Response(body: "Hello iOSDC") } } try! app.listen() // => listening at 0.0.0.0:3000 ίʔυΛExpressͱൺֱ
  19. Express Slimane const express = require('express'); const app = express();

    app.use((req, res, next) => { console.log("middleware"); next(); }); app.get("/", (req, res) => { res.send("Hello iOSDC"); }); app.listen(); // => listening at 0.0.0.0:3000 import Slimane let app = Slimane() app.use { req, next, result in print("middleware") next.respond(to: req, result: result) } app.get("/") { req, responder in responder { Response(body: "Hello iOSDC") } } try! app.listen() // => listening at 0.0.0.0:3000 ίʔυΛExpressͱൺֱ ׂͱࣅͯ·͢Ͷɻ
  20. import Slimane import Render import MustacheViewEngine let app = Slimane()

    app.get("/") { req, result in result { let templateData: TemplateData = ["name": "Slimane"] let render = Render(engine: MustacheViewEngine(templateData: templateData), path: “index”) return Response(custom: render) } } try! app.listen() RenderΛ࢖ͬͯHTMLΛ഑৴͢Δྫ
  21. import Slimane import Render import MustacheViewEngine let app = Slimane()

    app.get("/") { req, result in result { let templateData: TemplateData = ["name": "Slimane"] let render = Render(engine: MustacheViewEngine(templateData: templateData), path: “index”) return Response(costom: render) } } try! app.listen() RenderΛ࢖ͬͯHTMLΛ഑৴͢Δྫ MustacheViewEngineΛengineͱͯ͠ɺ RenderΦϒδΣΫτΛ࡞੒
  22. import Slimane import Render import MustacheViewEngine let app = Slimane()

    app.get("/") { req, result in result { let templateData: TemplateData = ["name": "Slimane"] let render = Render(engine: MustacheViewEngine(templateData: templateData), path: “index”) return Response(custom: render) } } try! app.listen() RenderΛ࢖ͬͯHTMLΛ഑৴͢Δྫ Response.initializerͷcostom labelʹ render ObjectΛ౉͢
  23. import Slimane import BodyParser import JSON let app = Slimane()

    app.use(BodyParser.JSON()) app.post("/") { req, responder in print(req.json?[“string”].asString) print(req.json?[“number”].asNumber) print(req.json?[“object”].asObject) responder { let successJSON: JSON = ["message": "OK"] Response( status: .created, headers: Header([“Content-Type”: “application/json“]), body: try JSONMapSerializer.serialize(successJSON) ) } } application/jsonͷRequest/Responseͷྫ
  24. import Slimane import BodyParser import JSON let app = Slimane()

    app.use(BodyParser.JSON()) app.post("/") { req, responder in print(req.json?[“string”].asString) print(req.json?[“number”].asNumber) print(req.json?[“object”].asObject) responder { let successJSON: Map = ["message": "OK"] Response( status: .created, headers: Header([“Content-Type”: “application/json“]), body: try JSONMapSerializer.serialize(successJSON) ) } } main.swift JSON BodyParserΛMiddlewareʹొ࿥
  25. import Slimane import BodyParser import JSON let app = Slimane()

    app.use(BodyParser.JSON()) app.post("/") { req, responder in print(req.json?[“string”].asString) print(req.json?[“number”].asNumber) print(req.json?[“object”].asObject) responder { let successJSON: Map = ["message": "OK"] Response( status: .created, headers: Header([“Content-Type”: “application/json“]), body: try JSONMapSerializer.serialize(successJSON) ) } } main.swift JSON Value΁ͷΞΫηε
  26. import Slimane import BodyParser import JSON let app = Slimane()

    app.use(BodyParser.JSON()) app.post("/") { req, responder in print(req.json?[“string”].asString) print(req.json?[“number”].asNumber) print(req.json?[“object”].asObject) responder { let successJSON: JSON = ["message": "OK"] Response( status: .created, headers: Header([“Content-Type”: “application/json“]), body: try JSONSerializer.serialize(successJSON) ) } } main.swift JSONܕ(Type safeͳߏ଄ܕ)ͰDictionaryΛੜ੒͠ɺ JSONSerializer.serializeʹ౉͢͜ͱͰJSONStringʹ
  27. ίʔυ import Suv let onThread = { ctx in let

    sql = "select * from foo where bar = 1" let ctx.storage[“result”] = mysql_real_query(con, sql, sql.characters.count) let res = mysql_use_result(con) result = mysql_foo..... } let onFinish = { ctx in print(ctx.storage[“result”]) } Process.qwork(onThread: onThread, onFinish: onFinish)
  28. • HTTPS Client • JSON • Session • DataBaseClient •

    View • Web socket Server/Client • Deploy SlimaneҎ֎ͰɺGithubΞΧ΢ϯτΛ࢖ͬͨ ChatαʔϏεʹඞཁͳ΋ͷ
  29. • HTTPS Client -> slimane-swift/SecureHanger • JSON • Session •

    DataBaseClient • View • Web socket • Deploy SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  30. • HTTPS Client -> slimane-swift/SecureHanger • JSON -> Zewo/JSON, slimane-swift/BodyParser

    • Session • DataBaseClient • View • Web socket • Deploy SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  31. • HTTPS Client -> slimane-swift/SecureHanger • JSON -> Zewo/JSON, slimane-swift/BodyParser

    • Session -> slimane-swift/SessionMiddleware • DataBaseClient • View • Web socket • Deploy SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  32. • HTTPS Client -> slimane-swift/SecureHanger • JSON -> Zewo/JSON, slimane-swift/BodyParser

    • Session -> slimane-swift/SessionMiddleware • DataBaseClient -> noppoMan/swift-redis • View • Web socket • Deploy SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  33. • HTTPS Client -> slimane-swift/SecureHanger • JSON -> Zewo/JSON, slimane-swift/BodyParser

    • Session -> slimane-swift/SessionMiddleware • DataBaseClient -> noppoMan/swift-redis • View -> slimane-swift/Render • Web socket • Deploy SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  34. • HTTPS Client -> slimane-swift/SecureHanger • JSON -> Zewo/JSON, slimane-swift/BodyParser

    • Session -> slimane-swift/SessionMiddleware • DataBaseClient -> noppoMan/swift-redis • View -> slimane-swift/Render • Web socket -> slimane-swift/WS • Deploy SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  35. • HTTPS Client -> slimane-swift/SecureHanger • JSON -> Zewo/JSON, slimane-swift/BodyParser

    • Session -> slimane-swift/SessionMiddleware • DataBaseClient -> noppoMan/swift-redis • View -> slimane-swift/Render • Web socket -> slimane-swift/WS • Deploy -> Suv.Cluster and Suv.Signal SlimaneҎ֎ͰɺTwitterΞΧ΢ϯτΛ࢖ͬͨ ChatΞϓϦʹඞཁͳ΋ͷ
  36. • TLS/HTTP 2.0 • QUIC • Async/Await • Actors or

    Coroutines(Other Project) Next Step