Swift, Perfect, mustache and PostgreSQL on Heroku — 2

Anatoly Rosencrantz
4 min readMay 1, 2016

--

Beginning is here, its code here.

Step 1.3 Add some PostgreSQL

Now we will create PostgreSQL database on mac, access it from our server app and populate more complicated mustache template with recieved data.

Step 1.3.1 Rise Postgres server, add database

If you want, you can make this whole substep in Terminal, but I prefer to use GUI. Its 21st century, guys, we can afford to be lazy sometimes!

Postico with my local databases

Download Postgres.app. Do not forget to set up $PATH as described here, that will reduce you much pain while deploying to Heroku. Run it… and you’ve got Postgres server running on your machine.

Now download Postico app for managing databases, it is cute and its trial has no time restrictions. Run, add new favourite, no need to change anything, connect… Should be something like on the picture.

Add new database named cat_food with table food like that:

I’m not a specialist in database architectures, btw, and haven’t ever tried to be

Populate table with some rows:

Ok, tired of mouse-clicking? Lets do some code.

Step 1.3.2 Food List Template

Add new file FoodList.mustache to Mustache folder in project’s directory. Placement is important: we are placing all .mustache files to one folder to reduce pain with future deployment to Heroku. Spoiler: we’ll build, link and compile project by hands on Heroku’s linux.

Don’t forget to add file to Copy Files section in Build Phases, or HTTP server will not understand which file user wants from him.

FoodList.mustache has contents:

{{%handler:FullFoodList}}{"menu":[{{#whole list}}{"name":"{{name}}","brand":"{{brand}}","contains_any_glutein":{{contains glutein}},"price":{{price}}}{{^last}},{{/last}}{{/whole list}}]}

Now you see why it is called “mustache”? Like in 80s, 👨 everywhere.

Just for fun, name of the file (FoodList) not suits handler’s identifier (FullFoodList). After values will be filled to their places, we want to have ordinary JSON, so keywords inside apostrophes (menu, name, brand, contains_any_glutein, price) must be in acceptable form. But pragma-keys (whole list, name, brand, contains gluten, price, last) can have any form you want and not have to suite JSON keywords.

We do not know how may rows we’ll have in database table and how many of them we want to send to client, so we are defining pattern for one row between {{#whole_list}}…{{/whole_list}} pragmas, which will be repeated as many times as we’ll need.

Pay attention to pragma last — in dictionary with values, provided by our PageHandler, last element of food options array will have flag “last”:true to tell template that comma is not needed there.

Step 1.3.3 Create new PageHandler for that template

Create new file FoodListHandler.swift with contents:

Long story short, here we are:

  • importing PostgreSQL lib
  • setting constants for our database and table (don’t forget to set your own username!)
  • opening connection to db with use of those constants
  • not forget to close connection in defer block (which will be called before returning from this method, no matter where it will happen)
  • check that connection was opened successfully
  • execute query — in this tutorial I will not describe any sql-stuff, you can read it in Postgre’s cool and fancy documentation
  • checking that nothing unpredictable happened
  • parse results of the query to form that we want — dictionary [String:Any]
  • add last:true to last element
  • and add header “whole list” to this list, wrapping into MapType dictionary

Step 1.3.4 Register new PageHandler

Last thing to do is to connect our mustache template to this FoodListHandler PageHandler class. Open PerfectHandlers.swift and add to the end of PerfectServerModuleInit() method lines:

This code will register mustache template with handler FullFoodList to be processed with FoodListHandler class. Add to the end of PerfectHandlers.swift

Pay attention: here we are using FullFoodList like in template’s pragma.

Step 1.3.5 Check that all works

First, run your Postgres server. Than build and run project and try to send request:

👏🏻☺️

But if that didn’t work, my shortlist for troubleshooting:

  1. Mustache evaluation error — if check that handler identifier in mustache is totally same as one of those we’ve added in PerfectServerModuleInit()
  2. File /foodlist not found — check that mustache file is added to Cope Files section in Build Phases
  3. All other code 500 errors we’ve described in code

Project in this state in branch step_1.3_postgres on github.

Next step will be… yeah, Part 2! Make it work on Heroku!

These tutorials are participating in Perfect’s contest. If you’ve liked — please, be kind to thumb up here, my cat wants moar food!

--

--