New directions for Swift on the server
Ian Partridge
27th January 2017
Beyond Mobile
Slide 2
Slide 2 text
Open Source Swift
2
www.swift.org
www.github.com/apple/swift
Slide 3
Slide 3 text
Open, inclusive development community
3
GitHub Satellite 2016
https://youtu.be/DoQiiE5oNOU?t=9m
Slide 4
Slide 4 text
Language evolution process
https://apple.github.io/swift-evolution/
Slide 5
Slide 5 text
WWDC 2016
5
Slide 6
Slide 6 text
Kitura
6
http://github.com/IBM-Swift/Kitura
• Open source web framework for Swift
on macOS and Linux
• Inspired by Express for Node.js
• Flexible routing
• Pluggable middlewares
• Easy to deploy
• Supports FastCGI, TLS, WebSockets
Slide 7
Slide 7 text
Kitura Simplicity
7
import Kitura
let router = Router()
router.get("/hello") { request, response, next in
response.status(.OK).send("
Swift on Linux
8
Apple Client Deployment Server/Cloud Deployment
Application-Specific Cloud Services
Client Facing App
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 9
Slide 9 text
Swift’s Characteristics
§ Compiled language
– No JIT compiler
§ ARC for memory management
– No tracing GC
§ Modern syntax design
– Enhanced programmer productivity
9
Slide 10
Slide 10 text
Swift Performance
10
Slide 11
Slide 11 text
Swift Memory Usage
11
Slide 12
Slide 12 text
Swift Performance by Memory
12
Slide 13
Slide 13 text
Traditional Backends
13
Server-side API
Web
iOS
JDBC SOAP
REST
Slide 14
Slide 14 text
Traditional Backends
14
Server-side API
Web
iOS
JDBC SOAP
REST
Slide 15
Slide 15 text
Backend-for-Frontend
15
15
Mobile BFF
Web
iOS
Web BFF
Slide 16
Slide 16 text
Backend-for-Frontend
16
16
Mobile BFF
Web
iOS
Web BFF
Slide 17
Slide 17 text
Backend-for-Frontend
17
17
Mobile BFF
Web
iOS
Web BFF
Slide 18
Slide 18 text
Enabling rapid prototyping
§ Rapid time to value is key for server-side Swift
§ Take inspiration from Ruby on Rails
§ Enable scaffolding via code generation
– Build on Yeoman
18
Slide 19
Slide 19 text
Scaffolding to Hello World
$ npm install -g yo
$ npm install –g git+https://github.com/IBM-Swift/generator-swiftserver
$ yo swiftserver
? What's the name of your application? testapp
? Enter the name of the directory to contain the project: testapp
? Select the data store memory (for development purposes)
create config.json
create Package.swift
create Sources/testapp/main.swift
create Sources/Generated/ApplicationConfiguration.swift
create manifest.yml
create definitions/testapp.yaml
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Compile Swift Module 'GeneratedSwiftServer' (10 sources)
Compile Swift Module 'GeneratedSwiftServer_CloudantStore' (1 sources)
Compile Swift Module 'Generated' (1 sources)
Compile Swift Module 'testapp' (1 sources)
swift build command completed
Next steps:
Run the app
$ .build/debug/testapp
19
Slide 20
Slide 20 text
Defining Models
$ yo swiftserver:model
? Enter the model name: book
? Custom plural form (used to build REST URL): books
Let's add some book properties now.
Enter an empty property name when done.
? Enter the property name: title
? Property type: string
? Required? Yes
? Default? No
? Enter the property name: author
? Property type: string
? Required? Yes
? Default? No
? Enter the property name:
create models/book.json
create Sources/Generated/Book.swift
Compile Swift Module 'Generated' (2 sources)
Compile Swift Module 'testapp' (1 sources)
swift build command completed
20
Slide 21
Slide 21 text
Defining Models
$ ./.build/debug/testapp
VERBOSE: init(mergeParameters:) Router.swift line 62 - Router initialized
INFO: init(projectRoot:) Application.swift line 87 - Defining routes for book
INFO: init(projectRoot:) Application.swift line 89 - Defining DELETE /api/books
INFO: init(projectRoot:) Application.swift line 108 - Defining GET /api/books
INFO: init(projectRoot:) Application.swift line 122 - Defining GET /api/books/:id
INFO: init(projectRoot:) Application.swift line 158 - Defining POST /api/books
INFO: init(projectRoot:) Application.swift line 203 - Defining PUT /api/books/:id
INFO: init(projectRoot:) Application.swift line 264 - Defining PATCH /api/books/:id
INFO: init(projectRoot:) Application.swift line 324 - Defining DELETE /api/books/:id
VERBOSE: run() Kitura.swift line 71 - Starting Kitura framework...
VERBOSE: run() Kitura.swift line 73 - Starting an HTTP Server on port 80...
INFO: listen(on:) HTTPServer.swift line 72 - Listening on port 80
21
Slide 22
Slide 22 text
Deploying to the Cloud
22
github.com/IBM-Swift/swift-ubuntu-docker github.com/IBM-Swift/swift-buildpack
CaaS PaaS
$ docker pull ibmcom/swift-ubuntu:latest $ cf push
Slide 23
Slide 23 text
Monitoring
23
Slide 24
Slide 24 text
Swift on Linux – Batteries not included
§ Building Kitura was harder than it should have been
§ Core infrastructure missing
– Sockets
– Security
§ Unsafe code required to interface with C libraries
§ Noone else should share our pain…
24
Slide 25
Slide 25 text
Swift.org Server APIs workgroup
§ Official swift.org project
§ Working to create reference APIs for low-level server functionality
– Adhering to Swift API design guidelines
§ Focus areas
– Networking
– Security
– Web protocols
25
Slide 26
Slide 26 text
Swift.org Server APIs workgroup
26
§ Easy to get involved
Slide 27
Slide 27 text
Future Directions for Swift
§ Reflection / dynamism
– Currently lacking
– Building a full ORM is hard
§ Concurrency
– Coming in Swift 5?
– Actor model?
§ Error handling
– Force unwrap is game over
– Servers need more flexibility
27