aboutsummaryrefslogtreecommitdiff
path: root/factory/factory.go
blob: ad3262e85506dacc030802623d01abc844649be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
}