Image for post
Image for post
Sample server code using Vapor framework.

Basic Swift Server Setup Using Vapor

timominous
May 17, 2016 · 4 min read

Ever since Apple announced and released the Linux compatible Swift source code, server side Swift has been one of the most active development pursuits. A lot of frameworks have come out including Perfect, Vapor, and Kitura (by IBM). In this blog post, we will discuss how easy it is to make a basic setup using Vapor.

Vapor is a web framework for Swift developed by the collaborators from Qutheory. It boasts of its simplicity, type safety, and speed. The framework gets updated frequently to work with the latest Swift 3 snapshots. Currently, development testing are being done on OSX and Ubuntu (14.04 and 15.10).

We will setup a server that will run locally using the latest Vapor release as of writing.

Installing Swift 3

For easier installation, we are going to use swiftenv which is a Swift version manager similar to pyenv and rbenv. Visit their github page for more detailed installation and usage instructions. For now, we are installing the latest snapshot Vapor supports by doing the following in the terminal.

This command downloads and installs the May 3, 2016 Swift 3 snapshot.

Installing the Vapor CLI

Vapor’s command line tool provides a lot of convenience when handling Vapor projects.

Updating the CLI is as simple as

Creating the server

Using the CLI, we will be able to create a basic server using simple commands.

Running the given commands will setup a basic server template and run it. If you visit http://localhost:8080 on your browser you will see something like this.

Image for post
Image for post
Vapor sample landing page.

It’s a simple project example which you can then modify to suit your server needs. Vapor even provides examples by clicking on the links on the landing page.

Custom Routes

Checking the project directory, we can see that a lot of files and subdirectories were created including VaporApp.xcodeproj. Double-click it to open it in Xcode. Make sure that you are on the correct Development Snapshot toolchain in Xcode.

Image for post
Image for post
Xcode toolchain selection

The project should have the following file structure. Open main.swift to create a custom application route.

Image for post
Image for post
Xcode project hierarchy

Just beneathe the line where the application was instantiated, we are going to create a custom route named greeting.

We need to halt (CTR +C) the current running server and rebuild the app to integrate our current changes.

The currently running server should reflect the changes we have made to the code.

Image for post
Image for post

Now, we’ll implement a route that gets the user’s name. Notice how we are passing another argument besides the route name to indicate that we are expecting a String parameter.

Rebuild and rerun the server and you should get the following output.

Image for post
Image for post

Final Thoughts

Setting up a Swift 3 server using Vapor is very simple and elegant. We’ve barely scratched the surface of what the Vapor can do. To be able to understand more about what you can do with Vapor and how it works, you should visit their github page and official documentation.

Vapor and all the other libraries under Qutheory are in a very active development stage. They are also inviting other developers to contribute to the codebase since everything is open sourced on github.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store