SlideShare a Scribd company logo
The Joy Of Server Side
Swift Development
In 1943 Konrad Zuse invented
Plankalkül
1940s
9 Programming languages
1950s
...
- Fortran(concept)
- Fortran II
- ALGOL 58(IAL)
- LISP(implementation)
...
1950s
58 Programming languages
1960s
...
- ALGOL 60
- COBOL 61(implementation)
- FORTRAN IV
- APL(concept)
- Simula(concept) - SNOBOL - CPL - SNOBOL3
- ALGOL 68(concept)
- BASIC
...
1960s
113 Programming languages
1970s
...
- Pascal
- Smalltalk
- C
- Prolog
- Structured Query language ( SQL)
- Bourne Shell(sh)
- Modula-2
- AWK
...
1970s
170 Programming languages
1980s
...
- Ada 80(MIL-STD-1815)
- GW-BASIC
- Turbo Pascal
- Objective-C
- C++
- Eiffel
- Erlang
...
1980s
231 Programming languages
1990s
...
- AMOS BASIC
- Haskell
- Python
- Visual Basic
- Brainfuck
- Borland Delphi
- Java
- PHP
- Ruby
- JavaScript
- Standard C++
...
1990s
292 Programming languages
2000s
...
- ActionScript
- C#
- D
- Scala
- Groovy
- F#
- Clojure
- Go
...
2000s
339 Programming languages
2010s
...
- Rust
- Elm
- Kotlin
- Elixir
- Hack
- C++14
...
2010s
354 Programming languages
and then...
The Joy  of Server Side Swift Development
355
Do we really need another
programming language?
Who Am I?
@giordanoscalzo
https://github.com/gscalzo
A developer
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
After one year
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
For Real!
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Officially, only Linux is
Supported
Being Open-Source...
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Server side development
without a web framework?
We have a few...
Perfect: github.github.com/PerfectlySoft/Perfect (8829)
Vapor: github.com/vapor/vapor (6763)
Kitura: github.com/IBM-Swift/Kitura (4578)
Swifton: github.com/necolt/Swifton (2016)
Zewo: github.com/Zewo/Zewo (1358)
Blackfish: github.com/elliottminns/blackfish (932)
Slimane: github.com/noppoMan/Slimane (61)
Tailor: github.com/brownleej/tailor (55)
Kunugi: github.com/novi/Kunugi (35)
Quark: github.com/QuarkX/Quark (33)
The Joy  of Server Side Swift Development
"Perfect is a complete and
powerful toolbox,
framework, and application
server for Linux, iOS, and
macOS"
They got money
(Even without React.js!)
The Joy  of Server Side Swift Development
Open-source-ish, but
enterprise-ish...
The Joy  of Server Side Swift Development
Sean Stephens
Chief Executive Officer
let server = HTTPServer()
var routes = Routes()
routes.add(method: .get, uri: "/", handler: {
request, response in
response.setHeader(.contentType, value: "text/html")
response.appendBody(string:
"<html><title>Hello, world!</title><body>Hello, world!</body></html>")
response.completed()
}
)
server.addRoutes(routes)
server.serverPort = 8181
server.documentRoot = "./webroot"
configureServer(server)
do {
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: (err) (msg)")
}
The Joy  of Server Side Swift Development
4 Laravel inspired
4 Easy to setup
4 Fantastic Command Line Tool
The Joy  of Server Side Swift Development
let drop = Droplet()
drop.get("/") { request in
return try drop.view.make("welcome.html")
}
drop.get("plaintext") { request in
return "Hello, World!"
}
drop.serve()
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
4 Backed by IBM
4 Express.js-like
4 Perfectly integrated in IBM cloud solution BlueMix
The Joy  of Server Side Swift Development
import Kitura
let router = Router()
router.get("/") {
request, response, next in
response.send("Hello, World!")
next()
}
Kitura.addHTTPServer(onPort: 8080, with: router)
Kitura.run()
Let's talk now of...
Performance!
Benchmarks are difficult
The Joy  of Server Side Swift Development
and useless...
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Swift Frameworks
Benchmarks
The Joy  of Server Side Swift Development
Fair Enough
Let's do some code
You can write Basic in
Swift...
You can write Haskell-ish
in Swift...
The sense of Swift
An Isomorphic Swift App!
A Universal Swift App!
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Introducing
Mr Drizzle!
An hyper local weather app
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
MrDrizzle Server Requirements:
4 Convert from Wunderground Models to MrDrizzle
Model
4 Refresh cache every hour
MrDrizzle Server
The Joy  of Server Side Swift Development
mkdir MrDrizzleServer
cd MrDrizzleServer
swift package init --type executable
The Joy  of Server Side Swift Development
Swift Package Manager
import PackageDescription
let package = Package(
name: "MrDrizzleServer",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 0)
]
)
Let's make it run
// main.swift
import Foundation
import HTTP
import Vapor
let wheaterUndergroundKey = "KEY"
let wheaterUndergroundDomain = "api.wunderground.com"
let drop = Droplet()
drop.get("/hello") { _ in
return "Hello World"
}
drop.run()
$ swift build
> Cloning https://github.com/vapor/vapor.git
> HEAD is now at 453f7cf Merge pull request #628
> Resolved version: 1.0.3
> Cloning https://github.com/vapor/crypto.git
> HEAD is now at 0aaa68b Merge pull request #15
> Resolved version: 1.0.1
> Cloning https://github.com/vapor/core.git
....
....
> Compile Swift Module 'TypeSafeRouting' (3 sources)
> Compile Swift Module 'Vapor' (83 sources)
> Compile Swift Module 'MrDrizzleServer' (1 sources)
> Linking ./.build/debug/MrDrizzleServer
$ .build/debug/MrDrizzleServer
> Could not load localization files
> No cipher key was set, using blank key.
> Chacha20 cipher requires an initialization
> No command supplied, defaulting to serve
> No preparations.
> No servers.json configuration found.
> Starting server at 0.0.0.0:8080
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
$ swift package generate-xcodeproj
> generated: ./MrDrizzleServer.xcodeproj
The Joy  of Server Side Swift Development
Hello World for dummies
This version:
drop.get("/hello") { _ in
return "Hello World"
}
Is equivalent to:
func handler(request: Request) throws -> ResponseRepresentable {
return Response(status: .ok, body: "Hello, World")
}
drop.get("/hello", handler: handler)
because:
extension Swift.String: ResponseRepresentable {
public func makeResponse() -> Response {
return Response(body: self.bytes)
}
}
Go back to
MrDrizzleServer
The list of the stations:
struct StationInfo {
let name: String
let stationId: String
let lat: Float
let lon: Float
}
let stationsInfo = [
StationInfo(name: "Chelsea",
stationId: "ILONDON330",
lat: 51.479633, lon:-0.180277),
StationInfo(name: "Westminster",
stationId: "ILONDON120",
lat: 51.65343, lon:-0.183732)
//...
]
Let's define a schema for
our models
Model schema in pseudocode
object Stations {
Array of Station stations
}
object Station {
string id
float lat
float lon
string name
Array of HourlyMeasure measures
}
object HourlyMeasure {
int32 hour
float temp_f
float temp_c
}
syntax = "proto3";
message Stations {
repeated Station stations = 1;
}
message Station {
string id = 1;
float lat = 2;
float lon = 3;
string name = 4;
repeated HourlyMeasure measures = 5;
}
message HourlyMeasure {
int32 hour = 1;
float temp_f = 2;
float temp_c = 3;
}
protobuf
Invented by Apple
The Joy  of Server Side Swift Development
Given this:
syntax = "proto3";
message Stations {
repeated Station stations = 1;
}
message Station {
string id = 1;
float lat = 2;
float lon = 3;
string name = 4;
repeated HourlyMeasure measures = 5;
}
message HourlyMeasure {
int32 hour = 1;
float temp_f = 2;
float temp_c = 3;
}
/*
* DO NOT EDIT.
*
* Generated by the protocol buffer compiler.
* Source: mrdrizzle.proto
*
*/
public struct Stations: ProtobufGeneratedMessage {
public var swiftClassName: String {return "Stations"}
public var protoMessageName: String {return "Stations"}
public var protoPackageName: String {return ""}
//...
}
public struct Station: ProtobufGeneratedMessage {
public var swiftClassName: String {return "Station"}
public var protoMessageName: String {return "Station"}
public var protoPackageName: String {return ""}
//...
}
public struct HourlyMeasure: ProtobufGeneratedMessage {
public var swiftClassName: String {return "HourlyMeasure"}
public var protoMessageName: String {return "HourlyMeasure"}
public var protoPackageName: String {return ""}
//...
}
We extend these structs to
instantiate from the
wunderground JSON
Wheater Underground JSON:
{
"response": {
"version": "0.1",
},
"hourly_forecast": [
{
"FCTTIME": {
"hour": "22",
"mday": "19",
"mday_padded": "19",
"yday": "292",
"isdst": "1",
"age": "",
"UTCDATE": ""
},
"temp": {
"english": "52",
"metric": "11"
},
"condition": "Clear",
"icon": "clear",
"metric": "-9999"
},
...
extension Station {
init?(stationInfo: StationInfo, json: JSON) {
guard let hourly_forecast =
json["hourly_forecast"]?.array as? [JSON]
else {
return nil
}
let measures = hourly_forecast.flatMap {
HourlyMeasure(json: $0)
}
self.init(lat: stationInfo.lat,
lon: stationInfo.lon,
name: stationInfo.name,
measures: measures)
}
}
extension HourlyMeasure {
init?(json: JSON) {
guard let fctTime = json["FCTTIME"]?.object,
let hour = fctTime["hour"]?.string,
let temp = json["temp"]?.object,
let tempF = temp["english"]?.string,
let tempC = temp["metric"]?.string
else {
return nil
}
self.init(hour: Int32(hour),
tempF: Float(tempF),
tempC: Float(tempC))
}
}
Retrieving from
Wunderground Server
extension Station {
init?(stationInfo: StationInfo, client: ClientProtocol.Type) {
let url = "http://(wheaterUndergroundDomain)/api/" +
"(wheaterUndergroundKey)/hourly/q/pws:" +
"(stationInfo.stationId).json"
guard let response = try? client.get(url),
case .data(let bytes) = response.body,
let json = try? JSON(bytes: bytes)
else {
return nil
}
self.init(stationInfo: stationInfo, json: json)
}
}
Refreshing every hour
We cannot use URLSession :-(
class WeatherService {
private let refreshQueue = DispatchQueue(label: "com.mrdrizzle.refresh")
private(set) var stations = Stations()
init(stationsInfo: [StationInfo], client: ClientProtocol.Type) {
refresh(stationsInfo: stationsInfo, client: client)
}
private func refresh(stationsInfo: [StationInfo], client: ClientProtocol.Type) {
stations = stations.refreshed(given: stationsInfo, client: client)
refreshQueue.asyncAfter(deadline: .now() + 3600) { [weak self] in
self?.refresh(stationsInfo: stationsInfo, client: client)
}
}
}
private extension Stations {
func refreshed(given stationsInfo: [StationInfo],
client: ClientProtocol.Type) -> Stations {
return Stations(stations: stationsInfo.flatMap {
Station(stationInfo: $0, client: client)
})
}
}
Putting everything
together
//main.swift
let drop = Droplet()
let stationsInfo = [
StationInfo(name: "Chelsea", stationId: "ILONDON330", lat: 51.479633, lon:-0.180277),
StationInfo(name: "Westminster", stationId: "ILONDON120", lat: 51.65343, lon:-0.183732)
//...
]
let weatherService = WeatherService(stationsInfo: stationsInfo, client: drop.client)
drop.get("/api/weather") { _ in
let stations = weatherService.stations
guard let data = try? stations.serializeProtobuf(),
let bytes = try? data.makeBytes()
else {
throw Abort.serverError
}
return Response(status: .ok, body: .data(bytes))
}
drop.run()
Job done!
MrDrizzle Client
Networking classes from
https://talk.objc.io/
episodes/S01E01-
networking
struct Resource<A> {
let url: URL
let parse: (Data) -> A?
}
for example:
let url = URL(string: "http://mrdrizzle.com:8080/hello")!
let helloResource = Resource<String>(url: url, parse: { data in
return String(data: data, encoding: .utf8)
})
class ApiService {
func load<A>(resource: Resource<A>, completion: @escaping (A?) -> ()) {
URLSession.shared.dataTask(with: resource.url) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200,
let data = data
else {
completion(nil)
return
}
completion(resource.parse(data))
}.resume()
}
}
let url = URL(string: "http://mrdrizzle.com:8080/hello")!
let helloResource = Resource<String>(url: url) { data in
return String(data: data, encoding: .utf8)
}
ApiService().load(resource: helloResource) {
guard let hello = $0 else {
print("ERROR - Hello")
return
}
print(hello)
}
let url = URL(string: "http://mrdrizzle.com:8080/hello")!
let stationResources = Resource<Stations>(url: url) { data in
return try? Stations(protobuf: data)
}
ApiService().load(resource: stationResources) {
guard let stations = $0 else {
print("ERROR - Stations")
return
}
print(stations)
}
The Joy  of Server Side Swift Development
It's ok :-)
Recap
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
However...
Apps are so 2014!
Let's do something modern
A Deep Learning Functional
Reactive Chatbot!
Story behind it
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Diagram of a Facebook
Messenger ChatBot
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Chat Flow
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
Let's do it
The Joy  of Server Side Swift Development
import PackageDescription
let package = Package(
name: "UncleLucio",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 0)
]
)
// main.swift
import Foundation
import Vapor
import HTTP
let PAGE_ACCESS_TOKEN = "STUFF"
let fbURL = "https://graph.facebook.com/v2.6/me/messages?access_token=" +
PAGE_ACCESS_TOKEN
let uncleLucio = UncleLucio(jokesDB: JokesDB())
let drop = Droplet()
// main.swift
drop.get("fbwebhook") { request in
guard let token = request.data["hub.verify_token"]?.string,
let response = request.data["hub.challenge"]?.string
else {
throw Abort.badRequest
}
if token == "UNCLELUCIO_VERIFICATION_TOKEN" {
return response // Response(status: .ok, text: response)
} else {
return "Error, invalid token"//Response(status: .ok, text: "Error, invalid token")
}
}
// main.swift
drop.post("fbwebhook") { request in
request.log()
guard let jsonBody = request.json,
let chatMessage = ChatMessage(message: jsonBody)
else {
throw Abort.badRequest
}
let replyMessage = uncleLucio.message(after: chatMessage)
return try drop.client.post(fbURL,
headers: ["Content-Type": "application/json; charset=utf-8"],
body: replyMessage.toJSON())
}
drop.run()
// ChatMessage.swift
import Foundation
import Vapor
import HTTP
struct ChatMessage {
let sender: String
let recipient: String
let text: String?
}
Payload send by Facebook callback
{
"object": "page",
"entry": [
{
"id": "1677732245875632",
"time": 1476209402183,
"messaging": [
{
"sender": {
"id": "1243544059050238"
},
"recipient": {
"id": "1677732245875632"
},
"timestamp": 1476209402090,
"message": {
"mid": "mid.1476209402075:82aff934133d72d746",
"seq": 17,
"text": "Knock knock"
}
}
]
}
]
}
// ChatMessage.swift
extension ChatMessage {
init?(message: JSON) {
guard let entry = message["entry"]?.array?.first as? JSON
else {
return nil
}
guard let messaging = entry["messaging"]?.array?.first as? JSON
else {
return nil
}
guard let senderWrapper = messaging["sender"]?.object,
let sender = senderWrapper["id"]?.string
else {
return nil
}
self.sender = sender
guard let recipientWrapper = messaging["recipient"]?.object,
let recipient = recipientWrapper["id"]?.string
else {
return nil
}
self.recipient = recipient
guard let message = messaging["message"]?.object
else {
return nil
}
self.text = message["text"]?.string
}
}
// ChatMessage.swift
extension ChatMessage {
func toJSON() throws -> JSON {
return try JSON(node: [
"sender": try JSON(node: [
"id": sender
]),
"recipient": try JSON(node: [
"id": recipient
]),
"message": try JSON(node: [
"text": text
]),
])
}
}
The Joy  of Server Side Swift Development
// UncleLucio.swift
class UncleLucio {
private let jokesDB: JokesDB
private var sessions = [String: State]()
init(jokesDB: JokesDB) {
self.jokesDB = jokesDB
}
func message(after chatMessage: ChatMessage) -> ChatMessage {
//...
return replyMessage
}
}
// UncleLucio.swift
func message(after chatMessage: ChatMessage) -> ChatMessage {
let state = sessions[chatMessage.sender] ?? StartState(joke: jokesDB.randomJoke())
let (text, newState) = state.nextState(when: chatMessage.text ?? "pota")
if newState is Done {
sessions.removeValue(forKey: chatMessage.sender)
} else {
sessions[chatMessage.sender] = newState
}
let replyMessage = ChatMessage(sender: chatMessage.recipient,
recipient: chatMessage.sender,
text: text)
return replyMessage
}
// UncleLucio.swift
protocol State {
func nextState(when message: String) -> (String, State)
}
// UncleLucio.swift
struct StartState: State {
let joke: Joke
func nextState(when message: String) -> (String, State) {
if message.lowercased().contains("joke") {
return ("Knock Knock", WaitingForReplyState(joke: joke))
}
return ("pota", self)
}
}
struct WaitingForReplyState: State {
let joke: Joke
func nextState(when message: String) -> (String, State) {
let text = message.lowercased()
if text.contains("who's there") ||
text.contains("who is there") {
return ("(joke.subject)!", WaitingForSubjectReplyState(joke: joke))
}
return ("pota", StartState(joke: joke))
}
}
struct WaitingForSubjectReplyState: State {
let joke: Joke
func nextState(when message: String) -> (String, State) {
let text = message.lowercased()
if text.contains("(joke.subject.lowercased()) who") {
return ("(joke.punchline)nahahah", Done())
}
return ("pota", StartState(joke: joke))
}
}
struct Done: State {
func nextState(when message: String) -> (String, State) {
return ("pota", Done())
}
}
Really easy to test
func testInStart_ReceivingGarbage_SaysSlogan_GoesStart() {
let state = StartState(joke: joke())
let (text, nextState) = state.nextState(when: "foobar")
XCTAssertEqual(text, "pota")
XCTAssertTrue(nextState is StartState)
}
func testWaitingForSubjectReply_ReceivingReply_SaysPunchline_GoesDone() {
let state = WaitingForSubjectReplyState(joke: joke())
let (text, nextState) = state.nextState(when: "(joke().subject) who")
XCTAssertEqual(text, "(joke().punchline)nahahah")
XCTAssertTrue(nextState is Done)
}
class JokesDB {
private let jokes: [Joke] = [
("Harry", "Harry up and let me in!"),
("Wanda", "Wanda hang out with me right now?"),
("Olive", "Olive you and I don’t care who knows it!"),
("Ho-ho", "You know, your Santa impression could use a little work."),
("Hanna", "...Hanna partridge in a pear tree!"),
("Mary and Abbey", "Mary Christmas and Abbey New Year!"),
("Irish", "Irish you a Merry Christmas!"),
("Yule log", "Yule log the door after you let me in, won’t you?"),
("Ya", "I’m excited to see you too!"),
("Sherlock", "Sherlock your door shut tight!"),
("Scold", "Scold outside—let me in!"),
("Robin", "Robin you! Hand over your cash!"),
("Needle", "Needle little help gettin’ in the door."),
("Nana", "Nana your business who’s there."),
("Luke", "Luke through the keyhole to see!"),
("Isabelle", "Isabelle working, or should I keep knocking?"),
].map {
Joke(subject: $0.0,
punchline: $0.1)
}
func randomJoke() -> Joke {
return jokes.randomItem()
}
}
extension Array {
func randomItem() -> Element {
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
}
https://github.com/gscalzo/UncleLucio
Finally...
A reason for using Swift in
Server...
Often in our job...
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
But sometimes...
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
The Joy  of Server Side Swift Development
"Everything in Its Right
Place" (cit.)
The Joy  of Server Side Swift Development
Thank You!

More Related Content

PDF
Getting Started with Raspberry Pi - DCC 2013.1
PDF
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
PDF
[GEG1] 10.camera-centric engine design for multithreaded rendering
PPTX
C++ AMP 실천 및 적용 전략
PDF
Vc4c development of opencl compiler for videocore4
PPTX
OpenCL Heterogeneous Parallel Computing
PDF
Open CL For Speedup Workshop
PDF
Qemu JIT Code Generator and System Emulation
Getting Started with Raspberry Pi - DCC 2013.1
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
[GEG1] 10.camera-centric engine design for multithreaded rendering
C++ AMP 실천 및 적용 전략
Vc4c development of opencl compiler for videocore4
OpenCL Heterogeneous Parallel Computing
Open CL For Speedup Workshop
Qemu JIT Code Generator and System Emulation

What's hot (19)

PDF
Inside the ABC's new Media Transcoding system, Metro
PDF
JIT compilation for CPython
PDF
tokyotalk
PDF
Video Transcoding at Scale for ABC iview (NDC Sydney)
PPTX
Introduction to Stockfish bitboard representation and magic bitboard
PDF
Video Transcoding at the ABC with Microservices at GOTO Chicago
PDF
200819 NAVER TECH CONCERT 03_화려한 코루틴이 내 앱을 감싸네! 코루틴으로 작성해보는 깔끔한 비동기 코드
PDF
Node Classifier Fundamentals - PuppetConf 2014
PDF
Machine Trace Metrics
PPT
Intro2 Cuda Moayad
KEY
Groovy: to Infinity and Beyond -- JavaOne 2010 -- Guillaume Laforge
PDF
Introduction to RevKit
PPTX
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
PDF
Reversible Logic Synthesis and RevKit
PPTX
Multicloud connectivity using OpenNHRP
PDF
C++ How I learned to stop worrying and love metaprogramming
PPTX
General Purpose Computing using Graphics Hardware
PPT
Introduction to Data Oriented Design
PDF
from Binary to Binary: How Qemu Works
Inside the ABC's new Media Transcoding system, Metro
JIT compilation for CPython
tokyotalk
Video Transcoding at Scale for ABC iview (NDC Sydney)
Introduction to Stockfish bitboard representation and magic bitboard
Video Transcoding at the ABC with Microservices at GOTO Chicago
200819 NAVER TECH CONCERT 03_화려한 코루틴이 내 앱을 감싸네! 코루틴으로 작성해보는 깔끔한 비동기 코드
Node Classifier Fundamentals - PuppetConf 2014
Machine Trace Metrics
Intro2 Cuda Moayad
Groovy: to Infinity and Beyond -- JavaOne 2010 -- Guillaume Laforge
Introduction to RevKit
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Reversible Logic Synthesis and RevKit
Multicloud connectivity using OpenNHRP
C++ How I learned to stop worrying and love metaprogramming
General Purpose Computing using Graphics Hardware
Introduction to Data Oriented Design
from Binary to Binary: How Qemu Works
Ad

Viewers also liked (17)

PPTX
Intro to Perfect - LA presentation
PDF
Server Side? Swift
PDF
CLP Faculty Award
PDF
scan0002
PDF
Κοινοποίηση πίνακα βραβευθέντων εκπαιδευτικών για συμμετοχή στην εκδήλωση Θεσ...
DOCX
Plywood and fibreboard
PDF
Keri Recommendation Letter
PDF
Appreciation (1)
PDF
UTAustinX UT.7
PDF
Coursera FDWS23XVLKLB
PDF
%Ce%a4%ce%9 f %ce%91%ce%a1%ce%98%ce%a1%ce%9f-%ce%9c%ce%9f%ce%a5
PDF
Swift for back end: A new generation of full stack languages?
PDF
Build,Run and manage MobileFirst apps with Eclipse
PDF
CSR-Best Initiative Award
PDF
Growing from 0 to 100 million users
PPTX
Server Side Swift
PDF
Server-side Swift
Intro to Perfect - LA presentation
Server Side? Swift
CLP Faculty Award
scan0002
Κοινοποίηση πίνακα βραβευθέντων εκπαιδευτικών για συμμετοχή στην εκδήλωση Θεσ...
Plywood and fibreboard
Keri Recommendation Letter
Appreciation (1)
UTAustinX UT.7
Coursera FDWS23XVLKLB
%Ce%a4%ce%9 f %ce%91%ce%a1%ce%98%ce%a1%ce%9f-%ce%9c%ce%9f%ce%a5
Swift for back end: A new generation of full stack languages?
Build,Run and manage MobileFirst apps with Eclipse
CSR-Best Initiative Award
Growing from 0 to 100 million users
Server Side Swift
Server-side Swift
Ad

Similar to The Joy of Server Side Swift Development (20)

ODP
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
ODP
Intravert Server side processing for Cassandra
PDF
Emerging Languages: A Tour of the Horizon
PDF
The Ring programming language version 1.3 book - Part 84 of 88
PDF
CouchDB Mobile - From Couch to 5K in 1 Hour
PDF
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
PPTX
Kotlin / Android Update
ODP
Introduction to Spark with Scala
PDF
Shrink to grow
PDF
Introduction to Coding
PDF
Presto anatomy
PDF
Kotlin, smarter development for the jvm
PDF
Bolt C++ Standard Template Libary for HSA by Ben Sanders, AMD
PDF
Flux and InfluxDB 2.0 by Paul Dix
PPTX
Building and Deploying Application to Apache Mesos
PDF
Kotlin Developer Starter in Android projects
PDF
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
PDF
Android RenderScript on LLVM
PDF
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
PPTX
Lambdas puzzler - Peter Lawrey
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
Intravert Server side processing for Cassandra
Emerging Languages: A Tour of the Horizon
The Ring programming language version 1.3 book - Part 84 of 88
CouchDB Mobile - From Couch to 5K in 1 Hour
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
Kotlin / Android Update
Introduction to Spark with Scala
Shrink to grow
Introduction to Coding
Presto anatomy
Kotlin, smarter development for the jvm
Bolt C++ Standard Template Libary for HSA by Ben Sanders, AMD
Flux and InfluxDB 2.0 by Paul Dix
Building and Deploying Application to Apache Mesos
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Android RenderScript on LLVM
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Lambdas puzzler - Peter Lawrey

Recently uploaded (6)

PDF
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
PPTX
ASMS Telecommunication company Profile
DOC
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
ASMS Telecommunication company Profile
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证

The Joy of Server Side Swift Development

