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

Going Server-Side with Swift

Going Server-Side with Swift

Ian Partridge

June 01, 2017
Tweet

More Decks by Ian Partridge

Other Decks in Programming

Transcript

  1. 2 Ian Partridge • Team leader in Swift@IBM engineering •

    Background in runtime systems • Java garbage collection • Runtime performance analysis • Full-stack debugging • Swift@IBM @ianpartridge @alfa
  2. 3 Today • Open source Swift • Swift on the

    Server • Kitura web framework • Emerging architecture patterns • Deploying Swift to the cloud
  3. IBM Swift Sandbox • Interactive sandbox for exploring Swift •

    Saves your work • Supports multiple versions of Swift • Deploys your Swift code in a Linux Docker container 8 https://swiftlang.ng.bluemix.net/
  4. Kitura Web Framework http://github.com/IBM-Swift/Kitura 10 • Open source web framework

    for Swift on macOS and Linux • Inspired by Express for Node.js • Flexible routing • Pluggable middlewares • Easy to deploy
  5. Trusted, permissive licence • Apache Software Licence v2.0 • Third

    most popular OSS licence • After MIT, GPLv2 • Includes patent licence requirements • Trusted by community and industry 12
  6. Kitura Architecture 13 Web Framework Web Framework Source C Helpers

    & Interfaces Swift Libraries SwiftyJSON SwiftMongoDB Swift Binary Foundation Package Manager C Libraries Dispatch HttpParser HiRedis CURL PCRE2 Pluggable Components
  7. Apple Client Deployment Server/Cloud Deployment Application-Specific Cloud Services Client Facing

    App Bringing Swift to the Server Foundation Swift Swift Standard Library Core Foundation Dispatch PWQ C libs GLibc Foundation Swift Swift Standard Library Core Foundation Dispatch Darwin C libs Client-specific Libraries App Libraries Server-specific Libraries App Libraries Driving Towards Consistent Runtime across Clients/Servers Server-side Environments (Built with Foundation & Libdispatch)
  8. Why Swift on the server? • Runtime characteristics • Performance

    – Compiled, no JIT • Memory usage – ARC, not a tracing GC • Developer experience • Isomorphic Development • Fun! 15
  9. Developing a Kitura web application 20 myProject ├── Package.swift ├──

    Sources │ └── main.swift └── Tests $ mkdir myProject && cd myProject 2. Next, initialize this project as a new Swift package 1. First, we create a new project directory $ swift package init --type executable Basic Swift package directory structure:
  10. Developing a Kitura web application 21 import PackageDescription let package

    = Package( name: "myProject", dependencies: [ .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 7) ] ) 3. Add Kitura as a dependency for your project (Package.swift):
  11. Developing a Kitura web application 22 Kitura.addHTTPServer(onPort: 8090, with: router)

    Kitura.run() let router = Router() router.get("/hello") { request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").end() } 6. Create and start an HTTPServer: 4. Add a router and a path: router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } 5. Add a JSON data route
  12. Adding features 23 • Templating • Mustache or Stencil •

    Static file serving • Custom middlewares • Implemented via a simple protocol • TLS • WebSockets
  13. Adding authentication via OAuth 2.0 • HTTP Basic/Digest Auth •

    https://github.com/IBM-Swift/Kitura-CredentialsHTTP • Facebook • https://github.com/IBM-Swift/Kitura-CredentialsFacebook • Google • https://github.com/IBM-Swift/Kitura-CredentialsGoogle • GitHub • https://github.com/IBM-Swift/Kitura-CredentialsGitHub • Twitter • https://github.com/jacobvanorder/Kitura-CredentialsTwitter (Thanks!) 24
  14. Adding a NoSQL backend Apps need persistence – we can

    help you! • Apache Cassandra • https://github.com/IBM-Swift/Kassandra • Redis • https://github.com/IBM-Swift/Kitura-redis • CouchDB • https://github.com/IBM-Swift/Kitura-CouchDB 25
  15. Backend Abstraction with Kuery class T1 { let tableName =

    "t1" let a = Column("a") let b = Column("b") } 26
  16. Kuerying SELECT * FROM t1; let t1 = T1() let

    s = Select(from: t1) s.execute(connection) { queryResult in if let resultSet = queryResult.asResultSet { for title in resultSet.titles { ... } for row in resultSet.rows { for value in row { ... } } } else if let queryError = result.asError { ... } } 27
  17. Kuerying continued SELECT UCASE(a) AS name FROM t1 WHERE b

    >= 0 GROUP BY a HAVING SUM(b) > 3 ORDER BY a DESC; … let s = Select(ucase(t1.a).as("name"), from: t1) .where(t1.b >= 0) .group(by: t1.a) .having(sum(t1.b) > 3) .order(by: .DESC(t1.a)) … 28
  18. 30

  19. 31

  20. 32

  21. 33

  22. 34

  23. 35

  24. 36

  25. 37

  26. 38

  27. 39

  28. 40

  29. 41

  30. 42

  31. 43

  32. 44

  33. 45

  34. 46

  35. 47

  36. Server-Side Swift: The Book 48 • Take your new skills

    to the next level • Step by step tutorials • Written by acclaimed Swift author Paul Hudson @twostraws https://www.hackingwithswift.com