bxcodec / go-clean-arch

Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture
MIT License
9.06k stars 1.19k forks source link

How to handle shared middleware and support functions? #53

Closed egru closed 2 years ago

egru commented 4 years ago

I've been using your go structure for a project I'm working on and I'm hitting a few areas that I'm not quite sure how to solve, at least in a clean architecture way. If we take your directory structure as an example, say we have an http handler for author as well as article. Then say we want to use the same middleware for each handler. How the structure is currently set up is that there's a middleware package within the deliver/http directory for article. Would we have to create the exact same middleware package in author too? Duplicating code is probably not the answer here. Would it be better to create a top level handler/http directory with the middleware in there that can be shared across all entities? How about other utility functions?

bxcodec commented 3 years ago

Hi, sorry for the late reply. So busy lately

Thanks for using this proposed structured project. I also face the same thing you had.

I'm still experimenting with a lot of structure for now. But I'll list what possible way that I've done.

.
├── Dockerfile
├── Makefile
├── README.md
├── app
│   ├── cmd
│   │   ├── rest.go
│   │   └── root.go
│   ├── config
│   │   ├── app.go
│   │   ├── postgres.go
│   │   ├── root.go
│   │   ├── root_test.go
│   │   ├── server.go
│   │   └── server_test.go
│   └── main.go
├── deploy.Dockerfile
├── dev.Dockerfile
├── docker-compose.test.yaml
├── docker-compose.yaml
├── docs
│   └── openapi.yaml
├── ebus
│   ├── README.md
│   ├── ebus.go
│   ├── ebus_test.go
│   ├── rabbitmq
│   │   └── rabbitmq.go
│   └── sns
│       └── sns.go
├── errors.go
├── go.mod
├── go.sum
├── internal
│   ├── README.md
│   ├── graphql
│   │   └── README.md
│   ├── grpc
│   │   └── README.md
│   ├── mongo
│   │   └── README.md
│   ├── postgres
│   │   ├── README.md
│   │   ├── migration.go
│   │   ├── migrations
│   │   │   ├── 20191018113359_create_todo_table.down.sql
│   │   │   └── 20191018113359_create_todo_table.up.sql
│   │   ├── postgres_suite.go
│   │   ├── todo.go
│   │   ├── todo_test.go
│   │   ├── utils.go
│   │   └── utils_test.go
│   ├── rabbitmq
│   │   └── README.md
│   ├── redis
│   │   └── README.md
│   ├── rest
│   │   ├── README.md
│   │   ├── middleware
│   │   │   ├── context_timeout_injector.go
│   │   │   └── middleware.go
│   │   ├── todo.go
│   │   └── todo_test.go
│   ├── serviceyoudependon
│   │   └── README.md
│   ├── sns
│   │   └── README.md
│   ├── sqlx
│   │   ├── tx.go
│   │   └── tx_test.go
│   └── sqs
│       └── README.md
├── mock.Dockerfile
├── mocks
│   ├── ITodoRepository.go
│   ├── ITodoService.go
│   └── README.md
├── test.Dockerfile
├── todo
│   ├── service.go
│   └── service_test.go
└── todo.go

23 directories, 58 files

Let me know what do you think about this. Also if you have a better Idea, let's discuss it over here.

bxcodec commented 3 years ago

I'll submit an example PR in the upcoming week *if not so busy LOL