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

Swift on the Server

Swift on the Server

Talk about Swift on the Server for Google Dev Fest Düsseldorf 2016

Benedikt Terhechte

October 23, 2016
Tweet

More Decks by Benedikt Terhechte

Other Decks in Programming

Transcript

  1. DÜSSELDORF

    View Slide

  2. SWIFT? ON THE SERVER?

    View Slide

  3. ABOUT ME
    Benedikt Terhechte
    Indie Developer
    macOS & iOS
    @terhechte
    appventure.me

    View Slide

  4. SERVER DEVELOPMENT
    ➤ Worked many years as a backend
    developer
    ➤ Kept an active interest in server
    development afterwards
    ➤ Developed in / for
    ➤ Google App Engine
    ➤ Python + Django
    ➤ Scala + Scalatra
    ➤ Clojure
    ➤ Go + Revel
    ➤ Lua + Openresty
    ➤ PHP + Symfony

    View Slide

  5. SWIFT?

    View Slide

  6. View Slide

  7. HISTORY
    ➤ Original Release (closed source) June 2014
    ➤ Open Source (Apache 2.0) since December 2015
    ➤ Current Version 3.0.1
    ➤ New language for Apple’s complete ecosystem
    ➤ Spearheaded by Chris Lattner (LLVM, Clang)

    View Slide

  8. TARGETS
    ➤ Officially Fully Supported
    ➤ iOS, macOS, watchOS, tvOS
    ➤ Ubuntu 14.04, Ubuntu 15.10, Ubuntu 16.04
    ➤ Unofficially Fully Supported
    ➤ Debian, Arch Linux, Fedora, CentOS
    ➤ FreeBSD 11.0
    ➤ Partial Support
    ➤ Android
    ➤ Windows

    View Slide

  9. SWIFT, THE LANGUAGE

    View Slide

  10. MODERN FEATURES
    ➤ Value Types (Structs, Strings, Numbers, Arrays, Dictionaries)
    ➤ Reference Types (Classes)
    ➤ Optionals & Syntactic Sugar
    ➤ Strong Types & Generics
    ➤ Protocols (“Interfaces”) & Protocol Extensions (“Traits”)
    ➤ Composition & Inheritance
    ➤ Closures / Functions are types
    ➤ Pattern Matching
    ➤ Tuples

    View Slide

  11. MODERN FEATURES
    ➤ Automatic Retain Counting (No GC)
    ➤ Safety by default
    ➤ Algebraic Data Types (Enums with associated data)
    ➤ Full Unicode Support
    ➤ Error handling via Non-Unwinding Exceptions
    ➤ Functional Programming Constructs
    ➤ Comes with a REPL (Commandline) & Playgrounds (Xcode)
    ➤ Static & Dynamic Dispatch

    View Slide

  12. INTRODUCTION

    View Slide

  13. INTRODUCTION

    View Slide

  14. INTRODUCTION

    View Slide

  15. INTRODUCTION

    View Slide

  16. INTRODUCTION

    View Slide

  17. INTRODUCTION

    View Slide

  18. View Slide

  19. View Slide

  20. OPTIONALS

    View Slide

  21. GENERICS

    View Slide

  22. GENERICS

    View Slide

  23. PROTOCOL ORIENTED PROGRAMMING

    View Slide

  24. PROTOCOL ORIENTED PROGRAMMING

    View Slide

  25. PROTOCOL ORIENTED PROGRAMMING

    View Slide

  26. PROTOCOL ORIENTED PROGRAMMING

    View Slide

  27. ENUMS & PATTERNS

    View Slide

  28. ENUMS & PATTERNS

    View Slide

  29. ERROR HANDLING

    View Slide

  30. ERROR HANDLING

    View Slide

  31. FUTURE FEATURES (I.E. ON AGENDA FOR SWIFT 4 OR 5)
    ➤ Coroutines / CSP
    ➤ Optional Strong Memory Lifetimes like Rust
    ➤ Extended Generics Support (Existentials)
    ➤ Performance Improvements
    ➤ Reflection & Dynamic Features

    View Slide

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

    View Slide

  33. GO

    View Slide

  34. GO
    THREE REASONS

    View Slide

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

    View Slide

  36. 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 stride for less
    1.

    View Slide

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

    View Slide

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

    View Slide

  39. SWIFT WEB FRAMEWORKS

    View Slide

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

    View Slide

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

    View Slide

  42. KITURA
    ➤ 5000 Github Stars
    ➤ "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

  43. 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

  44. 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

  45. WEB FRAMEWORK FEATURES

    View Slide

  46. ➤ 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

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

    View Slide

  48. 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

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

    View Slide

  50. ORM
    ➤ Spares you from writing
    custom SQL
    ➤ Vapor

    View Slide

  51. 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

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

    View Slide

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

    View Slide

  54. 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

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

    View Slide

  56. 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

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

    View Slide

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

    View Slide

  59. DEPLOYMENT
    Method
    Heroku
    Docker
    AWS
    Apache
    Nginx
    Blue mix
    DigitalOcean
    Perfect Vapor Kitura Zewo

    View Slide

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

    View Slide

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

    View Slide

  62. PERFORMANCE

    View Slide

  63. 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

  64. 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

  65. 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 droplet vs. 40 Core 32GB Ram Monster)
    ➤ I ran less tests (only plaintext)

    View Slide

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

    View Slide

  67. 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

  68. 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

  69. 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

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

    View Slide

  71. HELLO WORLD

    View Slide

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

    View Slide

  73. RESPONSE CONFORMANCE PROTOCOL

    View Slide

  74. RESPONSE CONFORMANCE PROTOCOL

    View Slide

  75. RESPONSE CONFORMANCE PROTOCOL

    View Slide

  76. DEFAULT ARGUMENTS

    View Slide

  77. VALIDATION

    View Slide

  78. MIDDLEWARE

    View Slide

  79. MIDDLEWARE

    View Slide

  80. MIDDLEWARE

    View Slide

  81. AUTHENTICATION

    View Slide

  82. AUTHENTICATION

    View Slide

  83. AUTHENTICATION

    View Slide

  84. AUTHENTICATION

    View Slide

  85. CHUNKED DATA

    View Slide

  86. WEB SOCKETS

    View Slide

  87. OUTLOOK

    View Slide

  88. SHARE ALL THE CODE??

    View Slide

  89. SHARE ALL THE CODE??
    NO

    View Slide

  90. HOWEVER

    View Slide

  91. SWIFT ON ANDROID
    ➤ 2 Facebook People and several other open source contributors
    ➤ Slowly evolving
    ➤ Uses JNI
    ➤ Alternative to C++
    ➤ No auto-generated wrapper code yet
    ➤ Painful to use
    ➤ Give it another year

    View Slide

  92. SWIFT ON THE SERVER
    ➤ 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

    View Slide

  93. SECURITY
    ➤ Don’t use it for anything vaguely important

    View Slide

  94. 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
    ➤ Whether the future direction of Swift aligns with business

    View Slide

  95. 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

  96. SOME ALTERNATIVES
    ➤ Kotlin (targets JVM & Javascript)
    ➤ Scala (targets JVM, Mobile / Native & Javascript)
    ➤ Javascript + Node + React / React Native
    ➤ C# (targets CLR, Mobile Devices & Desktops)
    ➤ C++ 17 (targets Server, Mobile & Emscripten / Javascript)
    ➤ Rust (targets Server, Mobile & Emscripten / Javascript)
    ➤ OCaml (targets Server, Mobile & Javascript)
    ➤ Java + bugVM (RoboVM) (targets Mobile, native macOS /
    Ubuntu, JVM, Javascript)

    View Slide

  97. HIRUNDO - SWIFT DISCUSSION & MORE
    STYLEMAC.COM/HIRUNDO

    View Slide

  98. THANKS!
    ➤ Will post links to Slides & Code on Twitter: @terhechte

    View Slide