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 / AppBuilders 2017
Jens Ravens
Developer at nerdgeschoss, a mobile first dev agency for sophisticated software. We
help startups and medium businesses to build awesome stuff.
9. jensravens.com / AppBuilders 2017
Jens Ravens
Also I blog about Swift stuff on jensravens.com and organize
the monthly swift.berlin meetup.
10. jensravens.com / AppBuilders 2017
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
11. jensravens.com / AppBuilders 2017
Why Swift on the server might actually
be a good idea.
🚀 🤓
speed developer happiness
13. jensravens.com / AppBuilders 2017
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.
20. jensravens.com / AppBuilders 2017
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 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 }
}
22. jensravens.com / AppBuilders 2017
Abstracting the server implementation
from request handling
Ruby: Rack.
Every app is just a function transforming request to response.
23. jensravens.com / AppBuilders 2017
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
and the Swift Server APIs.
https://swift.org/server-apis
24. jensravens.com / AppBuilders 2017
Structuring Web Applications in Swift with MVC-RS
*
*Term shamelessly copied from a presentation
by Greg Lhotellier at SwiftConf 2016
25. jensravens.com / AppBuilders 2017
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.
26. jensravens.com / AppBuilders 2017
Making things pretty: Rendering Views.
public protocol View {
func render() throws -> Streamable
func render() throws -> Response
var contentType: Format { get }
}
27. jensravens.com / AppBuilders 2017
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)
}
}
28. jensravens.com / AppBuilders 2017
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)
29. jensravens.com / AppBuilders 2017
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)
30. jensravens.com / AppBuilders 2017
MVC-RS in Review
Database External Webservice
Repository RepositoryRepository
Controller Controller
Model View Model View
Router
31. jensravens.com / AppBuilders 2017
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.
35. jensravens.com / AppBuilders 2017
My personal wishlist.
be explicit - but don’t be verbose
more projects conforming to Open Swift Standards Server APIs
don’t just imitate other frameworks - use Swift’s
modern and type safe features
a framework that comes totally configurable, but batteries included