Golang REST API with Domain Driven Design

iamsyahidi

Ilham S

Posted on June 9, 2021

Golang REST API with Domain Driven Design

Food App Backend Golang with Domain Driven Design
...

Please kindly visit this repo for the complete source code :Link

List of libraries we will be using :

  • Viper Link
  • Gin Web Framework Link
  • Go ORM / GORM Link
  • Go JWT Implementation Link
  • Go MySQL driver Link
  • Go Email validation Link
  • Bcrpyt hashing Link

...

First thing first

Let's start by initiating dependency management in Go using go.mod, execute in root project

Path: food-app/

go mod init food-app

....

Folder Organization

Alt Text
...

Pkg layer

Environment config

We use MySQL database, the connection configuration will be written in file App.yaml folder configurations

Please adjust the config to fit your local database

Then, inside pkg folder , we make package config to read the configuration from App.yaml . We use viper library to read local config file App.yaml

Path: food-app/pkg/config

...

Database connection

We will define the database connection package in the database.go file, it contains a function to initiate a MySQL database connection with the configuration we took from viper earlier.

Path: food-app/pkg/database

...

Token management

In this application we will use jwt-token to store individual information, verification, and validation

The jwttoken package we define in the token.go file, contains functions to create a jwttoken, extract the contents of the information stored in the jwttoken, and also validate the jwttoken sent from the client.

Path: food-app/pgk/jwttoken

...

Password management

In this application, the user password will be stored in the database in the form of hash data or the implementation of Provos and Mazières bcrypt adaptive hashing algorithm. Link

File hash.go
Path: food-app/pkg/security

...

Response custom

This custom Response Package will handle returning data according to certain needs and conditions

File response.go
Path: food-app/pkg/response

...

Domain Layer

At this layer start to enter the business logic of this application

Entity

We will function the Entity package as a blueprint for the domain, where we put the schema and struct model for food and users

File: food.go
Path: food-app/internal/domain/entity

File: user.go
Path: food-app/internal/domain/entity

Pay attention to the user entity, we have implemented a security package for user password encryption

...

Repository

The Package Repository will function as a place to define a collection of methods, which will later interact with databases, services, and other API integrations

File: food_repository.go
Path: food-app/internal/domain/repository

File: user_repository.go
Path: food-app/internal/domain/repository

...

Service

The Package Service will serve as the place where we implement the methods defined in the repository
File: food_service.go
Path: food-app/internal/domain/service

File: user_service.go
Path: food-app/internal/domain/service

...

Handler

The Package Handler here is where we handle HTTP requests and responses. The entry point for user-related services, and food-related services

File: food_handler.go
Path: food-app/internal/domain/handler

File: user_handler.go
Path: food-app/internal/domain/handler

...

API Layer

Middleware

Package Middleware here functions as an in-between process for us to pass requests from authenticated users and restrict/hold access from unauthenticated users. Here we also define CORS middleware Link so that users can access data from different domains

File: middleware.go
Path: food-app/api/middleware

...

Router

In this Router package we will define our API routes, and mapping the response, also include the middleware earlier

File : router.go
Path : food-app/api

...

Migration

In this application, we have given ddl for the MySQL client to initiate the tables that we are using, please access the food-app.sql file, at the path: food-app/migration

Run the command in it on your local MySQL database client

...

Main.go

In this main package, we will initiate the environment config and open a connection to the database, after success, the application will run locally on the port we specified in the environment.

...

Run application

Make sure your database has been created with dbname according to the environment, the tables have been formed after running the sql command in the migration folder, to run this application do the following command

go mod init
go get
go run main.go

...

That is all and thank you

Good luck

Credits and Insights

💖 💪 🙅 🚩
iamsyahidi
Ilham S

Posted on June 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related