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

Be Your Own Backend Developer

Be Your Own Backend Developer

Presentation I gave at the Code Mobile conference in Chester.

Abizer Nasir

April 18, 2017
Tweet

More Decks by Abizer Nasir

Other Decks in Programming

Transcript

  1. Be Your Own Backend Developer
    Abizer Nasir ❦ @abizern ❦ abizern.org
    1/37 — CodeMobile 2017

    View Slide

  2. I am not a backend developer
    2/37 — CodeMobile 2017

    View Slide

  3. This is not a swift tutorial
    3/37 — CodeMobile 2017

    View Slide

  4. 4/37 — CodeMobile 2017

    View Slide

  5. Swift is not just an application development
    language
    5/37 — CodeMobile 2017

    View Slide

  6. You can create compiled binaries with the Swift
    Package Manager and run them from the command
    line.
    6/37 — CodeMobile 2017

    View Slide

  7. #!/usr/bin/swift
    7/37 — CodeMobile 2017

    View Slide

  8. Why ||Do I|| Do You Want to Be a Backend
    Developer?
    → It’s fun to add a new skill to the toolbox.
    → Provide real time data during development.
    → Hackdays.
    → Help the current backend team.
    → Become a full stack native app developer.
    8/37 — CodeMobile 2017

    View Slide

  9. Swift on the Server, really means Swift on Linux,
    eventually maybe even Windows.
    9/37 — CodeMobile 2017

    View Slide

  10. Does Swift’s performance match up?
    10/37 — CodeMobile 2017

    View Slide

  11. Low Memory!
    Memory Usage (MB)!
    (lower is better)!
    Swift @ IBM
    http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=spectralnorm!
    11/37 — CodeMobile 2017

    View Slide

  12. Performant Applications!
    Duration (s)!
    (lower is better)!
    Swift @ IBM
    http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=spectralnorm!
    12/37 — CodeMobile 2017

    View Slide

  13. Setting up a Server
    → Create an executable Package with the Swift
    Package Manager.
    → Define the dependencies for the server,
    databases, template engine, logging...
    → Define the routes and specify the port to listen to
    requests on.
    → Build and run the project.
    13/37 — CodeMobile 2017

    View Slide

  14. Profit
    14/37 — CodeMobile 2017

    View Slide

  15. Swift is not the same everywhere.
    #if os(OSX)
    import Darwin
    public let random: (Int) -> Int = { Int(arc4random_uniform(UInt32($0))) }
    #else
    import Glibc
    public let random: (Int) -> Int = {
    while true {
    let x = Glibc.random() % $0
    let y = Glibc.random() % $0
    guard x == y else { return x }
    }
    }
    #endif
    15/37 — CodeMobile 2017

    View Slide

  16. You could run it locally on your Mac, or another
    network attached Mac.
    16/37 — CodeMobile 2017

    View Slide

  17. Set up or get a Linux box, set up Swift use your own
    editor, and terminal set up.
    17/37 — CodeMobile 2017

    View Slide

  18. Docker seems to be the current best practice. Run
    the app in the container, edit the project in Xcode.
    IBM have an almost complete version of Foundation
    and a complete version of Dispatch that matches
    those available on macOS.
    ibmcom/swift-ubuntu
    18/37 — CodeMobile 2017

    View Slide

  19. If you want to see if your Foundation code will run
    on Linux, you could run a REPL inside your docker
    container and try out some code.
    19/37 — CodeMobile 2017

    View Slide

  20. Swift Sandbox
    Playgrounds on the Web
    https://swift.sandbox.bluemix.net/#/repl
    20/37 — CodeMobile 2017

    View Slide

  21. 21/37 — CodeMobile 2017

    View Slide

  22. Frameworks
    There are choices. All open source
    → Kitura
    → Vapor
    → Perfect
    22/37 — CodeMobile 2017

    View Slide

  23. Routing
    This is the simplest part, the syntax varies between
    frameworks but essentially attach code to an
    endpoint.
    23/37 — CodeMobile 2017

    View Slide

  24. Kitura
    import Kitura
    // Create a new router
    let router = Router()
    router.get("/") { request, response, next in
    response.send("Hello, World!")
    next()
    }
    Kitura.run()
    24/37 — CodeMobile 2017

    View Slide

  25. Vapor
    import Vapor
    let drop = Droplet()
    drop.get("/hello") { _ in
    return "Hello Vapor"
    }
    drop.run()
    25/37 — CodeMobile 2017

    View Slide

  26. → Pick one and try it.
    → You can even run Vapor with a Kitura server.
    → There are plugins for almost anything you want to
    do. Security, databases, sockets, templates,
    Logging,
    26/37 — CodeMobile 2017

    View Slide

  27. Deployment
    Kitura applications can be served from IBM’s
    Bluemix. Using a provided app or a command line
    application
    27/37 — CodeMobile 2017

    View Slide

  28. Deployment
    Alternatively, Deploy to Heroku with a custom
    Buildpack.
    $ heroku create --buildpack https://github.com/
    kylef/heroku-buildpack-swift.git
    28/37 — CodeMobile 2017

    View Slide

  29. Summary
    You get to work mainly with. the language and tools
    you’re used to in a reasonably performant
    environment.
    29/37 — CodeMobile 2017

    View Slide

  30. Pick a framework. You’ll mostly be using
    dependencies, so there isn’t much code to rewrite.
    30/37 — CodeMobile 2017

    View Slide

  31. You have greater scope for your own projects, maybe
    even client projects.
    31/37 — CodeMobile 2017

    View Slide

  32. It’s a step towards learning other web technologies.
    32/37 — CodeMobile 2017

    View Slide

  33. References
    Paul Hudson’s Hacking with swift book.
    https://www.hackingwithswift.com/store/server-side-swift
    33/37 — CodeMobile 2017

    View Slide

  34. References
    Ray Wenderlich’s Tutorial site.
    http://raywenderlich.com
    34/37 — CodeMobile 2017

    View Slide

  35. References
    Swift Talks by the Objc.io
    https://talk.objc.io
    35/37 — CodeMobile 2017

    View Slide

  36. References
    Swift Web Weekly
    http://swiftwebweekly.com
    36/37 — CodeMobile 2017

    View Slide

  37. Thank You
    Abizer Nasir ❦ @abizern ❦ abizern.org
    37/37 — CodeMobile 2017

    View Slide