A tutorial for using IBM Cloud Runtime for Swift

Using Swift for both client and server side apps has never been simpler. With the combination of Swift 3.0’s release including new server-side capabilities, IBM’s high performing, open source Kitura web framework for Swift and the IBM Cloud Runtime for Swift, developers can now stand up the microservice in two simple steps:

1. Create a new Swift backend application on IBM Cloud (5 minutes)

2. Clone the Kitura-Starter app as a starting point for your app and deploy it to IBM Cloud (5 minutes)

First some basics. This tutorial uses buildpacks that examine user-provided artifacts to determine which dependencies to download and how to configure applications to communicate with bound services. Cloud Foundry buildpacks provide build and runtime support for applications in different frameworks. We’ll be using the IBM Swift buildpack, based on Cloud Foundry, to demonstrate how you can build and deploy your Swift 3 applications and microservices using Kitura on IBM Cloud.

Create a new Swift backend application on IBM Cloud (5 minutes)

  • Launch IBM Cloud. If you don’t already have an IBM Cloud account, sign up for a free trial.
  • Click on Compute, then on Cloud Foundry Applications.
    1
  • Select Swift. The IBM Cloud Runtime for Swift integrates the latest IBM Swift buildpack with a sample application built with Kitura.
    3
  • Enter a unique name for your sample app and create it. This unique name will be used with the IBM Cloud domain name to access your sample app from a browser. (for example, http://my-awesome-swift-app.bluemix.net)
    4
  • IBM Cloud will download the IBM Swift buildpack and the Kitura starter app from the IBM Swift and IBM Cloud Github repos, including all the dependent packages, then build and deploy it. Once the app shows as running click on View App to launch the sample Swift Kitura app.
    5

Clone the Kitura-Starter app as a starting point for your app and deploy it to IBM Cloud (5 minutes)

  • We will use the same sample app used by the IBM Cloud Runtime for Swift as a starting point and customize it.
  • Note: These instructions assume you have git and the Swift runtime installed on your machine.
  • Clone the Kitura-Starter git repo: git clone https://github.com/IBM-Bluemix/Kitura-Starter.git
  • Go to the Kitura-Starter directory and verify the contents of .swift-version. If required, change the contents of the file to 3.0 or whichever version of Swift you want to use.
  • The hello world microservice in the file: Sources/main.swift is as simple as this:
    // Basic GET request
    router.get("/hello") { _, response, next in
      Log.debug("GET - /hello route handler...")
      response.headers["Content-Type"] = "text/plain; charset=utf-8"
      try response.status(.OK).send("Hello from Kitura-Starter!").end()
    }
    
    // Basic POST request
    router.post("/hello") { request, response, next in
      Log.debug("POST - /hello route handler...")
      response.headers["Content-Type"] = "text/plain; charset=utf-8"
      if let name = try request.readString() {
        try response.status(.OK).send("Hello \(name), from Kitura-Starter!").end()
      } else {
        try response.status(.OK).send("Kitura-Starter received a POST request!").end()
      }
    }
  • Customize this file as needed for your microservice or plug in your application code here instead.
  • Make sure you put any new dependencies introduced by your application in Package.swift
  • Optional, but recommended: Compile and execute the app locally using make run
  • Follow the instructions in the Getting Started tab on your IBM Cloud app page to deploy your Swift app:

    bluemix api [API_ENDPOINT]
    bluemix login -u [USER] -o [ORG] -s [SPACE]
    cf push [APP_NAME]
  • You should see something like this:
    cf_push
  • Go to the URL for your app shown at the end of the deploy and test your app running on Kitura in IBM Cloud!
    6

    $curl -X POST -d "to my little friend" https://my-awesome-swift-app.stage1.mybluemix.net/hello
    Hello to my little friend, from Kitura-Starter!
    

Learn more about the IBM Swift buildpack and other components of Swift@IBM by visiting our Dev Center at https://developer.ibm.com/swift/.

1 comment on"Stand up your Swift 3 microservice in less than 10 minutes using IBM Cloud Runtime for Swift"

  1. kfbishop@us.ibm.com October 17, 2016

    Updated post to include new location for Bluemix Kitura Starter sample app — https://github.com/IBM-Bluemix/Kitura-Starter

Join The Discussion

Your email address will not be published. Required fields are marked *