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

The Ecosystem of Web Development with Swift

The Ecosystem of Web Development with Swift

4cd4fcfc621a4f4a6214bc8433f0b8b8?s=128

Shun Takebayashi

April 21, 2016
Tweet

Transcript

  1. The Ecosystem of Web Development with Swift Shun Takebayashi takebayashi

  2. Web Development

  3. What We Need • Web application frameworks • Web application

    servers • etc…
  4. Frameworks & Servers

  5. Popular Web Frameworks • PerfectLib • Kitura

  6. PerfectLib • First popular framework in Swift • Announced in

    November 2015 • Before Swift was open-sourced
  7. PerfectLib Application public func PerfectServerModuleInit() { Routing.Handler.registerGlobally() Routing.Routes["GET", "/hello"] =

    { _ in return HelloHandler() } } class HelloHandler: RequestHandler { func handleRequest(request: WebRequest, response: WebResponse) { response.appendBodyString("Hello, world!") response.requestCompletedCallback() } }
  8. Deploying PerfectLib Apps • We can deploy PerfectLib-powered Apps with

    PerfectServer • Hard to deploy
  9. Kitura • Developed by IBM • Announced in February 2016

  10. Kitura let router = Router() router.get("/hello") { (request, response, next)

    in response.status(HttpStatusCode.OK).send("Hello, world").end() }
  11. Deploying Kitura Apps let router = Router() router.get("/hello") { (request,

    response, next) in response.status(HttpStatusCode.OK).send("Hello, world").end() } // Deploy Kitura-app with Kitura-net let server = HttpServer.listen(8080, delegate: router) Server.run()
  12. Problem: Tightly Coupled We can deploy • PerfectLib apps with

    PerfectServer • Kitua apps with Kitura-net
  13. Problem: Tightly Coupled We can’t deploy • PerfectLib apps with

    Kitura-net
  14. Solution: Common Interface • Python: WSGI • Perl: PSGI •

    Ruby: Rack
  15. Nest

  16. What is Nest? • Minimal interface between apps and servers

    • Set of Swift protocols
  17. Nest Application public protocol RequestType { var method: String {

    get } var path: String { get } var headers: [Header] { get } var body: PayloadType? { get set } } public protocol ResponseType { var statusLine: String { get } var headers: [Headers] { get } var body: PayloadType? { get set } } public typealias Application = RequestType -> ResponseType
  18. Frameworks Supporting Nest • Swifton • Frank

  19. Servers Supporting Nest • Curassow • http4swift

  20. Nest Example import Inquiline let app: Application = { request:

    RequestType -> ResponseType in return Response(status: .Ok) }
  21. Nest Example import Inquiline let app: Application = { request:

    RequestType -> ResponseType in return Response(status: .Ok) } // Deploy with Curassow import Curassow serve(app)
  22. Nest Example import Inquiline let app: Application = { request:

    RequestType -> ResponseType in return Response(status: .Ok) } // Deploy with http4swift import http4swift let server = HTTPServer(port: 8080)! server.serve(app)
  23. Nest Tools • NestTest • NestTestSuite

  24. OpenSwift

  25. What is OpenSwift? • Cross project standards for Swift •

    Sub-projects • C7 (Core components) • S4 (Server-side) • D5 (Database)
  26. What is C7? • Standard data types • Data •

    Stream • URI • etc…
  27. What is S4? • Server-Side Swift Standards • Built on

    C7 components • More Swifty
  28. S4 Application public protocol AsyncResponder { func respond( to request:

    Request, result: (Void throws -> Response) -> Void ) } public protocol Responder: AsyncResponder { func respond(to request: Request) throws -> Response }
  29. S4 Server public protocol AsyncServer { func serve( _ responder:

    AsyncResponder, on host: String, at port: Int ) throws } public protocol Server { func serve( _ responder: Responder, on host: String, at port: Int ) throws }
  30. Frameworks Supporting S4 • Vapor • Zewo • Slimane

  31. Servers Supporting S4 • VeniceX • Skelton

  32. Nest vs. S4 /FTU 4 $PNNVOJUZ 4NBMM -BSHF "1* 4UBCMF

    6OTUBCMF 4QFD 4JNQMF $PNQMFY
  33. Which Should We Use? Both Nest and S4 are fine

  34. Which Should We Use? Case • Your favorite framework supports

    Nest • Your favorite server supports S4 You can adopt adapters
  35. Conclusion • For app developers: • Use frameworks supporting Nest

    or S4 • For server/framework developers: • Implement based on Nest or S4
  36. Happy Server-Side Swift