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

Server-side Swift with the Kitura web framework

Server-side Swift with the Kitura web framework

Ian Partridge

June 07, 2016
Tweet

More Decks by Ian Partridge

Other Decks in Programming

Transcript

  1. 1 Server-side Swift with the Kitura web framework Chris Bailey

    Ian Partridge IBM Runtimes Technologies, IBM Cloud 7th June 2016
  2. Chris Bailey • Senior Technical Staff Member • IBM Runtime

    Technologies, IBM Cloud • @Chris__Bailey 2 Ian Partridge • Senior Developer • IBM Runtime Technologies, IBM Cloud • @alfa
  3. Swift@IBM • The team within IBM working to • Accelerate

    MobileFirst development • Bring Swift to the cloud • IBM Swift Package Catalog • Kitura • IBM Swift Sandbox 3
  4. Swift on Linux 4 • Swift on Linux and Darwin

    share: • Language, Standard Library, CoreFoundation, Dispatch • Foundation on Linux is new Swift implementation • Available on Darwin as “SwiftFoundation”
  5. Swift 3.0 API Changes 5 • Swift 3.0 (should) introduce

    new Swift APIs for libraries • New Swift API for Dispatch • New Swift(ier) APIs for Foundation • Implicit bridging being replaced with explicit bridging
  6. Isomorphic Swift Code 9 Create Swift Module Generate Swift Client

    APIs Kitura Server Project Swagger REST API Deploy Deploy • Same module on client and server • Local or remote API calls • Automatically created Swagger APIs
  7. Kitura Web Framework What is it? New, modular, package-based web

    framework written in Swift Why is this cool? Empower a new generation of native mobile developers to write and deploy code into the Cloud. Developer Benefits ? Delivers core technologies needed to stand up enterprise apps on the server Enables developers to create a web application in Swift and deploy these servers on Linux and the Cloud. http://github.com/ibm-swift/kitura 10
  8. Kitura Architecture 11 Web Framework Web Framework Source C Helpers

    & Interfaces Swift Libraries SwiftyJSON Kitura-redis Swift Binary Foundation Package Manager C Libraries Dispatch HttpParser HiRedis CURL PCRE2 Pluggable Components
  9. Developing a Kitura web application 12 myProject ├── Package.swift ├──

    Sources │ └── main.swift └── Tests $ mkdir myProject 2. Next, initialize this project as a new Swift package 1. First, we create a new project directory $ cd myProject $ swift build --init Basic Swift package directory structure:
  10. Developing a Kitura web application 13 import PackageDescription let package

    = Package( name: "myProject", dependencies: [ .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 0, minor: 15) ] ) 3. Add Kitura as a dependency for your project (Package.swift):
  11. Developing a Kitura web application 14 Kitura.addHTTPServer(onPort: 8090, with: router)

    Kitura.run() let router = Router() router.get("/hello") { request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").end() } 6. Create and start an HTTPServer: 4. Add a router and a path: router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } 5. Add a JSON data route
  12. Developing a Kitura web application 15 import Kitura let router

    = Router() router.get("/hello") { request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").end() } router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run() 7. Sources/main.swift should now look like this:
  13. Developing a Kitura web application 16 Linux: swift build -Xcc

    –fblocks Mac OS X: swift build -Xcc -fblocks -Xswiftc -I/usr/local/include -Xlinker -L/usr/local/lib 8. Compile: $ .build/debug/myFirstProject & 9. Run: 10. Open your browser: http://localhost:8090/hello http://localhost:8090/ http://localhost:8090/hello.json
  14. Get started with Kitura • http://github.com/IBM-Swift/Kitura-Sample • `make run` •

    http://github.com/IBM-Swift/Kitura-Starter-Bluemix • `cf push` • http://github.com/IBM-Swift/swift-watson-sdk 17
  15. IBM Swift Package Catalog • https://swiftpkgs.ng.bluemix.net/ • Find, explore and

    share packages from the open-source Swift ecosystem • Explore dependencies • Submit your own packages 18
  16. IBM Swift Sandbox • Interactive sandbox for rapid prototyping and

    experimentation in Swift • Saves your work • Supports multiple versions of Swift • Responsive design 19 https://swiftlang.ng.bluemix.net/