End-to-end: Building a Web Service in Swift (MCE 2016)

End-to-end: Building a Web Service in Swift (MCE 2016)

Now that Swift is Open Source, Apple has created a new range of
possibilities. You can now take your existing knowledge, code and
apply them to web application development. I'm going to show you
how you can reuse existing skills to build and deploy your first
web service in Swift.

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

April 22, 2016
Tweet

Transcript

  1. 3.
  2. 7.
  3. 9.
  4. 13.

    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
  5. 14.
  6. 15.

    How

  7. 16.

    /// Format the given string for presentation func formatForDate(date: NSDate)

    -> String { let formatter = NSDateFormatter() formatter.formatString = "DD MM yyyy" return formatter.stringForDate(date) }
  8. 17.
  9. 21.
  10. 24.
  11. 26.
  12. 35.

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

    $ swift --version Apple Swift version 3.0-dev
  13. 38.
  14. 42.

    Custom Parameter Type extension Status : ParameterConvertible { init?(parser: ParameterParser)

    { switch parser.shift() ?? "" { case "open": self = .Open case "closed": self = .Closed default: return nil } } }
  15. 44.

    Swift Safety get("/users/:username") { (request, params) in let username =

    params["username"]! return "Hello \(username)" }
  16. 45.
  17. 46.

    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()
  18. 49.

    $ 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
  19. 50.
  20. 58.

    There are {{ articles.count }} articles. {% for article in

    articles %} - {{ article.title }} by {{ article.author }}. {% endfor %}
  21. 63.
  22. 64.

    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"])
  23. 65.

    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") }
  24. 66.
  25. 68.

    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" } }
  26. 69.
  27. 70.
  28. 71.

    class PersonTests: XCTestCase { let person = Person(name: "Kyle") func

    testPersonName() { XCTAssertEqual(person.name, "Kyle") } func testPersonDescription() { XCTAssertEqual(person.description, "Kyle") } }
  29. 72.

    extension PersonTests: XCTestCaseProvider { var allTests : [(String, () throws

    -> Void)] { return [ ("testPersonName", testPersonName), ("testPersonDescription", testPersonDescription), ] } } XCTMain([ PersonTests(), ])
  30. 77.

    $ cat Package.swift import PackageDescription let package = Package( name:

    "Hello", dependencies: [ .Package(url: "https://github.com/nestproject/Frank.git", majorVersion: 0, minor: 3), ] )
  31. 78.

    $ cat Sources/main.swift import Frank get { _ in return

    "Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }
  32. 82.

    $ 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'
  33. 83.
  34. 84.
  35. 87.
  36. 90.
  37. 91.
  38. 93.
  39. 94.
  40. 95.
  41. 96.

    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