To get started, we'll take you through a sample Swift hello world application that takes only few minutes to deploy.
You'll need a Bluemix account and the following tools. If you do not have the tools follow the links to download them.
- Git
- Cloud Foundry CLI
- Swift compiler for your platform.
Now you're ready to start working with the simple Swift app. Clone the repository and change to the directory to where the sample app is located.
git clone https://github.com/IBM-Bluemix/get-started-swift
cd get-started-swift
Peruse the files in the get-started-swift directory to familiarize yourself with the contents.
Once you have installed the Swift compiler and cloned this Git repository, you can now compile and run the application. Go to the root folder of this repository on your system and issue the following command:
swift build
This command might take a few minutes to run.
Once the application is successfully compiled, you can run the executable that was generated by the Swift compiler:
.build/debug/kitura-helloworld
You should see an output similar to the following:
Server is listening on port: 8080
View your app at: http://localhost:8080
To deploy to {{site.data.keyword.Bluemix_notm}}, it can be helpful to set up a manifest.yml file. One is provided for you with the sample. Take a moment to look at it.
The manifest.yml includes basic information about your app, such as the name, how much memory to allocate for each instance and the route. In this manifest.yml random-route: true generates a random route for your app to prevent your route from colliding with others. You can replace random-route: true with host: myChosenHostName, supplying a host name of your choice. Learn more...
applications:
- name: GetStartedSwift
random-route: true
memory: 256M
You can use the Cloud Foundry CLI to deploy apps.
Choose your API endpoint
cf api <API-endpoint>
Replace the API-endpoint in the command with an API endpoint from the following list.
URL | Region |
---|---|
https://api.ng.bluemix.net | US South |
https://api.eu-gb.bluemix.net | United Kingdom |
https://api.au-syd.bluemix.net | Sydney |
Login to your {{site.data.keyword.Bluemix_notm}} account
cf login
From within the get-started-swift directory push your app to {{site.data.keyword.Bluemix_notm}}
cf push
This can take a minute. If there is an error in the deployment process you can use the command cf logs <Your-App-Name> --recent
to troubleshoot.
When deployment completes you should see a message indicating that your app is running. View your app at the URL listed in the output of the push command. You can also issue the cf apps
command to view your apps status and see the URL.
Next, we'll add a NoSQL database to this application and set up the application so that it can run locally and on {{site.data.keyword.Bluemix_notm}}.
- Log in to {{site.data.keyword.Bluemix_notm}} in your Browser. Browse to the
Dashboard
. Select your application by clicking on its name in theName
column. - Click on
Connections
thenConnect new
. - In the
Data & Analytics
section, selectCloudant NoSQL DB
- Select a pricing plan. Bluemix offers free
Lite
plans for a select collection of its cloud services with enough capacity to get you started - Select
Restage
when prompted. Bluemix will restart your application and provide the database credentials to your application using theVCAP_SERVICES
environment variable. This environment variable is only available to the application when it is running on Bluemix.
Environment variables enable you to separate deployment settings from your source code. For example, instead of hardcoding a database password, you can store this in an environment variable which you reference in your source code. Learn more...
We're now going to update your local code to point to this database. Create a json file that will store the credentials for the services the application will use. This file will get used ONLY when the application is running locally. When running in {{site.data.keyword.Bluemix_notm}}, the credentials will be read from the VCAP_SERVICES environment variable.
- Create a file called
config.json
in theSources
directory with the following content (see config.json.example):
{
"vcap":{
"services":{
"cloudantNoSQLDB":[
{
"credentials":{
"host":"<host>",
"password":"<password>",
"port":443,
"url":"<url>",
"username":"<username>"
},
"label":"cloudantNoSQLDB",
"name":"cloudantService"
}
]
}
}
}
This sample application uses the Swift-cfenv package to interact with Bluemix to parse environment variables. Learn more...
-
Back in the {{site.data.keyword.Bluemix_notm}} UI, select your App -> Connections -> Cloudant -> View Credentials
-
Copy and paste just the credentials to fields in your local config.json file.
-
Run your application locally.
swift build
.build/debug/kitura-helloworld
View your app at: http://localhost:8080. Any names you enter into the app will now get added to the database.
This sample application uses the Kitura-CouchDB package to interact with Cloudant. Learn more...
- Make any changes you want and re-deploy to {{site.data.keyword.Bluemix_notm}}!
cf push
View your app at the URL listed in the output of the push command, for example, myUrl.mybluemix.net.