Sunday, October 2, 2022
HomeGolangInstance of a modular monolithic codebase in Golang utilizing hooks and dependency...

Instance of a modular monolithic codebase in Golang utilizing hooks and dependency injection


Hooks (examples) – A modular monolithic strategy

Overview

Except for simply offering utilization examples for the hooks library, that is an exploration of modular monolithic architectural patterns in Go by leveraging each hooks and do (for dependency injection). It’s advisable you overview and perceive these libraries earlier than reviewing this repository. Do is just not required to realize the sample illustrated on this software, however I discover it to be a really useful and stylish strategy.

I’m in no way advocating (right now) for this particular strategy however as a substitute utilizing this as an experiment and place to iterate with these concepts. I had plenty of success with modular monoliths with languages and frameworks earlier than studying Golang, and I haven’t encountered related patterns inside the Go ecosystem. Whereas microservices have change into extra distinguished, a modular monolith can’t solely be a more sensible choice in sure circumstances but when completed effectively, could make transitioning to microservices simpler.

The general targets of this strategy are:

  1. Create self-contained modules that signify segments of enterprise logic.
  2. Keep away from any patterns that attain throughout the codebase (ie, the entrypoint getting used to initialize all dependencies, a router that initializes all handlers and routes, and many others).
  3. Modules ought to have the ability to be added and eliminated with out having to the touch the core codebase in any respect.

Repo construction

Under is the repo construction and is only a proposed thought for an efficient, clear group, however there’s no requirement to comply with this.

hooks-example/
├─ modules/         # Modules that every signify a unit of impartial enterprise logic 
│  ├─ analytics/
│  ├─ todo/
├─ pkg/             # Basic-purpose, non-dependency packages which can be utilized throughout the appliance 
│  ├─ app/
├─ providers/        # Providers that are auto-registered as dependencies
│  ├─ cache/
│  ├─ config/
│  ├─ internet/
├─ fundamental.go

Golang Modules

See the func init() inside the major, self-named .go file of every module to grasp how the module auto-registers itself with the appliance.

  • modules/todo: Supplies a easy todo-list implementation with a Todo mannequin, a service to work together with todos as a registered dependency, an HTTP handler as a registered dependency, some JSON REST endpoints, and hooks to permit different modules to change todos earlier than saving and react when they’re saved.
  • modules/analytics: Supplies bare-bones analytics for the appliance, together with the variety of internet requests acquired and the variety of entities created. Included is an Analytics mannequin, a service to work together with analytics as a registered dependency, an HTTP handler as a registered dependency, middleware to trace requests, a GET endpoint to return analytics, a hook to broadcast updates to the analytics, a listener for todo creation to trace entities.

Golang Hooks

Dispatchers

  • pkg/app
    • HookBoot: Signifies that the appliance is booting and permit dependencies to be registered throughout the whole software by way of *do.Injector.
  • providers/internet
    • HookBuildRouter: Dispatched when the net router is being constructed which permits listeners to register their very own internet routes and middleware.
  • modules/todo
    • HookTodoPreInsert: Dispatched previous to inserting a brand new todo which permits listeners to make any required modifications.
    • HookTodoInsert: Dispatched after a brand new todo is inserted.
  • modules/analytics
    • HookAnalyticsUpdate: Dispatched when the analytics information is up to date.

Listeners

  • HookBoot
    • providers/cache: Registers a cache backend as a dependency.
    • providers/config: Registers configuration as a dependency.
    • providers/internet: Registers an internet server as a dependency.
    • modules/analytics: Registers analytics service and HTTP handler as dependencies.
    • modules/todo: Registers todo service and HTTP handler as dependencies.
  • HookBuildRouter
    • modules/analytics: Registers internet route and tracker middleware for analytics.
    • modules/todo: Registers internet routes for todos.
  • HookTodoInsert
    • modules/analytics: Increments analytics entity rely when todos are created.

Boot course of and registration

Under is an try and illustrate how the whole software self-registers ranging from a single hook that’s invoked.

Golang Code

func fundamental() {
  i := app.Boot()
  
  server := do.MustInvoke[web.Web](i)
  _ = server.Begin()
}

Searching for Golang Jobs

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments