Skip to content

mfauzirh/go-fiber-mongo-hexagonal-architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hexagonal Architecture Using GO

🚀 Find a more detailed explanation about the concept and code in my LinkedIn Article.
⭐ Give a star if you think this repository is awesome

Table of Contents

🤔 About

This repository shows how to implement hexagonal architecture using Go. Hexagonal Architecture, or Ports & Adapters Architecture, is a cool way to build software systems that stay flexible and easy to update. Hexagonal Architecture

To demonstrate, I created a simple REST API for product management. What implemented in this repository:

  • Build REST API for product management using Go, Fiber, and MySQL.
  • Organize our project to implement hexagonal architecture.
  • Implement request profiling to the requests our server receives, storing them in the database.
  • Create unit testing for repository, service, and handler.

To run the project, you should fulfilled this requirements:

  • Go (I'm using version go1.21.3)
  • MySQL database (For storing our product)
  • MongoDB database (For storing our request profiling)
  • IDE/text editor (I'm using VsCode)
  • Postman for testing the API

🏁 Getting Started

Setup MySQL Database

First, you need to setup the MySQL database, create a database named "golangdb" with bellow command.

CREATE DATABASE golangdb;

Next, create a product table with this command.

CREATE TABLE golangdb.products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    stock INT NOT NULL CHECK (stock >= 0),
    price INT NOT NULL CHECK (price > 0)
);

Setup MongoDB Database

To set up MongoDB to store our profiling requests. Create a new database called “product-management”, then create a collection called “request-logs”. You can easily create this using the MongoDB Compass GUI.

The end result will look like this. MongoDB

Running the Go Application

To run the program by typing this command in the terminal, your position at the root of the project.

go run ./cmd/http/main.go

The application will run as below Go Run

Running the Unit Test

To run test you can run this command in your terminal, your position is in the root of the project.

go test -v ./...

That's will run all your unit test, the result should be like below. Go Run Test

😎 Result

API Result

  • Create New Product Create Product
  • Get All Product Get Products Product
  • Get Product By Id Get Product By Id
  • Update Product Update Product
  • Delete Product Delete Product

Request Profiling

  • Logging Preview Profiling Log In Terminal
  • Persisted in MongoDB Profiling Log Persist in DB

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages