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.

Fea6d57cccac4021b6c8acbfaa468965?s=128

Benedikt Terhechte

September 15, 2016
Tweet

Transcript

  1. DEVELOPING APP-BACKENDS WITH SWIFT ON THE SERVER

  2. ABOUT ME Benedikt Terhechte Hirundo SourceKittenDaemon CoreValue Indie Developer @terhechte

    appventure.me
  3. 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
  4. SWIFT ON THE SERVER? WHY? There are already tons of

    solutions
  5. GO

  6. GO THREE REASONS

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

    programming -Kyle Jessup Founder, Perfect Framework
  8. 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.
  9. CODE SHARING ➤ Builds upon the first reason ➤ Share

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

    Ruby or Python ➤ Consumes less memory ➤ Strong Types ➤ Modern language features 3.
  11. WEB FRAMEWORKS

  12. WHAT’S A WEB FRAMEWORK ➤ Just like cocoa touch or

    cocoa ➤ Support the development of web applications and web APIs
  13. DB Browser Request Server Framework Code REQUEST / RESPONSE FLOW

    CHART
  14. DB Browser Request Server Framework Code REQUEST / RESPONSE FLOW

    CHART
  15. SWIFT WEB FRAMEWORKS

  16. PERFECT ➤ 8200 GitHub Stars ➤ "Perfect is a complete

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

    works on iOS, macOS, and Ubuntu." ➤ Very modular ➤ Beautiful API
  18. 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
  19. 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)
  20. 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
  21. WEB FRAMEWORK FEATURES

  22. ➤ 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
  23. ➤ Take with grain of salt ➤ Status quo may

    already have changed between my research and this talk
  24. 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
  25. RAW DATABASE SUPPORT Feature MySQL PostgreSQL MongoDB Redis RethinkDB Cassandra

    CouchDB Filemaker Perfect Vapor Kitura Zewo
  26. ORM ➤ Database Abstraction, like Core Data ➤ Spares you

    from writing custom SQL ➤ Vapor
  27. 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
  28. CRUD ➤ Create, Read, Update, Delete ➤ Inherit from a

    class to automatically gain CRUD ➤ Auto-generate routes ➤ /users/:id/delete ➤ Vapor
  29. SESSIONS ➤ Sessions allow associating stateful information with individual visitors

    ➤ Perfect, Vapor, Kitura, Zewo
  30. 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}}
  31. TURING TEMPLATES ➤ Conditionals ➤ Loops ➤ Vapor #if(entering) {

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

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

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

    ➤ Real-time data transfer from / to server ➤ Perfect, Vapor, Zewo
  35. DEPLOYMENT Method Heroku Docker AWS Apache Nginx Bluemix Perfect Vapor

    Kitura Zewo
  36. MISC ➤ File Uploads ➤ JSON Encoding / Decoding ➤

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

    / Build System ➤ Admin Tool ➤ Sitemaps ➤ Hot Reloading of Code ➤ Reloading of HTML / CSS / JS
  38. 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.
  39. PERFORMANCE

  40. 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
  41. TECH EMPOWER BENCHMARK ➤ It compares ~40 frameworks. ➤ It

    has very strict rules ➤ Optimized & tested heavily by lots of users ➤ www.techempower.com/ benchmarks
  42. 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)
  43. THE SWIFT FRAMEWORKS 0 70000 140000 210000 280000 Swift: Vapor

    Swift: Kitura Swift: Perfect Plaintext
  44. 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
  45. 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
  46. 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
  47. Vapor (currently) offers the most features Some code examples of

    common features with Vapor
  48. HELLO WORLD

  49. TYPE-SAFE ROUTING Type-safe routing. This will only be called if

    the `number` parameter is indeed an integer
  50. RESPONSE CONFORMANCE PROTOCOL

  51. RESPONSE CONFORMANCE PROTOCOL

  52. RESPONSE CONFORMANCE PROTOCOL

  53. DEFAULT ARGUMENTS

  54. VALIDATION

  55. MIDDLEWARE

  56. MIDDLEWARE

  57. MIDDLEWARE

  58. AUTHENTICATION

  59. AUTHENTICATION

  60. AUTHENTICATION

  61. QUESTION GAME

  62. None
  63. None
  64. None
  65. SHARING CODE: DEMO

  66. CAN WE USE THIS?

  67. 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
  68. 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
  69. 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
  70. 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.
  71. 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
  72. 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