-
Notifications
You must be signed in to change notification settings - Fork 0
Monolithic Microservice
Monolithic Microservice is an approach that recommends lower levels of abstraction than MVC, making the code more readable for smaller projects or services. It abstracts singleton services such as Database, Cache or Logger into separate packages which can be used as global providers.
💡 Don't use this approach on multi-database or multi-purpose services. Use classic MVC or another layering method instead.
The classical approach prohibits usage of global objects and singleton, making every part of the project abstract - everything needed is passed into the constructor or function.
The service may then look like this:
type Users struct {
DB *gorm.DB
Cache *redis.Client
Log *log.Logger
StatService *statservice.Client // connection to other service
Options Options // local options
}
function NewUsers(db *gorm.DB, cache *redis.Client, log *log.Logger, svc *statservice.Client, opts Options) *Users {
return &Users{
db, cache,
log, svc,
opts
}
}
This approach makes testing really easy as all we really need to do is mock all of our services to unit test this service. However, most services don't really do much of a business logic - they instead act just as proxies, while DBs or other services are doing the heavy lifting.
We'll describe a single service that is implemented with the package abstraction. The monolithic packaging looks like this:
users/
├── providers/
│ ├── db
| ├── db.go
| └── db.mock.go
│ ├── log
| ├── log.go
| └── log.mock.go
| └── providers.go
├── /bin/users
│ └── main.go
├── service.go
└── service.mock.go
The service.go
then implements main business logic, while providers contain singleton services which can be imported and used directly.
package users
import (
// all providers here should be global, such as Log, DB, Cache, ...
_ "github.com/flowup/project/users/providers"
)
type Users struct {
Options Options
}
func (s *Users) GetUser(id string) *User {
Log.Info("...")
}
-
FE / Angular
-
Golang
-
DevOps
-
Agile process and SCRUM
-
Internship
-
Bootcamp