Slide 1

Slide 1 text

End-to-end: Building a Web Service in Swift

Slide 2

Slide 2 text

Who am I?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

How many of you have build web services or APIs?

Slide 5

Slide 5 text

What is a web service?

Slide 6

Slide 6 text

Why Swift on Server?

Slide 7

Slide 7 text

Swift

Slide 8

Slide 8 text

Why do many use scripting languages for the web?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Code Sharing

Slide 11

Slide 11 text

Accessible to mobile developers

Slide 12

Slide 12 text

Performance

Slide 13

Slide 13 text

Performance Graph Server | Language | request/sec | Comparison ---------|----------|-------------|------------ Curassow | Swift | 7715.83 | Gunicorn | Python | 5269.49 | 32% less req/s than Curassow Unicorn | Ruby | 4253.33 | 45% less req/s Curassow

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

How

Slide 16

Slide 16 text

/// Format the given string for presentation func formatForDate(date: NSDate) -> String { let formatter = NSDateFormatter() formatter.formatString = "DD MM yyyy" return formatter.stringForDate(date) }

Slide 17

Slide 17 text

func viewDidLoad() { super.viewDidLoad() // Using our date formatting in iOS button.title = formatForDate(NSDate()) }

Slide 18

Slide 18 text

import Frank get { return formatForDate(NSDate()) }

Slide 19

Slide 19 text

$ swift build $ .build/debug/date-example

Slide 20

Slide 20 text

$ curl http://localhost:8000/ 21 04 2016

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Where to begin

Slide 23

Slide 23 text

Life-cycle

Slide 24

Slide 24 text

Planning

Slide 25

Slide 25 text

https://apiblueprint.org/

Slide 26

Slide 26 text

# Render [POST /render] + Request (application/json) { "template": "Hello {{ name }}", "context": { "name": "Kyle" } }

Slide 27

Slide 27 text

# Preview [POST /preview] + Response 200 Hello Kyle

Slide 28

Slide 28 text

Development

Slide 29

Slide 29 text

Swift Package Manager

Slide 30

Slide 30 text

Swift 2 vs Swift 3

Slide 31

Slide 31 text

swiftenv https://swiftenv.fuller.li/

Slide 32

Slide 32 text

$ swiftenv install 2.2

Slide 33

Slide 33 text

$ swiftenv local DEVELOPMENT-SNAPSHOT-2016-02-08-a

Slide 34

Slide 34 text

$ cat .swift-version DEVELOPMENT-SNAPSHOT-2016-02-08-a

Slide 35

Slide 35 text

$ swift --version Apple Swift version 2.2 $ cd Stencil $ swift --version Apple Swift version 3.0-dev

Slide 36

Slide 36 text

Web Frameworks

Slide 37

Slide 37 text

Separation of Concerns Web Servers / Web Frameworks

Slide 38

Slide 38 text

Frank

Slide 39

Slide 39 text

get("users", "kyle", "followers") { request in }

Slide 40

Slide 40 text

get("users", *) { (request, username: String) in }

Slide 41

Slide 41 text

Custom Parameter Type enum Status { case Open case Closed }

Slide 42

Slide 42 text

Custom Parameter Type extension Status : ParameterConvertible { init?(parser: ParameterParser) { switch parser.shift() ?? "" { case "open": self = .Open case "closed": self = .Closed default: return nil } } }

Slide 43

Slide 43 text

get("issues", *) { (request, status: Status) in return "Issues using status: \(status)" }

Slide 44

Slide 44 text

Swift Safety get("/users/:username") { (request, params) in let username = params["username"]! return "Hello \(username)" }

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

let router = Router() router.get("/users/:username") { request, response, next in let username = request.params["username"] ?? "(nil)" try response.status(HttpStatusCode.OK).send("Hello \(username)") } let server = HttpServer.listen(8090, delegate: router) Server.run()

Slide 47

Slide 47 text

Web Server

Slide 48

Slide 48 text

Curassow https://curassow.fuller.li/

Slide 49

Slide 49 text

$ curassow --workers 5 [INFO] Listening at http://localhost:8080 (65416) [INFO] Booting worker process with pid: 65417 [INFO] Booting worker process with pid: 65418 [INFO] Booting worker process with pid: 65419 [INFO] Booting worker process with pid: 65420 [INFO] Booting worker process with pid: 65421

Slide 50

Slide 50 text

Standards

Slide 51

Slide 51 text

Learn from others' mistakes

Slide 52

Slide 52 text

Tight Coupling

Slide 53

Slide 53 text

Nest https://github.com/nestproject/Nest

Slide 54

Slide 54 text

func application(request: RequestType) -> ResponseType { return Response(.Ok, body: "Hello World") }

Slide 55

Slide 55 text

Nest Enhancement Proposals

Slide 56

Slide 56 text

Template Languages

Slide 57

Slide 57 text

Stencil http://stencil.fuller.li/

Slide 58

Slide 58 text

There are {{ articles.count }} articles. {% for article in articles %} - {{ article.title }} by {{ article.author }}. {% endfor %}

Slide 59

Slide 59 text

Users

    {% for user in users %}
  • {{ user }}
  • {% endfor %}

Slide 60

Slide 60 text

Persistence

Slide 61

Slide 61 text

ORM Object Relational Mapping

Slide 62

Slide 62 text

QueryKit http://querykit.org/ let user = User.queryset(context) .filter { $0.name == "Kyle" } .orderBy {} .first

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Redis (Redbird) let client = try Redbird(config: ...) // Set name to MCE try client.command("SET", params: ["name", "MCE"]) // Get the name try client.command("GET", params: ["name"])

Slide 65

Slide 65 text

PostgreSQL let connection = Connection(host: "localhost", databaseName: "db") try connection.open() let usernames = try connection.execute("SELECT username FROM users").map { try $0.data("username") }

Slide 66

Slide 66 text

Testing

Slide 67

Slide 67 text

Spectre http://spectre.fuller.li/

Slide 68

Slide 68 text

describe("a person") { let person = Person(name: "Kyle") $0.it("has a name") { try expect(person.name) == "Kyle" } $0.it("returns the name as description") { try expect(person.description) == "Kyle" } }

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

XCTest

Slide 71

Slide 71 text

class PersonTests: XCTestCase { let person = Person(name: "Kyle") func testPersonName() { XCTAssertEqual(person.name, "Kyle") } func testPersonDescription() { XCTAssertEqual(person.description, "Kyle") } }

Slide 72

Slide 72 text

extension PersonTests: XCTestCaseProvider { var allTests : [(String, () throws -> Void)] { return [ ("testPersonName", testPersonName), ("testPersonDescription", testPersonDescription), ] } } XCTMain([ PersonTests(), ])

Slide 73

Slide 73 text

Dredd https://github.com/apiaryio/dredd

Slide 74

Slide 74 text

Swift on Travis CI https://swiftenv.fuller.li/en/latest/ integrations/travis-ci.html

Slide 75

Slide 75 text

Deployment

Slide 76

Slide 76 text

https://github.com/kylef/heroku-buildpack-swift

Slide 77

Slide 77 text

$ cat Package.swift import PackageDescription let package = Package( name: "Hello", dependencies: [ .Package(url: "https://github.com/nestproject/Frank.git", majorVersion: 0, minor: 3), ] )

Slide 78

Slide 78 text

$ cat Sources/main.swift import Frank get { _ in return "Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }

Slide 79

Slide 79 text

$ cat .swift-version 2.2-SNAPSHOT-2016-01-11-a

Slide 80

Slide 80 text

$ swift build $ .build/debug/Hello [INFO] Listening at http://0.0.0.0:8000 (48827) [INFO] Booting worker process with pid: 48828

Slide 81

Slide 81 text

$ cat Procfile web: Hello

Slide 82

Slide 82 text

$ heroku create --buildpack https://github.com/kylef/heroku-buildpack-swift.git $ git push heroku master remote: -----> Swift app detected remote: -----> Installing 2.2-SNAPSHOT-2016-01-11-a remote: -----> Installing clang-3.7.0 remote: -----> Building Package remote: -----> Copying binaries to 'bin'

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Manual Deployment

Slide 86

Slide 86 text

Monitoring

Slide 87

Slide 87 text

Logging

Slide 88

Slide 88 text

print("ERROR: Connection to database failed \(error)")

Slide 89

Slide 89 text

Papertrail

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

What's Next?

Slide 93

Slide 93 text

Swift 3

Slide 94

Slide 94 text

Stability

Slide 95

Slide 95 text

Maturity

Slide 96

Slide 96 text

What have we covered today • Why you might want to build a web application in Swift • How you can design, develop, deploy Swift web applications

Slide 97

Slide 97 text

kylefuller https://fuller.li/talks