Slide 1

Slide 1 text

Going Server-Side with Swift Ian Partridge 1st June 2017

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

3 Today • Open source Swift • Swift on the Server • Kitura web framework • Emerging architecture patterns • Deploying Swift to the cloud

Slide 4

Slide 4 text

Open Source Swift 4 www.swift.org www.github.com/apple/swift

Slide 5

Slide 5 text

Open, inclusive development community 5 GitHub Satellite 2016 https://youtu.be/DoQiiE5oNOU?t=9m

Slide 6

Slide 6 text

Building on the shoulders of giants 6

Slide 7

Slide 7 text

Language evolution process https://apple.github.io/swift-evolution/

Slide 8

Slide 8 text

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/

Slide 9

Slide 9 text

WWDC 2016 4

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Growing server-side ecosystem 11 https://medium.com/@rymcol/current-features-benefits-of-the-top-server-side-swift-frameworks-b15b4f2d7bc3

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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)

Slide 15

Slide 15 text

Why Swift on the server? • Runtime characteristics • Performance – Compiled, no JIT • Memory usage – ARC, not a tracing GC • Developer experience • Isomorphic Development • Fun! 15

Slide 16

Slide 16 text

Swift Performance

Slide 17

Slide 17 text

Swift Memory Usage

Slide 18

Slide 18 text

Swift Performance by Memory

Slide 19

Slide 19 text

Why does this matter? GB/hr 19

Slide 20

Slide 20 text

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:

Slide 21

Slide 21 text

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):

Slide 22

Slide 22 text

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("

Hello, World!

").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

Slide 23

Slide 23 text

Adding features 23 • Templating • Mustache or Stencil • Static file serving • Custom middlewares • Implemented via a simple protocol • TLS • WebSockets

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Backend Abstraction with Kuery class T1 { let tableName = "t1" let a = Column("a") let b = Column("b") } 26

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Future Work • SMTP for email • Socket.io for realtime • SOAP for… 29

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

31

Slide 32

Slide 32 text

32

Slide 33

Slide 33 text

33

Slide 34

Slide 34 text

34

Slide 35

Slide 35 text

35

Slide 36

Slide 36 text

36

Slide 37

Slide 37 text

37

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

39

Slide 40

Slide 40 text

40

Slide 41

Slide 41 text

41

Slide 42

Slide 42 text

42

Slide 43

Slide 43 text

43

Slide 44

Slide 44 text

44

Slide 45

Slide 45 text

45

Slide 46

Slide 46 text

46

Slide 47

Slide 47 text

47

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Find out more 49 https://developer.ibm.com/swift/ The Swift@IBM devCenter Join IBM Swift Engineering and learn the latest news