My new Best friends

My new Best friends

How I used GORM and Gin to create my API

Today's post is more of a study that I would like to share with you.

What is GORM?

It's an ORM library for Go.

Oh, ok... So what's an ORM?

ORM stands for Object Relational Mapping. ORM works as a bridge between object-oriented programs and relational databases. Even though Go is NOT an OOP language, it's OOP-ish, so an ORM makes sense for it. I'm not going in-depth into it, but there's this article by Lukas Lukac that goes over this.

I could just not use an ORM. I could simply use SQL scripts for every single request. But that sounds like a lot of work. An ORM simplifies this by generating SQL scripts for us. And in this project,

So now that we know what GORM is, why will I use it?

I've not been shy about how much I don't know about stuff. GORM is here to help with the heavy lifting. Instead of learning SQL and creating a query for every single request I want to do, I'll abuse this library to get small, comprehensible code. I'll try to write code as simply as possible and as efficiently as I can.

What is Gin?

Gin (/ˈdʒɪn/) is a distilled alcoholic drink that... no wait. Wrong link.

According to gin-gonic's website "Gin is a HTTP web framework written in Go (Golang)". It performs 40x better than Martini, a similar framework. It's blazingly fast and it will simplify our code. Its benefits include middleware support, crash-free, JSON validation, routes grouping, error management and it's extendable.

So why use it. I'll create a GET route for our activities so we can see how this helps us. I'll start creating a routes directory and package.

package routes

import (
   "github.com/gin-gonic/gin"
   "github.com/spintrack-api/controllers"
   "log"
)

func HandleRequests() {
   r := gin.Default()

   r.GET("/api/activities", controllers.GetActivities)

   err := r.Run()
   if err != nil {
      log.Panic("Error running Gin", err.Error())
   }
}

If we run the HandleRequests function in our main.go file, it will serve HTTP in my localhost:8080 port. As simple as that. When I go to this URL it will perform the GetActivities function, so I'll create the controllers package too.

package controllers

import (
   "github.com/gin-gonic/gin"
   "github.com/spintrack-api/database"
   "github.com/spintrack-api/models"
)

func GetActivities(c *gin.Context) {
   var activities []models.Activity
   database.DB.Find(&activities)
   c.JSON(200, activities)
}

The GetActivities is accessing the database through GORM and using the Gin Context from the routers package to give a JSON response. So back to our main.go file.

func main() {
   fmt.Println("Hello SpinTrack API")
   database.ConnectToDb()
   routes.HandleRequests()
}

When we run this code Gin is doing all the work for us. If we access http://localhost:8080/api/activities in our browser, something magical happens.

May not look like much, but this is a JSON response straight from our Postgres database on AWS of all the activities stored. But we have none, therefore it's empty. But when we do create them, it's going to be magical.

This is only 1% of what Gin brings to the table. It also has a real-time debug log, making it easy to keep track of our HTTP requests.

The next time, I'll make some more API endpoints for this project so we can actually store something in the DB. So stay curious!

Check out my project repository!

Cover photo by Priscilla Du Preez on Unsplash