startrekor / swift-helloworld

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#Swift HelloWorld App Overview This project contains a simple Swift hello world application that can be deployed to Bluemix or run locally on your OS X or Ubuntu Linux system. This sample application creates a basic server that returns an HTML greeting to the client. Please note that this is not a production-ready application. Instead, it is for educational purposes to learn about the types of applications you can develop using the Swift programming language.

Application Requirements

To compile and run this sample application on your system, you need to install the Swift compiler for your platform. Please note that the Swift language is evolving and changing rapidly. We have tested and executed this sample application using several of the different Swift versions that have been released such as 2.2-SNAPSHOT-2015-12-10-a and DEVELOPMENT-SNAPSHOT-2016-02-08-a.

We recommend downloading the DEVELOPMENT-SNAPSHOT-2016-02-08-a version of the Swift binaries, which is the latest version of Swift at the time of writing. You can follow the instructions provided on the Download Swift page for your platform.

If you are interested in manually deploying the application to Bluemix, you'd need to install the Cloud Foundry command line on your system. Once it is installed, you can use it to authenticate and access your Bluemix organization(s) and spaces. You can find further details on how to deploy this sample application to Bluemix in the following sections.

Run the app locally

Once you have installed the Swift compiler and cloned this Git repo, you can now compile and run the application. Go to the root folder of this repo on your system and issue the following command:

$ swift build

You should see an output similar to the following:

Compiling Swift Module 'Utils' (5 sources)
Linking Library:  .build/debug/Utils.a
Compiling Swift Module 'Server' (1 sources)
Linking Executable:  .build/debug/Server

Once the application is successfully compiled, you can run the executable that was generated by the Swift compiler:

$ .build/debug/Server

You should see an output similar to the following:

Server is listening on port: 8000

To connect to the server, you can use the browser of your preference (e.g. Firefox, Chrome, etc.) to point to the following URL: http://<hostname>:8000/, where <hostname> is the hostname or the IP address of the system where you are running the sample app. If the browser is running on the same system, you can use localhost as the value for the hostname. After you access the server, the browser should render an HTML page with the following message:

Hello from Swift on Linux!

Running the app on Bluemix

Using the magical button

Click the magical button below to automatically deploy this sample application to Bluemix.

Deploy to Bluemix

When automatically deploying to Bluemix, the manifest.yml file [included in the repo] is parsed to obtain the name of the application. For further details on the structure of the manifest.yml file, see the Cloud Foundry documentation.

Using the Cloud Foundry command line

You can also manually deploy the app to Bluemix. Though not as magical as using the Bluemix button above, manually deploying the app gives you some insights about what is happening behind the scenes. Remember that you'd need the Cloud Foundry command line installed on your system to deploy the app to Bluemix.

Using the Cloud Foundry command line you can get a list of the buildpacks (along with their versions) that are installed on Bluemix.

cf buildpacks

Executing the above command should result in output similar to the following:

Getting buildpacks...

buildpack                                 position   enabled   locked   filename   
liberty-for-java                          1          true      false    buildpack_liberty-for-java_v2.5-20160209-1336.zip   
sdk-for-nodejs                            2          true      false    buildpack_sdk-for-nodejs_v3.0-20160125-1224.zip   
noop-buildpack                            3          true      false    noop-buildpack-20140311-1519.zip   
java_buildpack                            4          true      false    java-buildpack-v3.5.1.zip   
ruby_buildpack                            5          true      false    ruby_buildpack-cached-v1.6.7.zip   
nodejs_buildpack                          6          true      false    nodejs_buildpack-cached-v1.5.0.zip   
go_buildpack                              7          true      false    go_buildpack-cached-v1.6.2.zip   
python_buildpack                          8          true      false    python_buildpack-cached-v1.5.1.zip   
php_buildpack                             9          true      false    php_buildpack-cached-v4.1.5.zip   
swift_buildpack                           10         true      false    swift_buildpack-v1.0.3.zip   
aspnet5-experimental                      11         true      false    buildpack_aspnet5-experimental_v0.7-20151022-1257.zip   
xpages_buildpack                          12         true      false    xpages_buildpack_v9.0.1-bmix-pb-20151217-8000.zip   
aspnet5-experimental_v0_6-20150916-1220   13         true      false    buildpack_aspnet5-experimental_v0.6-20150916-1220.zip   
liberty-for-java_v2_3-20151208-1311       14         true      false    buildpack_liberty-for-java_v2.3-20151208-1311.zip   
sdk-for-nodejs_v2_8-20151209-1403         15         true      false    buildpack_sdk-for-nodejs_v2.8-20151209-1403.zip   

Looking at the output above, we can see that Swift buildpack is installed on Bluemix. This will allow a seamless deployment of the starter application to Bluemix. After you have cloned this Git repo, go to its root folder on your system and issue the following command Cloud Foundry command:

cf push

Executing the Cloud Foundry push command will parse the contents of the manifest.yml file and upload the application to Bluemix. This action should generate an output similar to the following:

Using manifest file /Users/olivieri/git/swift-helloworld/manifest.yml

Creating app swift-helloworld in org john_doe@us.ibm.com / space dev as john_doe@us.ibm.com...
OK

