Getting started with server-side Swift with Vapor

Aditya Shinde
Mac O’Clock
4 min readMay 2, 2020

--

In 2014 WWDC Apple introduced Swift, a brand new programming language with which we can write apps for iOS and Mac OS. Writing code in Swift is great fun and I simply love this language. In 2016 Apple also announced Swift support for Linux and we can write server-side code with swift.

Being an iOS developer I was very curious about how the server code is written, especially the web services which we consume in our mobile apps. This curiosity drives me to learn more about server-side swift.

There are multiple frameworks available to write server codes with swift like Kitura(developed by IBM), Perfect, Vapor, etc. We will be writing server-side code in swift with Vapor.

Why Vapor?

  • Fluent: Fluent is an ORM provided by Vapor. With Fluent we can create Models for tables in the database. We can query tables as very similar as we query our entities in Core-Data in iOS. Due to ORM, you won’t have to write direct database queries.
  • Continuous evolution: We get continuous updates of this framework with new Swift versions. Vapor 3.0 was completely re-written to support Apple’s non-blocking networking framework Swift NIO. That means Vapor is a non-blocking framework. You don’t have to worry if 2 API calls are hit by the user at the same time. Vapor itself will take care of that.
  • Huge community: The last and most important reason I recommend Vapor over others is its huge community. Vapor has a huge community on different platforms like Slack, Discord. These are the places where you get instant help.

Install Vapor

  • To install Vapor refer to this link for mac OS. For Linux refers to this link.

First Vapor Project

  • Go to the desired directory path on the terminal and hit command
vapor new Todo

You can see that a new folder has been created at the path you had selected with the project name.

  • Now we have to go to the project directory path. Hit following command on terminal
cd Todo
  • Now hit following command
vapor xcode
  • Now open the project in Xcode and in run scheme select My Mac and build and run the project as shown in the following image.
Select Run Scheme and device My Mac to run the project
  • If you see Server starting on http://localhost:8080. Now hit the localhost URL on the browser and you’ll see the text It works! Congratulations. You’ve run your first Swift server code successfully. Refer following image
Output when you hit localhost URL in browser

Understanding the project structure

  • Package.swift

This file manages all dependencies we need in our project. Here you can see two dependencies, Vapor itself and Fluen-SQLite which is ORM provided by Vapor.

  • Todo.swift

The table will have two columns id and title. You can see Todo is confirming protocols like SQLiteModel, Content, Migration, and Parameter. For now let us only talk about content. Content confirms swift protocol codable. All of us know how codable has made our lives easier to create models from the JSON response. Content does a similar thing here. If your model is confirming content protocol, you don’t have to do anything else to return it as JSON in API response.

  • configure.swift

As its name suggests, everything we need in the app has to be configured here. You can see the last line in the code snippet where we are registering the model. Unless and until you register the model in this file like above, the respective table won’t be created in the database.

  • TodoController.swift

All the operations we have to do with Todo table with the help of web services, we’ll be doing them in this file. Here you can see the functions to fetch all todos and functions to create and delete todos.

  • routes.swift

In this file we call the services with the functions written in the controller with the URL endpoints. When you run the app for the first time, it printed “It works!” on the browser. You can see it in the first route. Now if you append “hello” on the localhost URL, the final URL would be http://localhost:8080/hello and you’ll see “Hello, world!” as an output on the browser. Refer following image

Response of hello world

You can test create, get all, and delete service outputs with REST clients like the postman.

What Next

Here we just explored the template project created by Vapor. In the next article, we’ll create a new table with the PostgreSQL database and will try to perform CRUD operations and we’ll also learn how to query the tables in the database.

Till then, Keep Calm and Keep Coding.

--

--

Aditya Shinde
Mac O’Clock

A Swift enthusiast. Very curious about new trends in technology.