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

NSSpain 2016: Developing App-Backends with Swift on the Server

NSSpain 2016: Developing App-Backends with Swift on the Server

Swift on Linux & FreeBSD will allow app developers to write their backend in Swift while re-using existing application code, such as models or utilities. This talk will give an overview and a concrete example of running Swift on a web server.

Swift on the server, that's a new option now that open source Swift is available for Linux and FreeBSD. Not only will this extend the possibilities and reach of Swift, it will also allow app developers to write their backend in Swift while re-using existing application code, such as models or utilities. This talk will first introduce general server development and highlight key differences to app development. We will then discuss the current state of web frameworks available for Swift and assess their performance. Here, we will also see which frameworks are best for certain use cases. Furthermore we will list restrictions, such as only using Foundation & libdispatch, in order to allow the sharing of our code between Swift and Linux. Finally, a concrete backend REST service will be implemented based on an already existing app / model.

Benedikt Terhechte

September 15, 2016

More Decks by Benedikt Terhechte

Other Decks in Programming


  1. SERVER DEVELOPMENT ➤ Worked many years as a backend developer

    ➤ Kept an active interest in server development afterwards ➤ Developed in ➤ Python + Django ➤ Scala + Scalatra ➤ Clojure ➤ Go + Revel ➤ Lua + Openresty ➤ PHP + Symfony
  2. GO

  3. “There’s a real desire among developers for less fractionalization in

    programming -Kyle Jessup Founder, Perfect Framework
  4. CURRENT FULL STACK SETUP ➤ Ruby on the Server ➤

    Javascript in the Frontend ➤ SQL in the Database ➤ CSS & HTML in the Frontend ➤ Java for the Android solution ➤ Swift for the iOS Solution ➤ Thats 7 Languages! ➤ New devices, new paradigms (VR), more languages ➤ Should strife for less 1.
  5. CODE SHARING ➤ Builds upon the first reason ➤ Share

    code among different targets ➤ Saves time ➤ Less Bugs 2.
  6. SWIFT ➤ Swift is a great language ➤ Faster than

    Ruby or Python ➤ Consumes less memory ➤ Strong Types ➤ Modern language features 3.
  7. WHAT’S A WEB FRAMEWORK ➤ Just like cocoa touch or

    cocoa ➤ Support the development of web applications and web APIs
  8. PERFECT ➤ 8200 GitHub Stars ➤ "Perfect is a complete

    and powerful toolbox, framework, and application server for Linux, iOS, and macOS (OS X)."
  9. VAPOR ➤ "A pure Swift & modular Web Framework that

    works on iOS, macOS, and Ubuntu." ➤ Very modular ➤ Beautiful API
  10. KITURA ➤ "Kitura is a web framework and web server

    that is created for web services written in Swift." ➤ Backed by IBM ➤ IBM is investing heavily into Kitura ➤ Kitura's developers also contribute to Swift in order to improve Linux support
  11. MANY MORE ➤ Swifton: https://github.com/necolt/Swifton (1994 Stars) ➤ Zewo: https://github.com/Zewo/Zewo

    (1215 Stars) ➤ Blackfish: https://github.com/elliottminns/blackfish (924 Stars) ➤ Slimane: https://github.com/noppoMan/Slimane (61 Stars) ➤ Tailor: https://github.com/brownleej/tailor (55 Stars) ➤ Kunugi: https://github.com/novi/Kunugi (36 Stars) ➤ Quark: https://github.com/QuarkX/Quark (31 Stars)
  12. FIRST VERDICT ➤ The major frameworks have around 20.000 GitHub

    Stars together ➤ Ruby on Rails has 31.000 ➤ Huge interest (already) in Swift Server development
  13. ➤ All Swift frameworks support different features ➤ Depending on

    your use case, this may help you decide on a framework ➤ Most frameworks are still kinda in development or 1.0 ➤ Many features are only partially implemented
  14. ➤ Take with grain of salt ➤ Status quo may

    already have changed between my research and this talk
  15. USER AUTHENTICATION ➤ Log a user in, keep a user

    logged in, log him out, access a user ➤ Via email / password or i.e. Google / Twitter / Facebook (OAuth) ➤ Perfect, Vapor
  16. ROUTING ➤ Interpret visitor input URLs ➤ Parse them ➤

    Retrieve parameters ➤ /users/23/posts/144/ comments/12 ➤ user_id: 23, post_id: 144, comment_id: 12 ➤ Perfect, Vapor, Kitura, Zewo
  17. CRUD ➤ Create, Read, Update, Delete ➤ Inherit from a

    class to automatically gain CRUD ➤ Auto-generate routes ➤ /users/:id/delete ➤ Vapor
  18. LOGIC-LESS TEMPLATES ➤ Syntax for search & replace placeholders in

    HTML Files ➤ Templates without the notion of if / else conditions ➤ “Mustache” ➤ Perfect, Vapor, Kitura, Zewo <h1>{{header}}</h1> {{#empty}} <p>The list is empty.</p> {{/empty}}
  19. TURING TEMPLATES ➤ Conditionals ➤ Loops ➤ Vapor #if(entering) {

    Hello, #(friend-name)! } ##else() { Goodbye, #(friend-name)! } #loop(friends, "friend") { <li><b>#(friend.name)</b></li> }
  20. MIDDLEWARE ➤ Custom code which can be injected to run

    before and after each request / response cycle ➤ Examples: ➤ Authentication ➤ Security ➤ Caching ➤ Perfect, Vapor, Kitura, Zewo
  21. LOCALISATION ➤ drop.localization[lang, "welcome", "title"], ➤ Vapor "welcome": { "title":

    "Welcome!", "body": "A web framework" } "welcome": { "title": "¡Bienvenidos!", "body": "Un framework web" }
  22. WEB SOCKETS ➤ “Full-Duplex communication channels over single TCP connection”

    ➤ Real-time data transfer from / to server ➤ Perfect, Vapor, Zewo
  23. MISC ➤ File Uploads ➤ JSON Encoding / Decoding ➤

    File Uploads ➤ Perfect, Vapor, Kitura, Zewo
  24. MISSING FEATURES ➤ Database Migrations ➤ Scaffolding ➤ Asset Pipeline

    / Build System ➤ Admin Tool ➤ Sitemaps ➤ Hot Reloading of Code ➤ Reloading of HTML / CSS / JS
  25. REMARKS ➤ All Frameworks use the Swift Package Manager ➤

    Vapor, Kitura, and Zewo use OpenSwift for compatibility. This means that middleware, requests, responses, and other objects are based on the same protocols and can be interchanged.
  26. BENCHMARKS ARE DIFFICULT ➤ There’re Swift Web Framework benchmark, but

    they were performed on macOS. The system and performance differs from Linux. Swift is better optimised for macOS ➤ Others only compare limited frameworks or lack the knowledge to implement the benchmark test correctly in the comparison languages ➤ One side will always be unhappy about the comparison
  27. TECH EMPOWER BENCHMARK ➤ It compares ~40 frameworks. ➤ It

    has very strict rules ➤ Optimized & tested heavily by lots of users ➤ www.techempower.com/ benchmarks
  28. STATE ➤ I’ve implemented the Techempower Plaintext benchmarks for Perfect,

    Kitura, and Vapor ➤ I’ve then compared Swift Web Frameworks to the competition as tested by Techempower ➤ Keep in mind ➤ Swift for Linux is new. ➤ The frameworks are all in development ➤ I’ve tested this on other hardware than Techempower ($5 digital ocean droplets vs. AWS c3.large) ➤ I ran less tests (only plaintext)
  29. SWIFT VS OTHER “MODERN” LANGUAGES Ruby: rails-unicorn Rust: iron Groovy:

    grails Javascript: express Javascript: nodejs Clojure: http-kit Elixir: Cowboy Swift: Vapor Swift: Kitura Scala: Play2 Swift: Perfect Go: falcore Go: gin Go: Raw 0 250000 500000 750000 1000000 Plaintext
  30. SWIFT VS THE FASTEST Clojure: http-kit Elixir: Cowboy Swift: Vapor

    Swift: Kitura php5 Java: Play2 Scala: Play2 Swift: Perfect Python: bottle Lua: lapis Go: falcore Python: falcon ur/web Go: gin Go: Raw Lua: openresty Java: servlet Java: vertx Java: netty 0 1250000 2500000 3750000 5000000 Plaintext
  31. PHP laravel Erlang: chicagoboss Ruby: rails-unicorn PHP: hhvm Ruby: sinatra-trinidad

    Java: ninja-standalone Rust: iron Python: flask Groovy: grails Javascript: express Javascript: nodejs Clojure: http-kit Elixir: Cowboy Swift: Vapor Swift: Kitura php5 Java: Play2 Scala: Play2 Swift: Perfect 0 75000 150000 225000 300000 Plaintext SWIFT VS COMMON SOLUTIONS
  32. TYPE-SAFE ROUTING Type-safe routing. This will only be called if

    the `number` parameter is indeed an integer
  33. GENERAL ➤ Early, but exciting ➤ Up until Swift 3

    GM, Swift changed a lot ➤ Frameworks had to adopt to these changes ➤ Frameworks are still changing. Not stable yet. ➤ Outdated examples, docs, and tutorials ➤ Xcode's SourceKitD crashes far more often than during iOS development
  34. WHICH FRAMEWORK ➤ If you’re creating a simple API service

    and really care about performance, go with Perfect ➤ Otherwise, Vapor is the best solution ➤ Keep an eye on Kitura, IBM is investing heavily
  35. SHOULD YOU CHOOSE SWIFT? ➤ It depends ➤ On how

    much frontend code you have ➤ On how polyglot your team is ➤ On how much shared code you expect ➤ On how much you love Swift ➤ Expect to refactor
  36. CONSIDERATIONS ➤ No UIKit / AppKit dependency: You can use

    value types, protocols & generics ➤ Test under Linux. There’re still differences even in Foundation. ➤ Prefer abstractions provided by the framework ➤ Learn from the Vapor source code. It is insightful.
  37. DOCUMENTATION ➤ Documentation is lacking for all frameworks. ➤ Possible

    approaches: ➤ Scout GitHub for Sample repos (though oftentimes they're outdated) ➤ Read unit tests ➤ Read Pull Requests for features. Oftentimes, they include basic usage instructions ➤ Read the source
  38. TIPS ➤ Use Swiftenv to manage Swift versions ➤ Watch

    this years WWDC Swift performance talks ➤ If you don't want to install Linux to run things, use Docker ➤ Will post links to Slides & Code on Twitter: @terhechte