Getting Started with Server Side Swift Using Kitura
--
Server side Swift is gaining momentum in the iOS development community and the time is near when we will break the shackles of other programming languages and frameworks and implement everything in our beloved Swift language.
There are several server side Swift frameworks available but the three of the most popular ones are as follows:
- Vapor
- Kitura
- Perfect
I covered the Vapor framework sometime back. This post talks about how you can get started with Kitura, which is a server side Swift framework from IBM.
Prerequisites:
- Xcode 8 or above
- Swift 3.0
Installing Kitura Package Using Swift Package Manager:
The first step is to install Kitura package using Swift Package Manager. Before we do that you need to initialize Swift Package Manager. Create a folder called “hello-kitura” and go inside that folder using terminal. Once, inside the folder initialize the Swift package as shown below:
Run the “swift build” command to build the code as shown below:
After the build is completed you can run the executable using the following command.
Hello World indeed! When you executed the code it ran all the code in “main.swift”. If you open “main.swift” file you will find the following line of code.
print(“Hello World”)
This confirms that your package has been successfully initialized. Now open your Package.swift file which contains all the packages you need use in your application. By default Package.swift will contain the following lines of code.
import PackageDescriptionlet package = Package(
name: “hello-kitura”
)
As, you can see the Package.swift file does not refer to any packages or dependencies. Let’s fix that and add a dependency on the Kitura project. Along with Kitura we also added a dependency on HeliumLogger. HeliumLogger is used to log events generated from Kitura and will be very helpful in debugging requests.
After making the above changes to Package.swift file jump to the terminal and run the following.
swift build
This will trigger the build and download all the dependencies contained in the Package.swift file. When everything is completed you might see something similar to the following.
The final step is to generate the Xcode project so you can start working on your Kitura app in your favorite editor :)
swift package generate-xcodeproj
This will generate Xcode project. You can open Xcode project from terminal using the following command.
open hello-kitura.xcodeproj/
When Xcode opens make sure to select the “App” and not the framework as shown below.
Build your project and make sure everything is good and ready!
Hello Kitura:
It is finally time to get your hands dirty with Kitura! Open “main.swift” file and insert the following code.
Router is the main class in Kitura framework which allows to intercept the requests and responses.
The last two lines configures the Kitura server and runs it. Run your “hello-kitura” project and visit the url “http://localhost:8090/” and you will be delighted to see the Kitura welcome page.
Congratulations you are now Kitura certified!
Let’s take it to the next level and add our custom routes to the Kitura project.
The above code adds a new route “hello” and returns “Hello World” as plain text. The response.end() makes sure that the response has ended. You can invoke the “hello” route using the following url:
http://localhost:8090/hello
And you will get the following result:
The next() closure allows the developer to chain multiple responses together. Eventually next() calls response.end() and makes sure that the response has finished.
I am really excited about the possibilities of running Swift on the server. I believe that this is the future of Swift language, which will take to the next level.
If you find this article interesting then please share and like it :) Happy coding!