At InterConnect IBM announced and demonstrated various ways to run Swift code on the server. You can use the Swift runtime in Bluemix (Cloud Foundry buildpack), you can use the Swift Sandbox, you can write event based logic with OpenWhisk and you can run Swift in Docker containers on Bluemix. With these capabilities iOS developers can now write server side code in the same language used for mobile apps.
In order to build REST APIs with Swift you need a web framework. For Node.js applications typically the Express framework is used. The counterpart for Swift is Kitura. Here is how my colleague Robert Dickerson describes Kitura.
Kitura is a lightweight web framework that allows you to easily build web services with complex routes. Much of its design was inspired by Express.js based on the success of its overall design in particular URL routing and pluggable middleware. Kitura takes these principles and adds the advantages of Swift.
Robert wrote a great tutorial how to use Kitura and run it locally. I tried to package up his ToDo sample in a Docker image and run it as container so that you can deploy the application to Bluemix. Turns out that at this point you need to do one extra step before you can run the ibmcom/swift-ubuntu image as Robert told me.
Download pcre and unpack it into a sub-folder ‘pcre2-10.20’ of your project. After this add this Dockerfile to the root.
1 2 3 4 5 6 7 8 9 10 FROM ibmcom/swift-ubuntu:latest EXPOSE 8090 USER root RUN apt-get install -y libhttp-parser-dev libcurl4-openssl-dev libhiredis-dev COPY ./pcre2-10.20 /root/ RUN cd /root && ./configure && make && make install RUN cd /root && mkdir swift-helloworld COPY . /root/swift-helloworld/ RUN cd /root/swift-helloworld && swift build -Xcc -fblocks -Xswiftc -I/usr/local/include -Xlinker -L/usr/local/lib CMD ["/root/swift-helloworld/.build/debug/TodoList"]
To build the image and run the container invoke these commands.
1 2 docker build -t swift-todos . docker run --name swift-todos -p 8090:8090 -d -t swift-todos
After this the REST APIs can be invoked via ‘http://dockerhost:8090’. To deploy it to Bluemix follow the documentation.