Skip to content

Commit

Permalink
[cleanup] codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Xbucks committed Apr 2, 2024
1 parent 85dd270 commit d564945
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 76 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Meysam Hadeli
Copyright (c) 2022 Quan Doan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
107 changes: 41 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,11 @@
[![CI](https://github.com/meysamhadeli/shop-golang-microservices/actions/workflows/ci.yml/badge.svg?branch=main&style=flat-square)](https://github.com/meysamhadeli/shop-golang-microservices/actions/workflows/ci.yml)
![Go Version](https://img.shields.io/badge/go%20version-%3E=1.21-61CFDD.svg?style=flat-square)
# 📍 Shop Golang Microservices

The main idea of creating this project is implementing an infrastructure for up and running distributed system with the latest technology and architecture like Vertical Slice Architecture, OpenTelemetry, RabbitMq in Golang, and we will not deal mainly with business. 🚀

<a href="https://gitpod.io/#https://github.com/meysamhadeli/shop-golang-microservices"><img alt="Open in Gitpod" src="https://gitpod.io/button/open-in-gitpod.svg"/></a>

## The Goals of This Project

- :sparkle: Using `Vertical Slice Architecture` for `architecture level`.
- :sparkle: Using `Rabbitmq` for `Event Driven Architecture` between our microservices with `streadway/amqp` library.
- :sparkle: Using `gRPC` for `internal communication` between our microservices with `grpc/grpc-go` library.
- :sparkle: Using `CQRS` implementation with `mehdihadeli/Go-MediatR` library.
- :sparkle: Using `Postgres` for `database` in our microservices with `go-gorm/gorm` library.
- :sparkle: Using `go-playground/validator` for `validating input` data in the REST calls.
- :sparkle: Using `OpenTelemetry` for `distributed tracing` top of `Jaeger`.
- :sparkle: Using `OAuth2` for implementation `authentication` and `authorization` with `go-oauth2/oauth2` library.
- :sparkle: Using `Echo framework` for `RESTFul api`.
- :sparkle: Using `Swagger` with `swaggo/swag` library for api documentation.
- :sparkle: Using `uber-go/fx` library for `dependency injection`.
- :sparkle: Using `Viper` for `configuration management`.
- :sparkle: Using `logrus` as a `structured logger`.
- :sparkle: Using `Unit Testing`,`Integration Testing` and `End To End Testing` for testing level.
- :sparkle: Using `Docker-Compose` for our `deployment` mechanism.
- :construction: Using `OpenTelemetry` for `monitoring` top of `Prometteuse` and `Grafana`
- :construction: Using `MongoDB` for read side with `mongo-driver`.
- :construction: Using `Domain Driven Design` (DDD) to implement all `business` processes in microservices.
- :construction: Using `Inbox Pattern` for ensuring message idempotency for receiver and `Exactly once Delivery`.
- :construction: Using `Outbox Pattern` for ensuring no message is lost and there is at `At Least One Delivery`.
# Golang Microservices

The main idea of creating this project is implementing an infrastructure for up and running distributed system with the latest technology and architecture like Vertical Slice Architecture, OpenTelemetry, RabbitMq in Golang, and we will not deal mainly with business.

## Plan

> 🌀This project is a work in progress, new features will be added over time.🌀
I will try to register future goals and additions in the [Issues](https://github.com/meysamhadeli/shop-golang-microservices/issues) section of this repository.

## Technologies - Libraries

- ✔️ **[`labstack/echo`](https://github.com/labstack/echo)** - High performance, minimalist Go web framework
- ✔️ **[`go-gorm/gorm`](https://github.com/go-gorm/gorm)** - The fantastic ORM library for Go, aims to be developer friendly
- ✔️ **[`sirupsen/logrus`](https://github.com/sirupsen/logrus)** - Logrus is a structured logger for Go
- ✔️ **[`streadway/amqp`](https://github.com/streadway/amqp)** - Go RabbitMQ Client Library
- ✔️ **[`spf13/viper`](https://github.com/spf13/viper)** - Go configuration with fangs
- ✔️ **[`swaggo/echo-swagger`](https://github.com/swaggo/echo-swagger)** - Echo middleware to automatically generate RESTful API documentation
- ✔️ **[`mehdihadeli/Go-MediatR`](https://github.com/mehdihadeli/Go-MediatR)** - This package is a Mediator Pattern implementation in Go
- ✔️ **[`go-playground/validator`](https://github.com/go-playground/validator)** - Implements value validations for structs and individual fields based on tags
- ✔️ **[`open-telemetry/opentelemetry-go`](https://github.com/open-telemetry/opentelemetry-go)** - Implementation of OpenTelemetry in Go for distributed-tracing
- ✔️ **[`meysamhadeli/problem-details`](https://github.com/meysamhadeli/problem-details)** - Error Handler for mapping our error to standardized error payload to client
- ✔️ **[`go-resty/resty`](https://github.com/go-resty/resty)** - Simple HTTP and REST client library for Go (inspired by Ruby rest-client)
- ✔️ **[`grpc/grpc-go`](https://github.com/grpc/grpc-go)** - The Go language implementation of gRPC. HTTP/2 based RPC
- ✔️ **[`go-oauth2/oauth2`](https://github.com/go-oauth2/oauth2)** - An open protocol to allow secure authorization in a simple and standard method
- ✔️ **[`stretchr/testify`](https://github.com/stretchr/testify)** - A toolkit with common assertions and mocks that plays nicely with the standard library
- ✔️ **[`uber-go/fx`](https://github.com/uber-go/fx)** - Fx is a dependency injection system for Go
- ✔️ **[`cenkalti/backoff`](https://github.com/cenkalti/backoff)** - This is a Go port of the exponential backoff algorithm
- ✔️ **[`stretchr/testify`](https://github.com/stretchr/testify)** - A toolkit with common assertions and mocks that plays nicely with the standard library
- ✔️ **[`testcontainers/testcontainers-go`](https://github.com/testcontainers/testcontainers-go)** - it's a package to create and clean up container for automated integration/smoke tests
- ✔️ **[`avast/retry-go`](https://github.com/avast/retry-go)** - Simple golang library for retry mechanism
- ✔️ **[`ahmetb/go-linq`](https://github.com/ahmetb/go-linq)** - .NET LINQ capabilities in Go

## The Domain And Bounded Context - Service Boundary

![](./assets/shop-golang-microservices.png)

## Structure of Project

In this project I used [vertical slice architecture](https://jimmybogard.com/vertical-slice-architecture/) and [feature folder structure](http://www.kamilgrzybek.com/design/feature-folders/) to structure my files.
Expand All @@ -75,10 +19,6 @@ When adding or changing a feature in an application in n-tire architecture, we a

With this approach, each of our vertical slices can decide for itself how to best fulfill the request. New features only add code, we're not changing shared code and worrying about side effects.

<div align="center">
<img src="./assets/vertical-slice-architecture.png" />
</div>

Instead of grouping related action methods in one endpoint, I used the [REPR pattern](https://deviq.com/design-patterns/repr-design-pattern). Each action gets its own small endpoint, and for communication between our endpoint and handlers, I use [Go-MediatR](https://github.com/mehdihadeli/Go-MediatR) for decouple our endpoint to handlers directly, and it gives use some pipeline behavior for logging, caching, validation and... easily.

The use of the [mediator pattern](https://golangbyexample.com/mediator-design-pattern-golang/) in my endpoints creates clean and thin endpoint. By separating action logic into individual handlers we support the [Single Responsibility Principle](https://en.wikipedia.org/wiki/Single_responsibility_principle) and [Don't Repeat Yourself principles](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself), this is because traditional controllers tend to become bloated with large action methods and several injected `Services` only being used by a few methods.
Expand All @@ -91,6 +31,43 @@ I used CQRS to decompose my features into small parts that makes our application

Using the CQRS pattern, we cut each business functionality into vertical slices, for each of these slices we group classes (see [technical folders structure](http://www.kamilgrzybek.com/design/feature-folders)) specific to that feature together (command, handlers, infrastructure, repository, controllers, etc). In our CQRS pattern each command/query handler is a separate slice. This is where you can reduce coupling between layers. Each handler can be a separated code unit, even copy/pasted. Thanks to that, we can tune down the specific method to not follow general conventions (e.g. use custom postgresql query or even different storage). In a traditional layered architecture, when we change the core generic mechanism in one layer, it can impact all methods.

## How to Run

> ### Docker-Compose
Run our `infrastructure` with `docker` using the [infrastructure.yaml](./deployments/docker-compose/infrastructure.yaml) file with the below command at the `root` of app:

```bash
docker-compose -f ./deployments/docker-compose/infrastructure.yaml up -d
```
##### Todo
I will add `docker-compsoe` for up and running whole app here in the next...


> ### Build
For `building` each microservice, Run this command in root of each microservice where `go.mod` located:
```bash
go build -v ./...
```

> ### Run
For `runing` each microservice, Run this command in root of each microservice where `go.mod` located:
```bash
go run -v ./...
```

> ### Test
For `testing` each microservice, Run this command in root of each microservice where `go.mod` located:
```bash
go test -v ./...
```

> ### Documentation Apis
Each microservice uses swagger open api, navigate to `/swagger` for getting the list endpoints.

Also, to test apis, I created the [shop.rest](./shop.rest) file. This file run with [REST Client](https://github.com/Huachao/vscode-restclient) `VSCode plugin`.

# Support

Expand All @@ -102,8 +79,6 @@ Thanks a bunch for supporting me!

## Contribution

Thanks to all [contributors](https://github.com/meysamhadeli/shop-golang-microservices/graphs/contributors), you're awesome and this wouldn't be possible without you! The goal is to build a categorized community-driven collection of very well-known resources.

Please follow this [contribution guideline](./CONTRIBUTION.md) to submit a pull request or create the issue.

## Project References & Credits
Expand All @@ -115,4 +90,4 @@ Please follow this [contribution guideline](./CONTRIBUTION.md) to submit a pull
- [https://github.com/jbogard/ContosoUniversityDotNetCore-Pages](https://github.com/jbogard/ContosoUniversityDotNetCore-Pages)

## License
This project is made available under the MIT license. See [LICENSE](https://github.com/meysamhadeli/shop-golang-microservices/blob/main/LICENSE) for details.
This project is made available under the MIT license. See [LICENSE](https://github.com/xbucks/go-microservices/blob/master/LICENSE) for details.
8 changes: 0 additions & 8 deletions internal/pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,6 @@ func CreateCustomMap[TSrc any, TDst any](fn MapFunc[TSrc, TDst]) error {
}
maps[k] = fn

//if srcType.Kind() == reflect.Ptr && srcType.Elem().Kind() == reflect.Struct {
// srcType = srcType.Elem()
//}
//
//if desType.Kind() == reflect.Ptr && desType.Elem().Kind() == reflect.Struct {
// desType = desType.Elem()
//}

return nil
}

Expand Down

0 comments on commit d564945

Please sign in to comment.