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
Tweet

More Decks by Benedikt Terhechte

Other Decks in Programming

Transcript

  1. DEVELOPING APP-BACKENDS WITH
    SWIFT ON THE SERVER

    View Slide

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

    View Slide

  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

    View Slide

  4. SWIFT ON THE SERVER? WHY?
    There are already tons of solutions

    View Slide

  5. GO

    View Slide

  6. GO
    THREE REASONS

    View Slide

  7. “There’s a real desire among
    developers for less fractionalization in
    programming
    -Kyle Jessup
    Founder, Perfect Framework

    View Slide

  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.

    View Slide

  9. CODE SHARING
    ➤ Builds upon the first reason
    ➤ Share code among different
    targets
    ➤ Saves time
    ➤ Less Bugs
    2.

    View Slide

  10. SWIFT
    ➤ Swift is a great language
    ➤ Faster than Ruby or Python
    ➤ Consumes less memory
    ➤ Strong Types
    ➤ Modern language features
    3.

    View Slide

  11. WEB FRAMEWORKS

    View Slide

  12. WHAT’S A WEB FRAMEWORK
    ➤ Just like cocoa touch or cocoa
    ➤ Support the development of web applications and web APIs

    View Slide

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

    View Slide

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

    View Slide

  15. SWIFT WEB FRAMEWORKS

    View Slide

  16. PERFECT
    ➤ 8200 GitHub Stars
    ➤ "Perfect is a complete and
    powerful toolbox, framework,
    and application server for
    Linux, iOS, and macOS (OS
    X)."

    View Slide

  17. VAPOR
    ➤ "A pure Swift & modular Web
    Framework that works on iOS,
    macOS, and Ubuntu."
    ➤ Very modular
    ➤ Beautiful API

    View Slide

  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

    View Slide

  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)

    View Slide

  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

    View Slide

  21. WEB FRAMEWORK FEATURES

    View Slide

  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

    View Slide

  23. ➤ Take with grain of salt
    ➤ Status quo may already have changed between my research
    and this talk

    View Slide

  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

    View Slide

  25. RAW DATABASE SUPPORT
    Feature
    MySQL
    PostgreSQL
    MongoDB
    Redis
    RethinkDB
    Cassandra
    CouchDB
    Filemaker
    Perfect Vapor Kitura Zewo

    View Slide

  26. ORM
    ➤ Database Abstraction, like
    Core Data
    ➤ Spares you from writing
    custom SQL
    ➤ Vapor

    View Slide

  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

    View Slide

  28. CRUD
    ➤ Create, Read, Update, Delete
    ➤ Inherit from a class to
    automatically gain CRUD
    ➤ Auto-generate routes
    ➤ /users/:id/delete
    ➤ Vapor

    View Slide

  29. SESSIONS
    ➤ Sessions allow associating
    stateful information with
    individual visitors
    ➤ Perfect, Vapor, Kitura, Zewo

    View Slide

  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
    {{header}}
    {{#empty}}
    The list is empty.
    {{/empty}}

    View Slide

  31. TURING TEMPLATES
    ➤ Conditionals
    ➤ Loops
    ➤ Vapor
    #if(entering) {
    Hello, #(friend-name)!
    }
    ##else() {
    Goodbye, #(friend-name)!
    }
    #loop(friends, "friend") {
    #(friend.name)
    }

    View Slide

  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

    View Slide

  33. LOCALISATION
    ➤ drop.localization[lang,
    "welcome", "title"],
    ➤ Vapor
    "welcome": {
    "title": "Welcome!",
    "body": "A web framework"
    }
    "welcome": {
    "title": "¡Bienvenidos!",
    "body": "Un framework web"
    }

    View Slide

  34. WEB SOCKETS
    ➤ “Full-Duplex communication
    channels over single TCP
    connection”
    ➤ Real-time data transfer from /
    to server
    ➤ Perfect, Vapor, Zewo

    View Slide

  35. DEPLOYMENT
    Method
    Heroku
    Docker
    AWS
    Apache
    Nginx
    Bluemix
    Perfect Vapor Kitura Zewo

    View Slide

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

    View Slide

  37. MISSING FEATURES
    ➤ Database Migrations
    ➤ Scaffolding
    ➤ Asset Pipeline / Build System
    ➤ Admin Tool
    ➤ Sitemaps
    ➤ Hot Reloading of Code
    ➤ Reloading of HTML / CSS / JS

    View Slide

  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.

    View Slide

  39. PERFORMANCE

    View Slide

  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

    View Slide

  41. TECH EMPOWER BENCHMARK
    ➤ It compares ~40 frameworks.
    ➤ It has very strict rules
    ➤ Optimized & tested heavily by
    lots of users
    ➤ www.techempower.com/
    benchmarks

    View Slide

  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)

    View Slide

  43. THE SWIFT FRAMEWORKS
    0
    70000
    140000
    210000
    280000
    Swift: Vapor Swift: Kitura Swift: Perfect
    Plaintext

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  47. Vapor (currently) offers the most features
    Some code examples of common features with Vapor

    View Slide

  48. HELLO WORLD

    View Slide

  49. TYPE-SAFE ROUTING
    Type-safe routing. This will only be called if the `number`
    parameter is indeed an integer

    View Slide

  50. RESPONSE CONFORMANCE PROTOCOL

    View Slide

  51. RESPONSE CONFORMANCE PROTOCOL

    View Slide

  52. RESPONSE CONFORMANCE PROTOCOL

    View Slide

  53. DEFAULT ARGUMENTS

    View Slide

  54. VALIDATION

    View Slide

  55. MIDDLEWARE

    View Slide

  56. MIDDLEWARE

    View Slide

  57. MIDDLEWARE

    View Slide

  58. AUTHENTICATION

    View Slide

  59. AUTHENTICATION

    View Slide

  60. AUTHENTICATION

    View Slide

  61. QUESTION GAME

    View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. SHARING CODE: DEMO

    View Slide

  66. CAN WE USE THIS?

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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.

    View Slide

  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

    View Slide

  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

    View Slide