package factory import ( "fmt" "os" "github.com/rs/zerolog" "www.thenautilus.net/cgit/go-example/business" "www.thenautilus.net/cgit/go-example/config" "www.thenautilus.net/cgit/go-example/logging" "www.thenautilus.net/cgit/go-example/something" ) type Factory struct { somethingClient *something.Something businessLogic *business.Logic config *config.MainConfig logger zerolog.Logger } func New(logger zerolog.Logger, config *config.MainConfig) Factory { return Factory{ config: config, logger: logger, } } func NewFromConfig() Factory { config, err := config.GetMainConfig() if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } log := logging.Logger(config.Logger) log.Info().Object("config", &config).Msg("configuration") return New(log, &config) } func (f *Factory) Logger() zerolog.Logger { return f.logger } func (f *Factory) SomethingClient() *something.Something { if f.somethingClient == nil { something := something.New( &f.config.Something, ) f.somethingClient = &something } return f.somethingClient } func (f *Factory) BusinessLogic() *business.Logic { if f.businessLogic == nil { logic := business.New( &f.config.BusinessLogic, f.SomethingClient(), ) f.businessLogic = &logic } return f.businessLogic }