This tutorial will show you how you can generate a rest API using Go for your Postgres database using monstarillo. The API will use Go, Gorm and Gin. The API will perform CRUD operations on the tables you run it against.
What is Monstarillo
Monstarillo is a template based code generator that works against Postgres, MySQL and Oracle.
How it works
Monstarillo runs templates that are executed by golang’s text template. The templates to run are identified in a json file. At a high level this file will identify the templates to execute, and where to put the output of the template. Monstarillo will also connect to a database and make metadata from the database available to your templates. This metadata includes column information (primary keys, column names, Golang, .Net, Java and Javascript datatypes) and table information (columns and relationships).
Monstarillo will connect to the database you provided and gather information about the tables. It then loops through all of the tables and runs each of your templates. If there are 10 tables in your database each of your template will be run 10 times, once for each table. Find out more at https://monstarillo.com.
Prerequisites
To follow along with this tutorial you will need:
- Monstarillo installed.
- A Postgres database – I will be using the chinhook database. Set up the Chinhook Sample Postgres Database in Docker
- Go installed
- A Go IDE – I will be using GoLand
- Git
The first step will be to get the templates that we will be using to generate our API. To do so clone the repo https://github.com/mrpatrickwright/shared-templates. The templates we will be using are in the go-api folder.
Tell Monstarillo where to put the Generated code
Next you will need to decide where you want the generated code to be placed. I will be running Monstarillo through Docker. I will be exposing the directory ~/shared-volume to the docker image running Monstarillo. The shared-templates folder created by cloning the repo is in the ~/shared-volume folder. I will be generating code in ~/shared-volume/code-gen-output. Monstarillo will create the code-gen-output folder when it generates the code.
Modify the templates-postgres.json file
Monstarillo uses a json file to tell it which templates to run, how to run them, and what to name the files it generates and where to put them. We will be modifying the file go-api/templates-postgres.json. The templates array in the templates-postgres.json file tells Monstarillo which templates to run, what to name the files it creates and where to put them. The tags array in the templates.json file defines some “tags” that are used in templates and/or the templates.json. To run the templates you will need to modify TemplateRoot and OutputPath tags in the tags array.
- TemplateRoot – Template root needs to point to the java-api folder in the repository that you cloned.
- OutputPath – Output path need to point to the folder that you want Monstarillo to put the files it generates in. Monstarillo will create the folder if it does not already exist.
- ModuleName – Used to set the name of the Go module
I am running Monstarillo in locally so I will set the tags to:
{
"tagName": "TemplateRoot",
"value": "/Users/patrickwright/code/shared-templates/go-api"
},
{
"tagName": "OutputPath",
"value": "/Users/patrickwright/code/code-gen-output/go-api-postgres"
},
{
"tagName": "ModuleName",
"value": "github.com/foo"
}
If I was running Monstarillo in docker I would set the tags to:
{
"tagName": "TemplateRoot",
"value": "/usr/local/monstarillo/shared-templates/go-api"
},
{
"tagName": "OutputPath",
"value": "/usr/local/monstarillo/code-gen-output/go-api-postgres"
},
{
"tagName": "ModuleName",
"value": "github.com/foo"
}
Run Monstarillo to Generate The Code
Next we will build the command to run Monstarillo to generate the code for us. We will need to tell Monstarillo that we are using a Postgres database and provide the connection information. We will also need to tell Monstarillo which templates to run by passing the location of the templates.json file we set up earlier.
To run Monstarillo in docker our command will look similar to:
monstarillo postgres \
--t /Users/patrickwright/code/shared-templates/go-api/templates-postgres.json \
--u postgres \
--p <Your Database Password>\
--db "chinhook-db" \
--host "localhost" \
--schema "public"
If I were running monstarillo in docker the command would be
docker run --volume=/Users/patrickwright/code:/usr/local/monstarillo \
--network=host \
monstarillo/monstarillo:latest postgres \
--t /usr/local/monstarillo/shared-templates/go-api/templates-postgres.json \
--u postgres \
--p <Your Database Password> \
--db "chinhook-db" \
--host "localhost" \
--schema "public"
In this command I am mounting /Users/patrickwright/code to the docker image as /usr/local/monstarillo that is running monstarillo. My shared-templates folder is at /Users/patrickwright/code/shared-templates and will be generating code to /Users/patrickwright/code/code-gen-output/go-api-postgres/
When you run the command your output will be similar to:

Notice that Monstarillo prints out each table name and that it runs against. You may be able to use this information to troubleshoot errors in your command.
View Your Generated Code
Next you can view your code by opening the output directory you chose in your IDE of choice.

Looking at the readme we see that there are a few steps we need to take before our project is ready to run. Open a command prompt and cd to the directory your code resides in and execute the following commands:
go mod init github.com/foo
go mod tidy
Substitute github.com/foo for the value of the Module name you used in the templates-postgres.json file.
Update the .env
Locate and update the .env file with the values you need to run against the database you have chosen.
Your API is ready to run.

You can test the API in Postman. You can see all of the routes for your API in the routers/setup.go file. I will test the http://localhost:8080/api/v1/artists endpoint.

You have generated a rest API using Go for a postgres database. You have configured the API to run against your database by updating the .env file and tested it using postman.