Swift, Perfect, mustache and PostgreSQL on Heroku — 3

Anatoly Rosencrantz
5 min readMay 3, 2016

--

First step is here, its code here. Previous step is here, its code here.

We’ve already built service that returns JSON of structure defined by mustache template and filled with data received from PostgreSQL database. All this is working on mac, and now it’s time to deploy whole construction to Heroku.

Part 2. Make it work on Heroku

You have to register, sign in, etc to Heroku first. They have different pricing options, and the good news I have is that all we need for this project is available on Free plan.

Before we’ll settle our application to Heroku, we need to manage two things:

  • heroku’s app which is a combination of resources, add-ons, environment variables, deployed code itself, buildpacks for it, associated domain names and lots of other stuff
  • heroku’s database which stands for Postgres server, its configuration and stored data itself

What do we already have? Code to be deployed and data to be stored. Not much, but enough for start.

Step 2.1 Set up Heroku app

Adding new app is pretty easy. Add any unique (throughout Heroku) name you want, as for me, catfoodserver sounds nice:

I’m so lucky — name of my choice was avaliable!

Step 2.1.1 Prepare project for deploy

In Deploy section choose Heroku Git, read instructions below and install the Heroku Toolbelt.

But its too early to Create a new Git repository and Deploy your application yet! First we have to set right structure in project folder, and we’ll do it in a couple of minutes.

Open Finder, go to your project’s folder and make it look like this:

all the stuff should be placed into src folder

But wait… Heroku is running some kind of linux, we do not have friendly Xcode that will build, link, compile and comfort our swifty code there 🤔… No other choice, we’ll make that all manually.

I’m far from linux expert, but as far as I’ve understood deployment process contains four stages:

  1. using heroku git we are pushing our project files to Heroku
  2. buildpack is working with them, placing all dependencies of the project to /app/.delta/usr/ directory on Heroku, so we could work with them later
  3. project is liked, built, installed
  4. executable is run

Step 2.1.2 Buildpack

First, we need to explain Heroku how swift code of our application should be built. In dashboard’s Settings section find Add build pack button and add https://github.com/PerfectlySoft/Perfect-Heroku-Buildpack

comment on the left is rather informative, as all Heroku’s documentation

Than open Terminal and create empty Package.perfect file in src folder:

during deployment process, buildpack will find this file and understand that we are using Perfect framework here

Step 2.1.3 Procfile

Second, we need to tell Heroku which executable should be run after application will be successfully deployed. In the same folder with src folder (not inside src! near it!), create file named Procfile with contents:

yes, throughout this Part, Terminal will be our home. Thug Linux Life.

Step 2.1.4 Makefile

Third, we have to define how code should be linked, compiled and treated after installation. For that reason in 1970s make utility was created, and for using it you should create in src directory file called makefile with contents:

When I’ve first seen this, I thought — I don’t want to build any backend ever. But than I’ve found this awesome tutorial, and strongly recommend to read it to understand at least something in this strange mess of bash and makish commands. Finally, I’ve edited one of Perfect’s example makefiles to support PostgreSQL and multiple swift-files.

Shortly, in this snippet we are explaining make utility on heroku’s linux how PerfectLib and its dependencies should be built, how PostgreSQL lib should be built, which of our swift files are needed and where to place all the libs and mustache stuff during installation (thats the reason I’ve insisted all mustache files to be placed in special folder — to move them all together with mv command). And clean the mess.

Step 2.1.5 Deploy!

By this time your project directory should look like that:

and don’t be freaked out by those russian Сегодня and Вчера on screenshot, they are just Today and Yesterday

If something went wrong (for example, dependencies can be broken), I’ve put project at this stage to step_2.1_heroku_deploy_start branch on github.

Now, as described in Heroku’s Deploy section, lets do:

… and pray. I’ve spent week seeing different errors while experimenting with makefile, so this push for me is strongly associated with:

but if you Have a Clean Heart, Heroku will accept your application. If not — welcome to documentation.

And finally last thing everybody are forgetting to do — tell Heroku to give resources to the app and run it:

Step 2.1.6 Check that all works

Open Safari, put the name of your heroku app (you can see it in Settings section also), slash, catname… What?

I’ve been braking my head on this problem for hours before one wise man reminded me Linux is case-sensitive, so I’ve tried exact same filename as mustache template had and it worked:

Not sure if it is a bug or feature, but whatever… We’ve deployed our service to the cloud! Hooray!

Code up to this stage is in step_2.1_heroku_deploy_start, altho it didn’t change since Step 2.2.5.

Now check the FoodList…

Sure, it shouldn’t work right now cuz we didn’t run PostgreSQL server on Heroku, we didn’t fill paths and username and password of database in FoodList class… And we’ll do it in next steps!

These tutorials are participating in Perfect’s contest. If you’ve liked — please, be kind to thumb up here, my cat does not fear weight problems

--

--