Backend Development with Swift
A Vapor Case Study
The Swift language has been announced in 2014 by Apple and has grown very well so far. To me, it’s one of the most promising general-purpose programming languages for the future. Strictly speaking, I would prefer using Swift rather than Java for backend development. Since it’s an opinion and experience-based topic I choose to talk about it later.
Today we’re going to write a simple backend service using Vapor, a framework written in Swift in order to make the developers use Swift for web API development. I don’t want to discuss the features, pros, and cons right now. I’m going to show you how to write the simplest web API, configure, and run.
Let’s get started
We’re going to use Xcode for this tutorial. If you don’t have macOS, you are still able to follow this tutorial. The code is going to be very simple so you can even use Vim as long as you know how to quit it.
The first requirement is to have Swift on your Mac or Linux machine. If you have Xcode you already have Swift. However, in case you use a Linux machine follow the instructions here.
We’re going to get Vapor Toolbox now. It’s a command-line interface, a.k.a CLI, tool which allows you to create new apps. It has more features for sure. We’re going to use it only for creating our app.
If you have Homebrew run the following commands:
brew tap vapor/tap
brew install vapor/tap/vapor
In case you don’t have it follow the instructions on its web page.
You can see if the Vapor has been installed smoothly using the help command as in the ss I attached here.
Here are the commands we’re going to use today: new, build, run, and, Xcode. Please check the other commands yourself. 😉
The new command has a number of options. We don’t need any options right now. Just enter the command below:
vapor new <Project Name>
Let me explain the files created by default with the new command. The files we should edit are placed inside the Sources directory. The configure.swift holds the database configuration at its simplest form. If you check the file you’re going to see the database.add() line. By default, the database is set to hold the data in memory so if you restart the service you’ll lose everything you saved at the database. The first thing should be editing this file. The routes.swift holds the paths and associate a method defined in controllers. The other two directories have the controllers and models.
I urge you to use Xcode from here. However, you can edit the code with any tool you like as I said before. Though the ones who like to use Xcode should enter the command below inside the project directory in order to have an Xcode project:
vapor xcode
When everything is OK the CLI will ask you to open Xcode. Say yes.
Make sure that Run on My Mac is chosen. When you see the message below in error log then it means that everything is ok. Those who don’t use Xcode should use the commands vapor build and vapor run respectively.
Server starting on http://localhost:8080
You can navigate to localhost:8080 to double-check.
Now we’re going to make simple changes in order to understand the structure. As the very first edit, change the storage from memory to file by .file(path: “<FilePath>”) in configure.swift. We’re going to add a new model called Camera. Create a new file called Camera.swift under the Models directory. The code of it should look like the below:
BTW, you can delete the default ones inside both the Controllers and the Models directories. Now create a file named CameraController inside the Controllers directory. The code of it should look like the below:
It’s time to define the corresponding paths. We’re going to have only one path: /camera. When we send a GET request to this path we’re going to be given a list of all the cameras. When we send a POST request with the corresponding parameters we’re going to create a new camera entry in the database. Edit the routes.swift as below:
The last thing we should do is to edit the configure file. We use object-relational mapping, a.k.a ORM, and need to tell about the migrations to see the corresponding tables and updates, if there are, in the database. Add the line, migrations.add(model: Camera.self, database: .sqlite), under the Migrations section in the configure.swift file. Delete any unused migration add line.
After running the server enter https://localhost:8080/camera. If you see an empty array everything seems to be ok. Now try adding some entries into the database and test if everything is perfect 👌