Hooray, open source Swift finally arrived on Linux in December. Let’s see how easy it is to use Swift for your backend and why Swift is a good choice for safe and fast development.
7. jensravens.com / SwiftConf 2016
Jens Ravens
Developer at nerdgeschoss, a mobile first dev agency for sophisticated software. We
help startups and medium businesses to build awesome stuff.
8. jensravens.com / SwiftConf 2016
Jens Ravens
50% iOS / macOS using Swift
50% Web / API using Ruby on Rails
9. jensravens.com / SwiftConf 2016
Jens Ravens
50% iOS / macOS using Swift
50% Web / API using Ruby on Rails
10. jensravens.com / SwiftConf 2016
Jens Ravens
50% iOS / macOS using Swift
50% Web / API using Ruby on Rails
11. jensravens.com / SwiftConf 2016
Jens Ravens
50% iOS / macOS using Swift
50% Web / API using Ruby on Rails
12. jensravens.com / SwiftConf 2016
Jens Ravens
Also I blog about Swift stuff on jensravens.com and organize
the monthly swift.berlin meetup.
14. jensravens.com / SwiftConf 2016
What I am going to talk about…
why Swift on the server might actually be a good idea
15. jensravens.com / SwiftConf 2016
What I am going to talk about…
a short introduction to http, the internet and everything else
why Swift on the server might actually be a good idea
16. jensravens.com / SwiftConf 2016
What I am going to talk about…
a short introduction to http, the internet and everything else
abstracting the server implementation from request handling
why Swift on the server might actually be a good idea
17. jensravens.com / SwiftConf 2016
What I am going to talk about…
a short introduction to http, the internet and everything else
abstracting the server implementation from request handling
Model, View, Controller, Router, Storage
why Swift on the server might actually be a good idea
18. jensravens.com / SwiftConf 2016
What I am going to talk about…
a short introduction to http, the internet and everything else
abstracting the server implementation from request handling
Model, View, Controller, Router, Storage
why Swift on the server might actually be a good idea
the current state of server side Swift
25. jensravens.com / SwiftConf 2016
So server side Swift is screaming fast.
It gives you more bang for the buck.
26. jensravens.com / SwiftConf 2016
So server side Swift is screaming fast.
It gives you more bang for the buck.
But here is a well kept secret…
27. jensravens.com / SwiftConf 2016
So server side Swift is screaming fast.
It gives you more bang for the buck.
But here is a well kept secret…
I just don't care.
28. jensravens.com / SwiftConf 2016
A look at last month’s spendings.
Server Costs
2 %
Office
11 %
Salaries
87 %
45. jensravens.com / SwiftConf 2016
Get a Request, give a Response.
public protocol Request {
var context: [String:AnyObject] { get set }
var method: HTTPMethod { get }
var path: String { get }
var params: [String:String] { get }
var headers: [String:String] { get }
var format: Format { get }
var body: Streamable? { get }
}
46. jensravens.com / SwiftConf 2016
Get a Request, give a Response.
public protocol Response {
var headers: [String: HeaderType] { get }
var code: StatusCode { get }
var content: Streamable { get }
}
public protocol Request {
var context: [String:AnyObject] { get set }
var method: HTTPMethod { get }
var path: String { get }
var params: [String:String] { get }
var headers: [String:String] { get }
var format: Format { get }
var body: Streamable? { get }
}
47. jensravens.com / SwiftConf 2016
Get a Request, give a Response.
public protocol Response {
var headers: [String: HeaderType] { get }
var code: StatusCode { get }
var content: Streamable { get }
}
public protocol Request {
var context: [String:AnyObject] { get set }
var method: HTTPMethod { get }
var path: String { get }
var params: [String:String] { get }
var headers: [String:String] { get }
var format: Format { get }
var body: Streamable? { get }
}
public protocol Streamable {
var stream: Void -> NSData? { get }
}
49. jensravens.com / SwiftConf 2016
But isn’t this the same for every app on every server?
Yep, you’re right.
50. jensravens.com / SwiftConf 2016
Abstracting the server implementation
from request handling
Ruby: Rack.
Every app is just a function transforming request to response.
51. jensravens.com / SwiftConf 2016
Abstracting the server implementation
from request handling
Meet the Open Swift Standards.
https://github.com/open-swift
52. jensravens.com / SwiftConf 2016
Abstracting the server implementation
from request handling
Meet the Open Swift Standards.
https://github.com/open-swift
Open Swift Standard
Server Implementation
Your awesome App
The Internetz
54. jensravens.com / SwiftConf 2016
public typealias Controller = Request throws -> Response
Get a Request, give a Response: the Controller.
55. jensravens.com / SwiftConf 2016
public typealias Controller = Request throws -> Response
let myApp: Controller = { request in
return Response(code: 200, headers: [:], content: "Hello World")
}
Get a Request, give a Response: the Controller.
56. jensravens.com / SwiftConf 2016
Making things pretty: Rendering Views.
public protocol View {
func render() throws -> Streamable
func render() throws -> Response
var contentType: Format { get }
}
57. jensravens.com / SwiftConf 2016
public struct JSONView: View {
public var contentType: Format { return .JSON }
let contents: AnyObject
public init(_ contents: AnyObject) {
self.contents = contents
}
public func render() throws -> Streamable {
return try NSJSONSerialization.dataWithJSONObject(
contents, options: .PrettyPrinted)
}
}
58. jensravens.com / SwiftConf 2016
Models and Persistence: Storage.
struct Post {
let id: Int
var title: String
var content: String
}
59. jensravens.com / SwiftConf 2016
Models and Persistence: Storage.
protocol Repository {
associatedtype Element
func find(index: Int) -> Element
func query(conditions: Predicate) -> [Element]
}
struct Post {
let id: Int
var title: String
var content: String
}
60. jensravens.com / SwiftConf 2016
Models and Persistence: Storage.
protocol Repository {
associatedtype Element
func find(index: Int) -> Element
func query(conditions: Predicate) -> [Element]
}
struct Post {
let id: Int
var title: String
var content: String
}
let post = try postsRepository.find(request.params[“id"])
let attributes = ["id": post.id, "title": post.title]
return JSONView(attributes)
61. jensravens.com / SwiftConf 2016
Where to go next: the Router.
public final class Router {
public func get(path: String, app: Void -> Controller)
public func post(path: String, app: Void -> Controller)
…
public func controller(request: Request) throws -> Response
}
62. jensravens.com / SwiftConf 2016
let myApp: Controller = { request in
json = JSONView(["message":"Hello World #{request.params["id"]}"])
return Response(code: 200, headers: [:], content: try json.render())
}
Where to go next: the Router.
public final class Router {
public func get(path: String, app: Void -> Controller)
public func post(path: String, app: Void -> Controller)
…
public func controller(request: Request) throws -> Response
}
63. jensravens.com / SwiftConf 2016
let myApp: Controller = { request in
json = JSONView(["message":"Hello World #{request.params["id"]}"])
return Response(code: 200, headers: [:], content: try json.render())
}
Where to go next: the Router.
public final class Router {
public func get(path: String, app: Void -> Controller)
public func post(path: String, app: Void -> Controller)
…
public func controller(request: Request) throws -> Response
}
let router = Router()
router.get("messages/:id") { myApp }
serve(router.controller)
74. jensravens.com / SwiftConf 2016
Some Observations.
Routers are also controllers and therefore can be nested.
75. jensravens.com / SwiftConf 2016
Some Observations.
Models don’t inherit or conform to anything.
Routers are also controllers and therefore can be nested.
76. jensravens.com / SwiftConf 2016
Some Observations.
Models don’t inherit or conform to anything.
Controllers can call other controllers (Middleware).
Routers are also controllers and therefore can be nested.
77. jensravens.com / SwiftConf 2016
Some Observations.
Models don’t inherit or conform to anything.
Controllers can call other controllers (Middleware).
Client and server architecture are pretty similar which allows code sharing.
Routers are also controllers and therefore can be nested.
80. jensravens.com / SwiftConf 2016
My personal wishlist.
a framework that comes totally configurable, but batteries included
81. jensravens.com / SwiftConf 2016
My personal wishlist.
be explicit - but don’t be verbose
a framework that comes totally configurable, but batteries included
82. jensravens.com / SwiftConf 2016
My personal wishlist.
be explicit - but don’t be verbose
more projects conforming to Open Swift Standards
a framework that comes totally configurable, but batteries included
83. jensravens.com / SwiftConf 2016
My personal wishlist.
be explicit - but don’t be verbose
more projects conforming to Open Swift Standards
don’t just imitate other frameworks - use Swift’s
modern and type safe features
a framework that comes totally configurable, but batteries included