Deploy gin-gonic (REST API) free in Heroku + Code
Well, there are many ways to create REST APIs, but when someone is looking for one of the fastest one, Gin-gonic is the answer.
This web framework written in Go are really fast and easy to handle as a SPA (Single page application). ¿SPA? Yes, this framework is designed to run as simple as possible. That means that is not designed to follow a design patter like MVC or similar that separes functions of the system on different files and folders.
I’m here for the code! Okey, okey, keep calm, I promise I’ll give you a repository (see the bottom) to fork or clone that it’s ready to being deployed to Heroku. You just will copy your code and deploy.
Now, the explanation for each file:
It’s a file that indicates heroku which framework is going to use and indicates the folder where is located our main go file “server.go”
It’s a file that indicates the name of the main app. If your main app is under a folder you’ll need to change to “web: my_folder/server” without the extension “.go” because you have already written on “heroku.yml” which framework are you using
This file is read by Heroku to choose the python version.
File that store environment variables such as PORT and any other that you want you use in your program.
- “go.sum” & “go.mod”
Files generated by “git mod init server && go mod vendor”. The first one creates go.mod and store version of Golang and name of the project. The second one set the path to a folder called vendor where dependencies are going to being stored. You can delete vendor folder, go.sum and go.mod and nothing will break. Just generate again.
In Heroku follow the steps:
- Create an app and write a name
- Select “Deploy”
- Choose Github in deployment method or any other that you want
- Deploy with “Deploy Branch” and enable automatic deploys, so in each push to github, Heroku will build the page and deploy.
That’s all, and the template repo is here:
*Don’t forget to change the folder that store the git repo in “heroku.yml”
Golang template with Gin-Gionic for Heroku Deployment Go template for Heroku apps go mod init server # packages go get…