infrastructure

package
v0.0.0-...-84048cf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Module = fx.Options(

	fx.Provide(

		config.New,

		func(cfg *config.Config) bool {
			return cfg.App.Debug
		},
		func(cfg *config.Config) string {
			return cfg.App.Name
		},
		logging.NewLogger,

		func(cfg *config.Config, logger logging.Logger) (*database.Database, error) {
			logger.Debug("initializing database",
				logging.String("host", cfg.Database.Host),
				logging.Int("port", cfg.Database.Port),
				logging.String("name", cfg.Database.Name),
				logging.String("user", cfg.Database.User),
			)
			return database.NewDB(cfg, logger)
		},
		NewStores,

		AsHandler(func(logger logging.Logger, renderer *view.Renderer, contactService contact.Service, subscriptionService subscription.Service, cfg *config.Config) *handler.WebHandler {
			return handler.NewWebHandler(logger,
				handler.WithRenderer(renderer),
				handler.WithContactService(contactService),
				handler.WithWebSubscriptionService(subscriptionService),
				handler.WithWebDebug(cfg.App.Debug),
			)
		}),

		AsHandler(func(logger logging.Logger, userService user.Service) *handler.AuthHandler {
			return handler.NewAuthHandler(logger, handler.WithUserService(userService))
		}),

		AsHandler(func(logger logging.Logger, contactService contact.Service) *handler.ContactHandler {
			return handler.NewContactHandler(logger, handler.WithContactServiceOpt(contactService))
		}),

		AsHandler(func(logger logging.Logger, subscriptionService subscription.Service) *handler.SubscriptionHandler {
			return handler.NewSubscriptionHandler(logger, handler.WithSubscriptionService(subscriptionService))
		}),
	),

	fx.Invoke(
		registerDatabaseHooks,
	),
)

Module combines all infrastructure-level modules and providers. This is the main dependency injection configuration for the application. It follows a specific order of initialization: 1. Configuration is loaded first 2. Logger is set up 3. Database connection is established 4. Stores are created 5. Handlers are registered with their required dependencies

IMPORTANT: When adding new handlers, follow these guidelines: 1. Use the AsHandler function to annotate the handler constructor 2. Provide all required dependencies through functional options 3. Follow the pattern:

AsHandler(func(logger logging.Logger, dependencies...) *handler.SomeHandler {
    return handler.NewSomeHandler(logger, handler.WithDependency(dep)...)
})

Functions

func AsHandler

func AsHandler(f any) any

AsHandler annotates the given constructor to state that it provides a handler to the "handlers" group. This is used to register handlers with the fx dependency injection container. Each handler must be annotated with this function to be properly registered.

Example:

AsHandler(func(logger logging.Logger, svc SomeService) *handler.SomeHandler {
    return handler.NewSomeHandler(logger, handler.WithSomeService(svc))
})

func NewHandlers

func NewHandlers(p HandlerParams) []handler.Handler

NewHandlers creates all application handlers

Types

type HandlerParams

type HandlerParams struct {
	fx.In

	Logger              logging.Logger
	VersionInfo         handler.VersionInfo
	Renderer            *view.Renderer
	ContactService      contact.Service
	SubscriptionService subscription.Service
	UserService         user.Service
	Config              *config.Config
}

HandlerParams contains dependencies for creating handlers. This struct is used with fx.In to inject dependencies into handlers. Each field represents a required dependency that must be provided by the fx container.

type Stores

type Stores struct {
	fx.Out

	ContactStore      contact.Store
	SubscriptionStore subscription.Store
	UserStore         user.Store
}

Stores groups all database store providers. This struct is used with fx.Out to provide multiple stores to the fx container in a single provider function.

func NewStores

func NewStores(db *database.Database, logger logging.Logger) Stores

NewStores creates all database stores. This function is responsible for initializing all database stores and providing them to the fx container.

Directories

Path Synopsis
Package database provides database connection and management
Package database provides database connection and management
Package logging provides a unified logging interface using zap
Package logging provides a unified logging interface using zap

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