  • 1. The Joy Of Server Side Swift Development
  • 2. In 1943 Konrad Zuse invented Plankalkül
  • 4. 1950s ... - Fortran(concept) - Fortran II - ALGOL 58(IAL) - LISP(implementation) ...
  • 6. 1960s ... - ALGOL 60 - COBOL 61(implementation) - FORTRAN IV - APL(concept) - Simula(concept) - SNOBOL - CPL - SNOBOL3 - ALGOL 68(concept) - BASIC ...
  • 8. 1970s ... - Pascal - Smalltalk - C - Prolog - Structured Query language ( SQL) - Bourne Shell(sh) - Modula-2 - AWK ...
  • 10. 1980s ... - Ada 80(MIL-STD-1815) - GW-BASIC - Turbo Pascal - Objective-C - C++ - Eiffel - Erlang ...
  • 12. 1990s ... - AMOS BASIC - Haskell - Python - Visual Basic - Brainfuck - Borland Delphi - Java - PHP - Ruby - JavaScript - Standard C++ ...
  • 14. 2000s ... - ActionScript - C# - D - Scala - Groovy - F# - Clojure - Go ...
  • 16. 2010s ... - Rust - Elm - Kotlin - Elixir - Hack - C++14 ...
  • 20. 355
  • 21. Do we really need another programming language?
  • 35. Officially, only Linux is Supported
  • 41. We have a few...
  • 42. Perfect: github.github.com/PerfectlySoft/Perfect (8829) Vapor: github.com/vapor/vapor (6763) Kitura: github.com/IBM-Swift/Kitura (4578) Swifton: github.com/necolt/Swifton (2016) Zewo: github.com/Zewo/Zewo (1358) Blackfish: github.com/elliottminns/blackfish (932) Slimane: github.com/noppoMan/Slimane (61) Tailor: github.com/brownleej/tailor (55) Kunugi: github.com/novi/Kunugi (35) Quark: github.com/QuarkX/Quark (33)
  • 44. "Perfect is a complete and powerful toolbox, framework, and application server for Linux, iOS, and macOS"
  • 45. They got money (Even without React.js!)
  • 50. let server = HTTPServer() var routes = Routes() routes.add(method: .get, uri: "/", handler: { request, response in response.setHeader(.contentType, value: "text/html") response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>") response.completed() } ) server.addRoutes(routes) server.serverPort = 8181 server.documentRoot = "./webroot" configureServer(server) do { try server.start() } catch PerfectError.networkError(let err, let msg) { print("Network error thrown: (err) (msg)") }
  • 52. 4 Laravel inspired 4 Easy to setup 4 Fantastic Command Line Tool
  • 54. let drop = Droplet() drop.get("/") { request in return try drop.view.make("welcome.html") } drop.get("plaintext") { request in return "Hello, World!" } drop.serve()
  • 57. 4 Backed by IBM 4 Express.js-like 4 Perfectly integrated in IBM cloud solution BlueMix
  • 59. import Kitura let router = Router() router.get("/") { request, response, next in response.send("Hello, World!") next() } Kitura.addHTTPServer(onPort: 8080, with: router) Kitura.run()
  • 60. Let's talk now of...
  • 74. You can write Basic in Swift...
  • 75. You can write Haskell-ish in Swift...
  • 76. The sense of Swift
  • 82. An hyper local weather app
  • 87. MrDrizzle Server Requirements: 4 Convert from Wunderground Models to MrDrizzle Model 4 Refresh cache every hour
  • 90. mkdir MrDrizzleServer cd MrDrizzleServer swift package init --type executable
  • 92. Swift Package Manager import PackageDescription let package = Package( name: "MrDrizzleServer", dependencies: [ .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 0) ] )
  • 93. Let's make it run // main.swift import Foundation import HTTP import Vapor let wheaterUndergroundKey = "KEY" let wheaterUndergroundDomain = "api.wunderground.com" let drop = Droplet() drop.get("/hello") { _ in return "Hello World" } drop.run()
  • 94. $ swift build > Cloning https://github.com/vapor/vapor.git > HEAD is now at 453f7cf Merge pull request #628 > Resolved version: 1.0.3 > Cloning https://github.com/vapor/crypto.git > HEAD is now at 0aaa68b Merge pull request #15 > Resolved version: 1.0.1 > Cloning https://github.com/vapor/core.git .... .... > Compile Swift Module 'TypeSafeRouting' (3 sources) > Compile Swift Module 'Vapor' (83 sources) > Compile Swift Module 'MrDrizzleServer' (1 sources) > Linking ./.build/debug/MrDrizzleServer $ .build/debug/MrDrizzleServer
  • 95. > Could not load localization files > No cipher key was set, using blank key. > Chacha20 cipher requires an initialization > No command supplied, defaulting to serve > No preparations. > No servers.json configuration found. > Starting server at 0.0.0.0:8080
  • 98. $ swift package generate-xcodeproj > generated: ./MrDrizzleServer.xcodeproj
  • 100. Hello World for dummies
  • 101. This version: drop.get("/hello") { _ in return "Hello World" }
  • 102. Is equivalent to: func handler(request: Request) throws -> ResponseRepresentable { return Response(status: .ok, body: "Hello, World") } drop.get("/hello", handler: handler) because: extension Swift.String: ResponseRepresentable { public func makeResponse() -> Response { return Response(body: self.bytes) } }
  • 104. The list of the stations: struct StationInfo { let name: String let stationId: String let lat: Float let lon: Float } let stationsInfo = [ StationInfo(name: "Chelsea", stationId: "ILONDON330", lat: 51.479633, lon:-0.180277), StationInfo(name: "Westminster", stationId: "ILONDON120", lat: 51.65343, lon:-0.183732) //... ]
  • 105. Let's define a schema for our models
  • 106. Model schema in pseudocode object Stations { Array of Station stations } object Station { string id float lat float lon string name Array of HourlyMeasure measures } object HourlyMeasure { int32 hour float temp_f float temp_c }
  • 107. syntax = "proto3"; message Stations { repeated Station stations = 1; } message Station { string id = 1; float lat = 2; float lon = 3; string name = 4; repeated HourlyMeasure measures = 5; } message HourlyMeasure { int32 hour = 1; float temp_f = 2; float temp_c = 3; }
  • 110. Given this: syntax = "proto3"; message Stations { repeated Station stations = 1; } message Station { string id = 1; float lat = 2; float lon = 3; string name = 4; repeated HourlyMeasure measures = 5; } message HourlyMeasure { int32 hour = 1; float temp_f = 2; float temp_c = 3; }
  • 111. /* * DO NOT EDIT. * * Generated by the protocol buffer compiler. * Source: mrdrizzle.proto * */ public struct Stations: ProtobufGeneratedMessage { public var swiftClassName: String {return "Stations"} public var protoMessageName: String {return "Stations"} public var protoPackageName: String {return ""} //... } public struct Station: ProtobufGeneratedMessage { public var swiftClassName: String {return "Station"} public var protoMessageName: String {return "Station"} public var protoPackageName: String {return ""} //... } public struct HourlyMeasure: ProtobufGeneratedMessage { public var swiftClassName: String {return "HourlyMeasure"} public var protoMessageName: String {return "HourlyMeasure"} public var protoPackageName: String {return ""} //... }
  • 112. We extend these structs to instantiate from the wunderground JSON
  • 113. Wheater Underground JSON: { "response": { "version": "0.1", }, "hourly_forecast": [ { "FCTTIME": { "hour": "22", "mday": "19", "mday_padded": "19", "yday": "292", "isdst": "1", "age": "", "UTCDATE": "" }, "temp": { "english": "52", "metric": "11" }, "condition": "Clear", "icon": "clear", "metric": "-9999" }, ...
  • 114. extension Station { init?(stationInfo: StationInfo, json: JSON) { guard let hourly_forecast = json["hourly_forecast"]?.array as? [JSON] else { return nil } let measures = hourly_forecast.flatMap { HourlyMeasure(json: $0) } self.init(lat: stationInfo.lat, lon: stationInfo.lon, name: stationInfo.name, measures: measures) } }
  • 115. extension HourlyMeasure { init?(json: JSON) { guard let fctTime = json["FCTTIME"]?.object, let hour = fctTime["hour"]?.string, let temp = json["temp"]?.object, let tempF = temp["english"]?.string, let tempC = temp["metric"]?.string else { return nil } self.init(hour: Int32(hour), tempF: Float(tempF), tempC: Float(tempC)) } }
  • 117. extension Station { init?(stationInfo: StationInfo, client: ClientProtocol.Type) { let url = "http://(wheaterUndergroundDomain)/api/" + "(wheaterUndergroundKey)/hourly/q/pws:" + "(stationInfo.stationId).json" guard let response = try? client.get(url), case .data(let bytes) = response.body, let json = try? JSON(bytes: bytes) else { return nil } self.init(stationInfo: stationInfo, json: json) } }
  • 119. We cannot use URLSession :-( class WeatherService { private let refreshQueue = DispatchQueue(label: "com.mrdrizzle.refresh") private(set) var stations = Stations() init(stationsInfo: [StationInfo], client: ClientProtocol.Type) { refresh(stationsInfo: stationsInfo, client: client) } private func refresh(stationsInfo: [StationInfo], client: ClientProtocol.Type) { stations = stations.refreshed(given: stationsInfo, client: client) refreshQueue.asyncAfter(deadline: .now() + 3600) { [weak self] in self?.refresh(stationsInfo: stationsInfo, client: client) } } }
  • 120. private extension Stations { func refreshed(given stationsInfo: [StationInfo], client: ClientProtocol.Type) -> Stations { return Stations(stations: stationsInfo.flatMap { Station(stationInfo: $0, client: client) }) } }
  • 122. //main.swift let drop = Droplet() let stationsInfo = [ StationInfo(name: "Chelsea", stationId: "ILONDON330", lat: 51.479633, lon:-0.180277), StationInfo(name: "Westminster", stationId: "ILONDON120", lat: 51.65343, lon:-0.183732) //... ] let weatherService = WeatherService(stationsInfo: stationsInfo, client: drop.client) drop.get("/api/weather") { _ in let stations = weatherService.stations guard let data = try? stations.serializeProtobuf(), let bytes = try? data.makeBytes() else { throw Abort.serverError } return Response(status: .ok, body: .data(bytes)) } drop.run()
  • 126. struct Resource<A> { let url: URL let parse: (Data) -> A? } for example: let url = URL(string: "http://mrdrizzle.com:8080/hello")! let helloResource = Resource<String>(url: url, parse: { data in return String(data: data, encoding: .utf8) })
  • 127. class ApiService { func load<A>(resource: Resource<A>, completion: @escaping (A?) -> ()) { URLSession.shared.dataTask(with: resource.url) { data, response, error in guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, let data = data else { completion(nil) return } completion(resource.parse(data)) }.resume() } }
  • 128. let url = URL(string: "http://mrdrizzle.com:8080/hello")! let helloResource = Resource<String>(url: url) { data in return String(data: data, encoding: .utf8) } ApiService().load(resource: helloResource) { guard let hello = $0 else { print("ERROR - Hello") return } print(hello) }
  • 129. let url = URL(string: "http://mrdrizzle.com:8080/hello")! let stationResources = Resource<Stations>(url: url) { data in return try? Stations(protobuf: data) } ApiService().load(resource: stationResources) { guard let stations = $0 else { print("ERROR - Stations") return } print(stations) }
  • 132. Recap
  • 136. Apps are so 2014!
  • 138. A Deep Learning Functional Reactive Chatbot!
  • 146. Diagram of a Facebook Messenger ChatBot
  • 156. import PackageDescription let package = Package( name: "UncleLucio", dependencies: [ .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 0) ] )
  • 157. // main.swift import Foundation import Vapor import HTTP let PAGE_ACCESS_TOKEN = "STUFF" let fbURL = "https://graph.facebook.com/v2.6/me/messages?access_token=" + PAGE_ACCESS_TOKEN let uncleLucio = UncleLucio(jokesDB: JokesDB()) let drop = Droplet()
  • 158. // main.swift drop.get("fbwebhook") { request in guard let token = request.data["hub.verify_token"]?.string, let response = request.data["hub.challenge"]?.string else { throw Abort.badRequest } if token == "UNCLELUCIO_VERIFICATION_TOKEN" { return response // Response(status: .ok, text: response) } else { return "Error, invalid token"//Response(status: .ok, text: "Error, invalid token") } }
  • 159. // main.swift drop.post("fbwebhook") { request in request.log() guard let jsonBody = request.json, let chatMessage = ChatMessage(message: jsonBody) else { throw Abort.badRequest } let replyMessage = uncleLucio.message(after: chatMessage) return try drop.client.post(fbURL, headers: ["Content-Type": "application/json; charset=utf-8"], body: replyMessage.toJSON()) } drop.run()
  • 160. // ChatMessage.swift import Foundation import Vapor import HTTP struct ChatMessage { let sender: String let recipient: String let text: String? }
  • 161. Payload send by Facebook callback { "object": "page", "entry": [ { "id": "1677732245875632", "time": 1476209402183, "messaging": [ { "sender": { "id": "1243544059050238" }, "recipient": { "id": "1677732245875632" }, "timestamp": 1476209402090, "message": { "mid": "mid.1476209402075:82aff934133d72d746", "seq": 17, "text": "Knock knock" } } ] } ] }
  • 162. // ChatMessage.swift extension ChatMessage { init?(message: JSON) { guard let entry = message["entry"]?.array?.first as? JSON else { return nil } guard let messaging = entry["messaging"]?.array?.first as? JSON else { return nil } guard let senderWrapper = messaging["sender"]?.object, let sender = senderWrapper["id"]?.string else { return nil } self.sender = sender guard let recipientWrapper = messaging["recipient"]?.object, let recipient = recipientWrapper["id"]?.string else { return nil } self.recipient = recipient guard let message = messaging["message"]?.object else { return nil } self.text = message["text"]?.string } }
  • 163. // ChatMessage.swift extension ChatMessage { func toJSON() throws -> JSON { return try JSON(node: [ "sender": try JSON(node: [ "id": sender ]), "recipient": try JSON(node: [ "id": recipient ]), "message": try JSON(node: [ "text": text ]), ]) } }
  • 165. // UncleLucio.swift class UncleLucio { private let jokesDB: JokesDB private var sessions = [String: State]() init(jokesDB: JokesDB) { self.jokesDB = jokesDB } func message(after chatMessage: ChatMessage) -> ChatMessage { //... return replyMessage } }
  • 166. // UncleLucio.swift func message(after chatMessage: ChatMessage) -> ChatMessage { let state = sessions[chatMessage.sender] ?? StartState(joke: jokesDB.randomJoke()) let (text, newState) = state.nextState(when: chatMessage.text ?? "pota") if newState is Done { sessions.removeValue(forKey: chatMessage.sender) } else { sessions[chatMessage.sender] = newState } let replyMessage = ChatMessage(sender: chatMessage.recipient, recipient: chatMessage.sender, text: text) return replyMessage }
  • 167. // UncleLucio.swift protocol State { func nextState(when message: String) -> (String, State) }
  • 168. // UncleLucio.swift struct StartState: State { let joke: Joke func nextState(when message: String) -> (String, State) { if message.lowercased().contains("joke") { return ("Knock Knock", WaitingForReplyState(joke: joke)) } return ("pota", self) } }
  • 169. struct WaitingForReplyState: State { let joke: Joke func nextState(when message: String) -> (String, State) { let text = message.lowercased() if text.contains("who's there") || text.contains("who is there") { return ("(joke.subject)!", WaitingForSubjectReplyState(joke: joke)) } return ("pota", StartState(joke: joke)) } }
  • 170. struct WaitingForSubjectReplyState: State { let joke: Joke func nextState(when message: String) -> (String, State) { let text = message.lowercased() if text.contains("(joke.subject.lowercased()) who") { return ("(joke.punchline)nahahah", Done()) } return ("pota", StartState(joke: joke)) } }
  • 171. struct Done: State { func nextState(when message: String) -> (String, State) { return ("pota", Done()) } }
  • 172. Really easy to test
  • 173. func testInStart_ReceivingGarbage_SaysSlogan_GoesStart() { let state = StartState(joke: joke()) let (text, nextState) = state.nextState(when: "foobar") XCTAssertEqual(text, "pota") XCTAssertTrue(nextState is StartState) }
  • 174. func testWaitingForSubjectReply_ReceivingReply_SaysPunchline_GoesDone() { let state = WaitingForSubjectReplyState(joke: joke()) let (text, nextState) = state.nextState(when: "(joke().subject) who") XCTAssertEqual(text, "(joke().punchline)nahahah") XCTAssertTrue(nextState is Done) }
  • 175. class JokesDB { private let jokes: [Joke] = [ ("Harry", "Harry up and let me in!"), ("Wanda", "Wanda hang out with me right now?"), ("Olive", "Olive you and I don’t care who knows it!"), ("Ho-ho", "You know, your Santa impression could use a little work."), ("Hanna", "...Hanna partridge in a pear tree!"), ("Mary and Abbey", "Mary Christmas and Abbey New Year!"), ("Irish", "Irish you a Merry Christmas!"), ("Yule log", "Yule log the door after you let me in, won’t you?"), ("Ya", "I’m excited to see you too!"), ("Sherlock", "Sherlock your door shut tight!"), ("Scold", "Scold outside—let me in!"), ("Robin", "Robin you! Hand over your cash!"), ("Needle", "Needle little help gettin’ in the door."), ("Nana", "Nana your business who’s there."), ("Luke", "Luke through the keyhole to see!"), ("Isabelle", "Isabelle working, or should I keep knocking?"), ].map { Joke(subject: $0.0, punchline: $0.1) } func randomJoke() -> Joke { return jokes.randomItem() } }
  • 176. extension Array { func randomItem() -> Element { let index = Int(arc4random_uniform(UInt32(self.count))) return self[index] } }
  • 179. A reason for using Swift in Server...
  • 180. Often in our job...
  • 188. "Everything in Its Right Place" (cit.)