Getting Started with Server-Side Swift Using the Vapor Web Framework

Ever since Apple open-sourced Swift, the developer community saw a greater potential for the language than mere client-side iOS development. That community envisioned a language that could become more universal and cross-platform, as evidenced by the emergence of server-side Swift powered by the Vapor Web Framework.

Why code your back-end in Swift?

One of the most burning of questions is, "Why have Swift on the server in the first place?" True, we already have great and well-established server frameworks out there, like Node.js and Rails, so why invest in a platform that may not be as mature as those other platforms? You get a lot of added benefits, for starters.

Leveraging the same technology on your backend as on the iOS client means you can use the same team on both ends, adding greater knowledge transfer and knowledge redundancy and lowering cost-of-maintenance through additional resources.

Having a Swift back-end and front-end means that you can divide feature development according to features instead of technology stack. While one developer will own the entire registration process, another developer owns the entire activity feed feature—that is, you get vertical rather than horizontal delegation and ownership.

Swift has amazing potential on the server, and to be an even better language for server-side development Swift needs great low-level APIs common among server frameworks. The Server APIs project will provide core capabilities in areas such as networking and security, so Swift programs no longer need to rely on platform-specific C libraries frequently to provide this functionality. As a result, developers will be able to create frameworks and server applications using pure-Swift code, without the need to also have systems programming skills and knowledge of multiple platforms. (Source: Swift.org)

Getting Started with Server-Side Swift, Using Vapor

While it's still early days for server-side Swift technology, Vapor is actually one of three emergent server-side Swift frameworks, with the others being Perfect and Kitura. Perfect was one of the first Web server toolkits for Swift, and Kitura (as featured during an Apple 2016 WWDC Server session) is backed by IBM. My framework of choice for this tutorial is Vapor.

Vapor is a modern server-side platform using HTTP or Web Sockets with some claiming that it is almost 100 times faster than other comparable Web frameworks, such as Ruby and PHP.

The Architecture of the Vapor Application

In this example, you're going to build a RESTful API (a simple one, mind you) that will return the list of Apple Stores in the Bay Area, based on a simple HTTP GET call to your database. In the final steps of this tutorial, you'll push your API project to a MongoDB-driven stack on Heroku, but you can just as easily plug in any other database platform, like MySQL or CouchDB.

Download the entire example tutorial project.

To build your test application, you'll first install the Vabor development tools onto a Mac and then build out the server side using Heroku.

Setting up Heroku

In order to get yourself setup, the first thing you need to do is sign up for a Heroku account. Next, in order to interact with Heroku, you will need to install the Heroku toolbelt, using Homebrew in terminal, which you should install if you don't already have it on your system. To do that, fire up terminal and enter the following:

> brew install heroku-toolbelt

Then you'll login via the terminal to heroku, as follows:

Login via the terminal to Heroku

The installation seems to happen by magic—essentially homebrew installs it in Terminal, so you don't need to do anything beyond those steps. If the installation runs without any hitches, you should be done with this first step, which is setting up your server. You could have used a Linux server via AWS or a Docker instance, but Heroku is an easy way to set up a project. Next, you'll install the Vapor Web Framework.

Installing Vapor

Vapor's excellent documentation clearly describes how to set up the Swift and Vapor development environment, whether it's on your Mac or on Ubuntu Linux (it is not available for Windows).

Doron Katz A keen passion for emerging technologies, practices and methodologies, Doron embraces the vision of lean development with continuous customer development. Consultant for various startups, as a Project and Product Manager, with a mobile engineering background in iOS, and over 10 years of professional web development experience.
 

Comments

Comments(2)

phani2

Hi , am able to successfully create the user but am not able to acces the console for stores db with the following command  >mongo -u myTester -p xyz123. I get this error  E QUERY    [main] SyntaxError: missing ; before statement @(shell):1:9

Am new to mongodb, any help appreciated. Thanks