REST API with Swift on Vapor

Valerii Che
4 min readJan 13, 2019

--

Good day everyone!

Recently I was in need to have a server-side storage with some kind of a discovery service for my JSON data files. So I was tearing myself apart:

  • One part of me, as a server-side engineer, was pushing towards having the files stored on Amazon S3 with one more JSON file as a discovery information for the available data;
  • Another part of me, as a client-side engineer on iOS, was craving to find some web server framework in Swift and play with it.

After a bit of fighting with myself, I decided to come up with this post due to the following facts:

  1. Lately I’m mostly busy developing iOS apps in Swift;
  2. I’m actually very keen to see how different is Swift on the server in comparison to more traditional server technologies like Java, JavaScript, etc.

In this post I’m going to implement a simple REST API web server which is able to provide JSON data identified by unique ID, in our case a piece of data will be represented as an animation exported from Adobe After Effects (AAE) as JSON (read as “some JSON object”).

So let’s begin!..

Preparation

After having a quick read of this quite interesting article about most popular Swift based web frameworks such as Kitura, Perfect and Vapor, I decided to go with the latter one due to its rapid development nature aka Ruby on Rails and active community around it. Mind though — I don’t say it’s the best choice.

So let’s create an empty Vapor project. First we need to install Vapor framework and its command line tools following this guidelines.

After we have Vapor installed, creating new project is simple as running the following command in Terminal:

$ vapor new AnimationsProviderServer

It’s also useful to generate Xcode project for easier development of our server:

$ vapor xcode

At this point we should have Xcode project ready to be opened and even ran!

The server will be available on default 8080 port, so you can open “http://localhost:8080” URL in your browser and check if it works.

Short remark: Vapor doesn’t support “hot reload” feature out of the box at the moment of writing this article, but it’s possible to set this up via gulp-vapor package available here on NPMJS.

Voila, now let’s code a little bit!

Data model

Now it’s time to add some logic to the server. Let’s create a model entity to hold information about our animations, for now everything we need to represent an animation is to have its identificator and JSON data exported from Adobe After Effects (AAE). So the animation model may look like this:

Next we need to import animations into the project, to do so simply drag and drop JSON files exported from AAE into Resources folder of the server Xcode project, the project hierarchy should look like this:

Note: Make sure the imported JSON files are added to the “App” target.

Data storage

Somehow we need to access imported animations resources, to achieve that we may want to implement some kind of a storage entity, let’s call it AnimationsStorage:

In order to make AnimationsStorage service available in Vapor controllers, we should register it in configure.swift file:

Basically, treat configure.swift file as source of information about all available global services, utilities, etc.

REST API

Next let’s create a simple controller which returns requested animation by ID as JSON via HTTP GET:

The last step for the server project is to add required routing rules, so the animations are accessible via HTTP. Vapor framework has a separate file for specifying routing tables called routes.swift, in our project it should look similar to this:

Now we have fully working animations provider server, which could be requested by URL via HTTP GET, for instance:

Requesting unavailable animation will result in 404 HTTP code with an empty body.

Note: at this point you may want to restart the web server to make all the changes we’ve made to the code available via REST API.

Conclusions

So we made it to the end! We’ve gone through the process of creating Vapor project from scratch, adding some business functions to the server, deploying and making it available via HTTP.

I wouldn’t say that Swift on Vapor sold me the idea of using it for implementing server solutions after I got to know with such server oriented technologies like Java and Go. But Swift is evolving, and evolving fast, so I’m very curious to see how it will pass the test of time, and what business applications Swift will have apart of client-side development for Apple platforms.

Meanwhile to me it looks like Swift on Vapor is a great starting point for those iOS/macOS engineers who want to become more familiar with server development concepts using the language they are already familiar with.

Thanks for your attention :)

Resources

--

--