Creating route swift-helloworld.stage1.mybluemix.net...
OK

Binding swift-helloworld.stage1.mybluemix.net to swift-helloworld...
OK

Uploading swift-helloworld...
Uploading app files from: /Users/olivieri/git/swift-helloworld
Uploading 553.9K, 76 files
Done uploading               
OK

Starting app swift-helloworld in org john_doe@us.ibm.com / space dev as john_doe@us.ibm.com...
-----> Downloaded app package (256K)
-----> Buildpack version 1.0.3
-----> Installing Swift DEVELOPMENT-SNAPSHOT-2016-02-08-a
       Downloaded Swift
-----> Installing Clang 3.7.0
       Downloaded Clang
-----> Building Package
       Compiling Swift Module 'Utils' (5 sources)
       Linking Library:  .build/release/Utils.a
       Compiling Swift Module 'Server' (1 sources)
       Linking Executable:  .build/release/Server
-----> Copying dynamic libraries
-----> Copying binaries to 'bin'
-----> Cleaning up build files

-----> Uploading droplet (3.8M)

0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App swift-helloworld was started using this command `Server -bind 0.0.0.0:$PORT`

Showing health and status for app swift-helloworld in org john_doe@us.ibm.com / space dev as john_doe@us.ibm.com...
OK

requested state: started
instances: 1/1
usage: 128M x 1 instances
urls: swift-helloworld.stage1.mybluemix.net
last uploaded: Thu Feb 11 16:25:08 UTC 2016
stack: cflinuxfs2
buildpack: Swift

     state     since                    cpu    memory         disk        details   
#0   running   2016-02-11 10:27:43 AM   0.0%   6.4M of 128M   14M of 1G      

Once the sample application is pushed to Bluemix, you can access it using its route. You can log on to your Bluemix account to find the route of your application or you can inspect the output from the execution of the cf push command. The string value (e.g. swift-helloworld.stage1.mybluemix.net) shown next to the urls should contain the route. Use that route as the URL to access the sample server using the browser of your choice. The browser should render an HTML page with the following message:

Hello from Swift on Linux!

Using a different version of Swift on Bluemix

If you look closely at the output above returned by the cf push command, you will notice that DEVELOPMENT-SNAPSHOT-2016-02-08-a was the Swift version used for compiling and running the sample app on Bluemix. If you would like to use a different version of the Swift language on Bluemix, say 2.2-SNAPSHOT-2015-12-10-a, you'd need to update the contents of the .swift-version file to:

swift-2.2-SNAPSHOT-2015-12-10-a

After updating the .swift-version file, you can run the cf push command one more time. This should upload the application to Bluemix and use the 2.2-SNAPSHOT-2015-12-10-a version of the Swift binaries for compiling and running the starter application as shown below:

Using manifest file /Users/olivieri/git/swift-helloworld/manifest.yml

Updating app swift-helloworld in org ricardo.olivieri@us.ibm.com / space dev as ricardo.olivieri@us.ibm.com...
OK

Uploading swift-helloworld...
Uploading app files from: /Users/olivieri/git/swift-helloworld
Uploading 555.9K, 75 files
Done uploading               
OK

Stopping app swift-helloworld in org ricardo.olivieri@us.ibm.com / space dev as ricardo.olivieri@us.ibm.com...
OK

Starting app swift-helloworld in org ricardo.olivieri@us.ibm.com / space dev as ricardo.olivieri@us.ibm.com...
-----> Downloaded app package (256K)
-----> Downloaded app buildpack cache (258M)
-----> Buildpack version 1.0.3
-----> Installing Swift 2.2-SNAPSHOT-2015-12-10-a
       Downloaded Swift
-----> Building Package
       Compiling Swift Module 'Utils' (5 sources)
       Linking Library:  .build/release/Utils.a
       Compiling Swift Module 'Server' (1 sources)
       Linking Executable:  .build/release/Server
-----> Copying dynamic libraries
-----> Copying binaries to 'bin'
-----> Cleaning up build files

-----> Uploading droplet (3.4M)

0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App swift-helloworld was started using this command `Server -bind 0.0.0.0:$PORT`

Showing health and status for app swift-helloworld in org ricardo.olivieri@us.ibm.com / space dev as ricardo.olivieri@us.ibm.com...
OK

requested state: started
instances: 1/1
usage: 128M x 1 instances
urls: swift-helloworld.stage1.mybluemix.net
last uploaded: Thu Feb 11 20:22:04 UTC 2016
stack: cflinuxfs2
buildpack: Swift

     state     since                    cpu    memory         disk          details   
#0   running   2016-02-11 02:25:52 PM   0.0%   4.9M of 128M   12.6M of 1G      

For a complete list of the Swift versions supported by the Cloud Foundry Swift buildpack, see the buildpack's manifest file.

What's next?

Once a new official release of the Cloud Foundry Swift buildpack is available, we will also make it available on Bluemix. Also, our team is currently working on a much more advanced version of this HTTP server that will be available soon. We also plan to provide Swift packages that will allow developers communicate with middleware services such as CouchDB and Cloudant. Stay tuned for updates!

About

License:Apache License 2.0


Languages

Language:Swift 100.0%