Upgrade to Pro — share decks privately, control downloads, hide ads and more …

End-to-end: Building a Web Service in Swift (MC...

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.

Kyle Fuller

April 22, 2016
Tweet

More Decks by Kyle Fuller

Other Decks in Technology

Transcript

  1. 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
  2. How

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

    -> String { let formatter = NSDateFormatter() formatter.formatString = "DD MM yyyy" return formatter.stringForDate(date) }
  4. $ swift --version Apple Swift version 2.2 $ cd Stencil

    $ swift --version Apple Swift version 3.0-dev
  5. Custom Parameter Type extension Status : ParameterConvertible { init?(parser: ParameterParser)

    { switch parser.shift() ?? "" { case "open": self = .Open case "closed": self = .Closed default: return nil } } }
  6. Swift Safety get("/users/:username") { (request, params) in let username =

    params["username"]! return "Hello \(username)" }
  7. 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()
  8. $ 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
  9. There are {{ articles.count }} articles. {% for article in

    articles %} - {{ article.title }} by {{ article.author }}. {% endfor %}
  10. 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"])
  11. 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") }
  12. 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" } }
  13. class PersonTests: XCTestCase { let person = Person(name: "Kyle") func

    testPersonName() { XCTAssertEqual(person.name, "Kyle") } func testPersonDescription() { XCTAssertEqual(person.description, "Kyle") } }
  14. extension PersonTests: XCTestCaseProvider { var allTests : [(String, () throws

    -> Void)] { return [ ("testPersonName", testPersonName), ("testPersonDescription", testPersonDescription), ] } } XCTMain([ PersonTests(), ])
  15. $ cat Package.swift import PackageDescription let package = Package( name:

    "Hello", dependencies: [ .Package(url: "https://github.com/nestproject/Frank.git", majorVersion: 0, minor: 3), ] )
  16. $ cat Sources/main.swift import Frank get { _ in return

    "Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }
  17. $ 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'
  18. 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