• Stars
    star
    278
  • Rank 148,454 (Top 3 %)
  • Language
    Go
  • Created almost 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Golang API project showcase heavily influenced by the Clean Architecture and Hexagonal Architecture, using PostgreSQL.

What is this repository for?

Golang app architecture showcase using Gin, PostgreSQL and Gorm as ORM. You can start your Golang projects building on this boilerplate.

Architecture Overview

The app is designed to use a layered architecture. The architecture is heavily influenced by the Clean Architecture and Hexagonal Architecture. Clean Architecture is an architecture where the business rules can be tested without the UI, database, web server, or any external element.

Also, in entry point(server.go), I use Dependency Injection(DI). There are many reasons using Dependency Injection as:

  1. Decoupling
  2. Easier unit testing
  3. Faster development
  4. Dependency injection is really helpful when it comes to testing. You can easily mock your modules' dependencies using this pattern.

You can take a look at this tutorial: https://blog.risingstack.com/dependency-injection-in-node-js/. According to DI: A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details.

The code style being used is based on the airbnb js style guide.

Data Layer

The data layer is implemented using repositories, that hide the underlying data sources (database, network, cache, etc), and provides an abstraction over them so other parts of the application that make use of the repositories, don't care about the origin of the data and are decoupled from the specific implementations used, like the Gorm that is used by this app. Furthermore, the repositories are responsible to map the entities they fetch from the data sources to the models used in the applications. This is important to enable the decoupling.

Domain Layer

The domain layer is implemented using services. They depend on the repositories to get the app models and apply the business rules on them. They are not coupled to a specific database implementation and can be reused if we add more data sources to the app or even if we change the database for example from MongoDB to Couchbase Server.

Routes/Controller Layer

This layer is being used in the express app and depends on the domain layer (services). Here we define the routes that can be called from outside. The services are always used as the last middleware on the routes and we must not rely on res.locals from express to get data from previous middlewares. That means that the middlewares registered before should not alter data being passed to the domain layer. They are only allowed to act upon the data without modification, like for example validating the data and skipping calling next().

Entry point

The entry point for the applications is the server.go file. It does not depend on gin or other external frameworks. It is responsible for instantiating the application layers, connecting to the db and mounting the http server to the specified port.

Quick start

Prerequisites

Create an .env file in project root to register the following required environment variables:

  • POSTGRES_HOST - Postgres DB host
  • POSTGRES_PORT - Postgres DB port
  • POSTGRES_USER - Postgres DB user
  • POSTGRES_DB - Postgres DB name
  • POSTGRES_PASSWORD - Postgres DB password

Use Docker:

You can use Docker to start the app locally. The Dockerfile and the docker-compose.yml are already provided for you. For this option you must specify following var in the .env file:

DATABASE_URL

then run the following command:

docker-compose up

Support Me

ko-fi

Show your support

Give a ⭐️ if this project helped you!

More Repositories

1

nodejs-api-showcase

A Node.js App Architecture showcase heavily influenced by the Clean Architecture and Hexagonal Architecture, using MongoDB.
JavaScript
252
star
2

network-avatar-picker

A npm module that returns user's social network avatar. Supported providers: facebook, instagram, twitter, tumblr, vimeo, github, youtube and gmail
JavaScript
96
star
3

rust-api-ddd

Rust API server showcase heavily influenced by the DDD, Clean Architecture and Hexagonal Architecture, using PostgreSQL.
Rust
27
star
4

throw-http-errors

Custom errors for HTTP status codes.
TypeScript
11
star
5

couchbase-server-promises

Couchbase-server-promises library wraps couchbase's callback functions for handling the result of the asynchronous operations, with Bluebird promises to provide a convenient, promise-based interface.
JavaScript
6
star
6

gcf-stripe-boilerplate

Serverless implementation of basic Stripe requests using google cloud functions(Faas)
JavaScript
6
star
7

real_time_streaming_analytics

we look at how we can use managed services such as Amazon Kinesis to handle our incoming data analytics streams from our sources while AWS handles the undifferentiated heavy lifting of managing the infrastructure
HCL
6
star
8

js-notes

Chapter series to learn JS in depth
HTML
5
star
9

express-async-handler

Simple middle to handle exceptions and catch error within express routes in asynchronous and pass them to your express error handlers.
TypeScript
2
star
10

express-error-handler

TypeScript
2
star
11

slack-golang-gcf

Google cloud function which sends messages to slack via slack-webhook and triggered by http request
Go
2
star
12

Lisp_to_JS_converter

API that convert programs in common lisp into their javascript equivalent.
JavaScript
2
star
13

sequencers-pipeline

An implementation of pseudo-generators without the use of es6 generators and a function that pipes these generators to various functions.
JavaScript
2
star
14

jwt-jwks-authentication

JavaScript
2
star
15

filestage_test_challenge

Solution to filestage's test challenge
JavaScript
1
star
16

sendgrid-golang-gcf

Google cloud function(FaaS) which sends email via sendGrid and triggered by http request
Go
1
star
17

in-memory-database

TypeScript
1
star