DEV Community

Afzal Ansari
Afzal Ansari

Posted on

Developing a Web App with Gin framework, integrating it with psql DB and my GO HACK Hackathon Experience


Before I deep dive into the architecture of what I rebuilt an app during the hackathon, I would like to tell about my experiences in my words.
When I first came across #GO Hack Challenge on devpost, I was overwhelming to find related tracks which I could be fit for and tutorials to enrich my knowledge here and there to get into it. It was really a learning opportunity to someone who is newbie and wants to contribute or build any stuff under the hood.
I stepped into the open source projects under CERN Foundation, where I didn’t even know what the ecosystem around the project is. I was much curious then. Next, I delved into one of the projects for a few days and looked into a few issues and made a small contribution to it by fixing one of the bugs. I moved to another track where we needed to build any app based on golang. I came to know about use of generic function which is currently released and used in Go >= 18 Version. I managed to rebuild an app using Gin-Gonic framework and use the generic function usage in my app.

Monolithic services architecture enables developers to develop product simpler and deploy easier, which can further help them achieve the product's needs sharply. This has also better development productivity ultimately. With that, I am going to give you a short demonstration on how to build a web app using monolithic way.

Well, this looks scary when you never heard about this architecture. But when you start building things on top of that, you can gradually understand the scope of this architecture i.e. when and how to use it.

We take the resource management app as an example to build a monolithic service here. The app service is consists of multiple modules, the modules include add resource, get resource and login modules, etc. Each module will have its own independent business logic, and each module will also depend on some others. For example, the get resource (/resource/get-resc) module will depend on the login module. In the monolithic application this kind of dependency is usually accomplished by method calls between modules. Monolithic services generally share storage resources, such as MySQL but in our case, it is PSQL.

The overall architecture of monolithic services is relatively simple, which is also the advantage of monolithic services.


This section describes how to quickly implement a resource management app monolithic service based on gin framework of golang.
With this framework, we can quickly build, configure and deploy resources within a few commands. We can create and store our API’s and configuration in a centralized repository as many as possible.

Getting Started

To get started, I would assume you have GOlang already installed and configured locally and the corresponding Go package of verion >=18 for this app. For more details, please follow the official docs. Once we set up the requirements, we will proceed with the next steps to play around GO gin pkg for apis and gorm pkg for database instance creations.

So, let’s install plugins and libraries step by step;

The below commands will clone basic templates for you to work with the API after you clone the repo. Plz, ignore the docker file for now.

After that install the dependencies as follows; this allows you to update the pkg installed.

go mod download

go mod tidy

Let’s dive into main.go file and create another file inside our routes dir.

User and Resource model API definition

type Resource struct {
    ID        int    `json:"id"`
    Title     string `json:"title"`
    Category  string `json:"category"`
    Status    string `json:"status"`
    Types     string `json:"types"`
    Content   string `json:"content"`
    FileLink  string `json:"file_link"`
    CreatedBy int    `json:"created_by"`
    CreatedAt int64  `json:"created_at"`
    UpdatedBy int    `json:"updated_by"`
    UpdatedAt int64  `json:"updated_at"`

type User struct {
    ID        int    `json:"id"`
    FirstName string `json:"first_name"`
    LastName  string `json:"last_name"`
    Password  string `json:"password"`
    Email     string `json:"email"`
    Role      string `json:"role"`

Enter fullscreen mode Exit fullscreen mode

Resource module API definition

Add Resource details -> resource/add-resource
View Resource details -> resource/get-resc
Login User -> resource/login
Enter fullscreen mode Exit fullscreen mode


Another way to handle http request through http.HandleFunc to support generic function here as shown below
handlefunc and generic func

To more about generic function, you can definitely check this link out.

Let's deep dive into gorm pkg and psql pkg a little bit now.

Since we are focusing here PostgreSql, we look into that sql query managed by that as shown below;
In order to execute the program, you need to have psql installed in your system:

Using Linux env and running on its terminal:

sudo apt install postgresql -> installs the psql

sudo -u postgres psql -> sudo into user name as postgress

psql --version -> checks version

export RESC_DB_DSN='postgres://USER_NAME:YOUR_PWD@localhost/DB_NAME' psql $RESC_DB_DSN

psql --host=localhost --dbname=DB_NAME --username=USER_NAME
Enter fullscreen mode Exit fullscreen mode


I hope we are done with the psql creation and its usage through gorm pkg.

Let's run the App using go run main.go, then using POSTMAN API Client or curl command you can go ahead request the body.
curl localhost:8080/resource/get-resource

Using Postman


What’s next!

I am looking forward to adding more API endpoints to make use of HTTP requests persistently and integrating with kubernetes cluster


The above walk-through shows that it is very simple to use go-gin gonic framework to develop monolithic services. You only need to define the api file, and then the goctl tool can automatically generate the project code. We only need to fill in the business logic code in the logic package. In this article, I just demonstrated how to quickly develop monolithic services based on go-gin, gorm pkgs and generic function. Hope, you find it insightful and adopt the usage in building your app.

Top comments (0)