Be Your Own Backend Developer
Abizer Nasir ❦ @abizern ❦ abizern.org
1/37 — CodeMobile 2017
Slide 2
Slide 2 text
I am not a backend developer
2/37 — CodeMobile 2017
Slide 3
Slide 3 text
This is not a swift tutorial
3/37 — CodeMobile 2017
Slide 4
Slide 4 text
4/37 — CodeMobile 2017
Slide 5
Slide 5 text
Swift is not just an application development
language
5/37 — CodeMobile 2017
Slide 6
Slide 6 text
You can create compiled binaries with the Swift
Package Manager and run them from the command
line.
6/37 — CodeMobile 2017
Slide 7
Slide 7 text
#!/usr/bin/swift
7/37 — CodeMobile 2017
Slide 8
Slide 8 text
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
Slide 9
Slide 9 text
Swift on the Server, really means Swift on Linux,
eventually maybe even Windows.
9/37 — CodeMobile 2017
Slide 10
Slide 10 text
Does Swift’s performance match up?
10/37 — CodeMobile 2017
Slide 11
Slide 11 text
Low Memory!
Memory Usage (MB)!
(lower is better)!
Swift @ IBM
http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=spectralnorm!
11/37 — CodeMobile 2017
Slide 12
Slide 12 text
Performant Applications!
Duration (s)!
(lower is better)!
Swift @ IBM
http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=spectralnorm!
12/37 — CodeMobile 2017
Slide 13
Slide 13 text
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
Slide 14
Slide 14 text
Profit
14/37 — CodeMobile 2017
Slide 15
Slide 15 text
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
Slide 16
Slide 16 text
You could run it locally on your Mac, or another
network attached Mac.
16/37 — CodeMobile 2017
Slide 17
Slide 17 text
Set up or get a Linux box, set up Swift use your own
editor, and terminal set up.
17/37 — CodeMobile 2017
Slide 18
Slide 18 text
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
Slide 19
Slide 19 text
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
Slide 20
Slide 20 text
Swift Sandbox
Playgrounds on the Web
https://swift.sandbox.bluemix.net/#/repl
20/37 — CodeMobile 2017
Slide 21
Slide 21 text
21/37 — CodeMobile 2017
Slide 22
Slide 22 text
Frameworks
There are choices. All open source
→ Kitura
→ Vapor
→ Perfect
22/37 — CodeMobile 2017
Slide 23
Slide 23 text
Routing
This is the simplest part, the syntax varies between
frameworks but essentially attach code to an
endpoint.
23/37 — CodeMobile 2017
Slide 24
Slide 24 text
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
Slide 25
Slide 25 text
Vapor
import Vapor
let drop = Droplet()
drop.get("/hello") { _ in
return "Hello Vapor"
}
drop.run()
25/37 — CodeMobile 2017
Slide 26
Slide 26 text
→ 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
Slide 27
Slide 27 text
Deployment
Kitura applications can be served from IBM’s
Bluemix. Using a provided app or a command line
application
27/37 — CodeMobile 2017
Slide 28
Slide 28 text
Deployment
Alternatively, Deploy to Heroku with a custom
Buildpack.
$ heroku create --buildpack https://github.com/
kylef/heroku-buildpack-swift.git
28/37 — CodeMobile 2017
Slide 29
Slide 29 text
Summary
You get to work mainly with. the language and tools
you’re used to in a reasonably performant
environment.
29/37 — CodeMobile 2017
Slide 30
Slide 30 text
Pick a framework. You’ll mostly be using
dependencies, so there isn’t much code to rewrite.
30/37 — CodeMobile 2017
Slide 31
Slide 31 text
You have greater scope for your own projects, maybe
even client projects.
31/37 — CodeMobile 2017
Slide 32
Slide 32 text
It’s a step towards learning other web technologies.
32/37 — CodeMobile 2017
Slide 33
Slide 33 text
References
Paul Hudson’s Hacking with swift book.
https://www.hackingwithswift.com/store/server-side-swift
33/37 — CodeMobile 2017
Slide 34
Slide 34 text
References
Ray Wenderlich’s Tutorial site.
http://raywenderlich.com
34/37 — CodeMobile 2017
Slide 35
Slide 35 text
References
Swift Talks by the Objc.io
https://talk.objc.io
35/37 — CodeMobile 2017
Slide 36
Slide 36 text
References
Swift Web Weekly
http://swiftwebweekly.com
36/37 — CodeMobile 2017