Share
Embed
Transcript
28 slides
-
New directions for Swift on the server Ian Partridge 27th
January 2017 Beyond Mobile -
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 -
Kitura Simplicity 7 import Kitura let router = Router() router.get("/hello")
{ request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").end() } Kitura.addHTTPServer(onPort: 80, with: router) Kitura.run() -
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) -
Swift’s Characteristics § Compiled language – No JIT compiler §
ARC for memory management – No tracing GC § Modern syntax design – Enhanced programmer productivity 9 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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