Some months ago, I noticed that most iOS developers here at Wolox needed to make regular changes to a ruby-based API, but only a few of them knew how to code in that language. Consequently, making changes to this API always took a considerable amount of effort. Then I thought: wouldn’t it be great if we could build a backend in a language we all knew? That would save us a lot of time and effort!
And then I found Vapor.
In this tutorial we will learn how to set up a basic project with Vapor 3, the most frequently used web framework for Swift. After building the app, we will connect it to a database and, lastly, we will host it on a Heroku server.
So, first of all, we will install the necessary tools to develop this project.
1. Install Xcode
We will need Xcode, so let’s download it from here. Xcode also includes a version of Swift.
2. Install Homebrew
Homebrew is the most complete package manager for macOS. It downloads and installs all the dependencies you will need during this tutorial.
We have also set a buildpack to the Heroku instance. This provides Heroku with the necessary instructions to start and deploy the app.
Next, go to Heroku site, sign in and enter your application dashboard. Click on “Resources” tab and write “Heroku Postgres” in the textbox to include it as an add-on.
You will have to modify configure.swift to use the new Postgres credentials:https://medium.com/media/8985e49ae820d4bba2270ee8bd53bac6
Here, "DATABASE_URL" refers to an environment variable that is available by default on every Heroku instance. As the name implies, the variable holds the URL to connect to the database. Its content may change from time to time, so it’s a good idea to always point to the environment variable instead of using its content directly in your app.
Next, let’s add a new file in the root directory of your project. We will name it Procfile and it will contain the instructions to start the app. This is its content:
web: Run serve --env production --port $PORT --hostname 0.0.0.0
We’re ready to push our app to Heroku. To do so, first commit your changes:
git add . git commit -m "Wolox-users app"
and then run the following command:
vapor heroku push
It may take a while to finish, but while you wait, you can check out the logs in your Heroku Dashboard → Overview → More → View logs.
Done! You can now send requests to your app. Just try the following curl (it should return an empty user list):
We have built an app from scratch, connected it to a database and uploaded it to a Heroku server. Using Vapor, every iOS developer can create their own backend very quickly and with little to no effort.
If you have any feedback or comments, just let me know! I’d really appreciate it! 😉
If Postgres throws an error similar to “could not connect to server”, you may have to uninstall the old version of Postgres and delete some remaining folders before installing the new version. To do that, execute the following commands:
# Uninstall postgres
brew uninstall postgresql
brew cleanup# Remove remaining folders
rm -r /usr/local/var/postgres
rm -r /Users/<username>/Library/Application\ Support/Postgres# Install the last version
brew install postgresql# Start the process
brew services start postgresql
We use analytics to understand the usage, to improve user experience and to measure the performance of our website. We anonymise any information we may collate so we can’t identify you personally.