Server-Side Swift with Vapor

I recently took the server-side Swift framework Vapor for a spin and want to share the experience! I will show you how to build your first server written in Swift and ready for deployment!

Server-Side Swift

Swift has been touted as a multipurpose language since its introduction, but it was originally used for most of the same client-side applications as Objective-C. Since Swift was open-sourced, developers have started releasing all sorts of projects for other platforms.

Vapor Introduction

For my first Swift server, I chose to use the Vapor framework because it is open-source and has an energetic and active group of contributors. I even contributed a few pull requests as I found bugs in pre-release versions. It has been a really great learning experience to participate in the development as it reached version 1.0 alongside the release of Swift 3.0.

Server-side Swift if very exciting becauase it enables the possibility of building client-side and server-side with the same language. And unlike Node.js, Vapor uses a strongly-typed language focused on reducing programmer error. It is also exciting to use Xcode for both client and server development!

Installing Vapor

1) Install Xcode 8 which comes with Swift 3.0
2) Check that your system is compatible with Vapor using this terminal command:

curl -sL | bash

3) Install the Vapor toolbox using terminal commamd:

curl -sL | bash

4) If everything went smoothly, congrats Vapor is installed!

Your First Vapor Server

1) The Vapor toolbox provides a standard template for a new project. Clone the standard template project using the following terminal command:

vapor new first-project

2) Vapor makes use of the standard Swift Package Manager. Each project includes a Package.swift file that tells the package manager what other repos are dependencies fo the current project. The Swift Package Manager will clone each dependency repo into its Packages folder. Each dependency repo also has its own Package.swift file that lists is dependenceis. These dependencies will also be automatically downloaded and this process will continue recursively until every dependency is cloned.

In the template project’s Package.swift, you will see the Vapor framework is included a dependency. You can add additional Swift packages to your project in the future. For now, let’s fetch all of the package dependencies using the command:

vapor fetch

3) At this point, you can go either build and start a server from the command-line using:

vapor build
vapor run

or you can do something even cooler and create an Xcode project and benefit from all of the great features of the IDE (i.e autocompletion). To generate the Xcode project, run the command:

vapor xcode

Then simply open up the new .xcodeproj in Xcode and start your server by selecting the App scheme and hitting the run button. The Xcode console should show the address and port the server is running on like so:

Now open up your browser to that ip address and you should something similiar to:

You can see this Vapor template project on Github.

Explore The Vapor Documentation

Vapor is a full-featured server framework with support for RESTful APIs, authentication, templating, routing, middleware and database ORMs. Check out the Vapor documentation for more information. They also link to several examples as well as instructions for deploying to Heroku, Docker, Digital Ocean, AWS, and more.