-
Notifications
You must be signed in to change notification settings - Fork 50
/
app.go
127 lines (102 loc) · 3.35 KB
/
app.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package application
import (
"context"
"fmt"
"github.com/justtrackio/gosoline/pkg/appctx"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/kernel"
"github.com/justtrackio/gosoline/pkg/log"
)
type App struct {
configOptions []ConfigOption
configPostProcessors []cfg.PostProcessor
kernelOptions []KernelOption
loggerOptions []LoggerOption
setupOptions []SetupOption
}
func (a *App) addConfigOption(opt ConfigOption) {
a.configOptions = append(a.configOptions, opt)
}
func (a *App) addKernelOption(opt KernelOption) {
a.kernelOptions = append(a.kernelOptions, opt)
}
func (a *App) addLoggerOption(opt LoggerOption) {
a.loggerOptions = append(a.loggerOptions, opt)
}
func (a *App) addSetupOption(opt SetupOption) {
a.setupOptions = append(a.setupOptions, opt)
}
func Default(options ...Option) kernel.Kernel {
defaults := []Option{
WithApiHealthCheck,
WithConfigErrorHandlers(defaultErrorHandler),
WithConfigFile("./config.dist.yml", "yml"),
WithConfigFileFlag,
WithConfigEnvKeyReplacer(cfg.DefaultEnvKeyReplacer),
WithConfigSanitizers(cfg.TimeSanitizer),
WithMetadataServer,
WithConsumerMessagesPerRunnerMetrics,
WithLoggerGroupTag,
WithLoggerApplicationTag,
WithLoggerContextFieldsMessageEncoder,
WithLoggerContextFieldsResolver(log.ContextLoggerFieldsResolver),
WithLoggerHandlersFromConfig,
WithLoggerMetricHandler,
WithLoggerSentryHandler(log.SentryContextConfigProvider, log.SentryContextEcsMetadataProvider),
WithMetrics,
WithProducerDaemon,
WithTracing,
WithUTCClock(true),
}
options = append(defaults, options...)
return New(options...)
}
func New(options ...Option) kernel.Kernel {
var err error
var ker kernel.Kernel
ctx := appctx.WithContainer(context.Background())
config := cfg.New()
logger := log.NewLogger()
if ker, err = NewWithInterfaces(ctx, config, logger, options...); err != nil {
defaultErrorHandler("can not initialize the app: %w", err)
}
return ker
}
func NewWithInterfaces(ctx context.Context, config cfg.GosoConf, logger log.GosoLogger, options ...Option) (kernel.Kernel, error) {
var err error
var cfgPostProcessors map[string]int
app := &App{
configOptions: make([]ConfigOption, 0),
loggerOptions: make([]LoggerOption, 0),
kernelOptions: make([]KernelOption, 0),
}
for _, opt := range options {
opt(app)
}
for _, opt := range app.configOptions {
if err = opt(config); err != nil {
return nil, fmt.Errorf("can not apply config options on application: %w", err)
}
}
if cfgPostProcessors, err = cfg.ApplyPostProcessors(config); err != nil {
return nil, fmt.Errorf("can not apply post processor on config: %w", err)
}
for _, opt := range app.loggerOptions {
if err = opt(config, logger); err != nil {
return nil, fmt.Errorf("can not apply logger options on application: %w", err)
}
}
for name, priority := range cfgPostProcessors {
logger.Info("applied priority %d config post processor '%s'", priority, name)
}
for _, opt := range app.setupOptions {
if err = opt(config, logger); err != nil {
return nil, fmt.Errorf("can not apply setup options on application: %w", err)
}
}
kernelOptions := make([]kernel.Option, len(app.kernelOptions))
for i := 0; i < len(app.kernelOptions); i++ {
kernelOptions[i] = app.kernelOptions[i](config)
}
return kernel.BuildKernel(ctx, config, logger, kernelOptions)
}